Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post request by axios to laravel giving 500 error #2227

Open
oussama1478 opened this issue Aug 20, 2023 · 1 comment
Open

post request by axios to laravel giving 500 error #2227

oussama1478 opened this issue Aug 20, 2023 · 1 comment

Comments

@oussama1478
Copy link

oussama1478 commented Aug 20, 2023

Subject of the issue

post request by axios to laravel giving 500 error

Your environment

Q A
Bug? yes
Framework Laravel / react

Steps to reproduce

I am using Laravel with react making http request with the axios. It is giving 500 error:
POST https://api.oussama-dev.online/api/login 500 (Internal Server Error)

My request code is

const onSubmit=(ev)=>{
  ev.preventDefault();
  setError('')
  axiosClient.post('/login',{
    
    email,
    password,
   
  }).then(({data})=>{
   setCurrentUser(data.user)
  
   setUserToken(data.token)
   setUserType(data.user_type)
   localStorage.setItem('user_id', data.user.id);
  })
 
  .catch((error)=>{
    if (error.response) {
      setError(error.response.data.error);
    }
    console.error(error);
  });
};

Lavael Route is

Route::post('/login',[AuthController::class,'login']);

And Controller is

public function login(LoginRequest $request)
    {
        $credentials = $request->validated();
        $remember = $credentials['remember'] ?? false;
        unset($credentials['remember']);

        if (!Auth::attempt($credentials, $remember)) {
            return response([
                'error' => 'mot de passe incorrect'
            ], 422);
        }
        $user = Auth::user();
        $token = $user->createToken('main')->plainTextToken;

        return response([
            'user' => $user,
            'token' => $token,
            'user_type' => $user->usertype,

        ]);
    }
@kirkbushell
Copy link

What does this have to do with JWT?

You haven't provided any details about the error or anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants