I am developing a project with vue as frontend and laravel 8 api. I am using JWT for authentication. I understand a token has to be generated before a user can login, but is there a way to automatically log in the user after they register?
Return the token after the user was registered succesfully:
/*This is your register function, this is after you succesfully saved the user in the database.
Remember to use JWTAuth*/
/*YOUR REGISTER LOGIC HERE, THEN*/
if($user->save()){
$credentials = ["email"=>$user->email,"password"=>password_from_request];
try{
if(JWTAuth::attempt($credentials)){
return $this->respondWithToken($token);
}
}
catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => $e], 500);
}
}else
return $this->userRegisterFailedResponse();
/*Your custom response in case the register fails*/
You can check the implementation of the respondWithToken function at the JWT documentation