简体   繁体   中英

How to use laravel sanctum without typical laravel /login

I have been created PWA with NuxtJS,

then I'm going to use sanctum package , but I don't want to request to the typical Laravel /login route, because I have a customized api /login route which authenticates users with OTP, not by password!

So I'm wonder how can I use sanctum in this situation??

You can use Sanctum's token based authentication to achieve this. All you have to do is add the HasApiTokens trait to your authenticable model and issue new tokens for it on a successful login. So you would have a route which will accept phone number or email and perform a login action. This would trigger the application to send OTP to the user. You have to store the OTP somewhere on the system. Then create another endpoint where user's can send back the OTP they received. This route will then check if the provided OTP matches the one on the system and issue a token to the user.

$token = $user->createToken(<provide-a-token-name>);

Make sure to send back the plaintext token to the user using the plainTextToken function on the token instance. User's will need to add this token to requests as a bearer token in the Authorization header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM