简体   繁体   中英

How to use simple API token together with web based login in Laravel

I developed a laravel 5.8 web dashboard to allow users to check their power consumption.

I used auth:make to scaffold all auth contollers and related routes, but now I need to expose some API endpoints to be called by my partners and for this I chose simple API token to authenticate (it is sufficient to my problem).

My question is : do I have to write new controllers for login/logout or I can simply use the existing ones (I need it to support both web and API access).

So this code might go for API login (while keeping the original logincontroller for web users) // public function login(Request $request) // { // $this->validateLogin($request);

//     if ($this->attemptLogin($request)) {
//         $user = $this->guard()->user();
//         $user->generateToken();

//         return response()->json([
//             'data' => $user->toArray(),
//         ]);
//     }

//     return $this->sendFailedLoginResponse($request);
// }

When using API token, there is no need for any login/logout controllers. Authentication is done simply by passing along the API token with each request.

如上所述,通过向API添加登录控制器来解决该问题。

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