Answer the question
In order to leave comments, you need to log in
How to properly use Laravel Sanctum?
public function login()
{
$validator = Validator::make(request()->all(), [
'email' => 'required|email|max:255',
'password' => 'required|string',
]);
if ($validator->fails()) return response()->json($validator->errors()->getMessages());
$user = User::where('email', request()->email)->first();
if (! $user || ! auth()->attempt(request()->only(['email', 'password']))) {
return response()->json(['message' => 'Unauthorized'], 401);
}
$token = $user->createToken('token-name');
}
Answer the question
In order to leave comments, you need to log in
1. It is necessary to do so that there are no these most at each authorization . Once entered and remembered. This is not oAuth2. If you need oAuth2 then use Passport
2. Because tokens are eternal. Their fading, updating and everything that is in oAuth2 is not provided in Sanctum.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question