简体   繁体   English

如何在Laravel中将简单的API令牌与基于Web的登录一起使用

[英]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. 我开发了laravel 5.8 Web仪表板,以允许用户检查其功耗。

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). 我使用auth:make来搭建所有auth contollers和相关路由,但是现在我需要公开一些由合作伙伴调用的API端点,为此,我选择了简单的API令牌进行身份验证(这足以解决我的问题)。

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). 我的问题是:我是否必须编写新的控制器用于登录/注销,还是可以仅使用现有的控制器(我需要它来支持Web和API访问)。

So this code might go for API login (while keeping the original logincontroller for web users) // public function login(Request $request) // { // $this->validateLogin($request); 因此,此代码可能会用于API登录(同时保留原始的web用户的logincontroller)// //公共函数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. 使用API​​令牌时,不需要任何登录/注销控制器。 Authentication is done simply by passing along the API token with each request. 只需通过随每个请求传递API令牌即可完成身份验证。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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