tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.23k stars 1.55k forks source link

post request by axios to laravel giving 500 error #2227

Open oussama1478 opened 10 months ago

oussama1478 commented 10 months ago

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 commented 10 months ago

What does this have to do with JWT?

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