简体   繁体   English

使用用户名和令牌登录 Laravel API

[英]Laravel API login with username and token

I am developing a laravel API( MyAPI ) with the 5.1 version.I am connecting to this API from a widget which hosted in another website.it has a separate API( hostAPI ).我正在开发一个 5.1 版本的 Laravel API( MyAPI )。我从另一个网站上托管的小部件连接到这个 API。它有一个单独的 API( hostAPI )。 I need to authenticate user when widget loaded.我需要在加载小部件时对用户进行身份验证。

following are my requirement.以下是我的要求。

" When the widget loads it will attempt to authenticate. It will send a request to the MyAPI with " 当小部件加载时,它将尝试进行身份验证。它将向 MyAPI 发送一个请求

{
 username: username,
 token: token
}

MyAPI will POST this information on to the hostAPI . MyAPI会将此信息发布到hostAPI 上 This will return either success or a 401.这将返回成功或 401。

On sucess, we log the user in. We may need to create an account if an user with that name does not exist"成功后,我们将用户登录。如果不存在具有该名称的用户,我们可能需要创建一个帐户”

how can i auth a user with only username and a token key我如何验证只有用户名和令牌密钥的用户在此处输入图片说明

Is not a good practice to login user by user interface.通过用户界面登录用户不是一个好习惯。 But it is an option and maybe in your case you can use that.但这是一种选择,也许在您的情况下您可以使用它。

So you know only the username, and token.所以你只知道用户名和令牌。

I think you can query the database based on you username and token .我认为您可以根据您的 username 和 token 查询数据库。 find the user and login the selected one :)找到用户并登录所选用户:)

Example:例子:

$user=User::where('username',$username)->where('token',$token)->first();
Auth::login($user);
return  Auth::user();

In case you want to create the user if it does not exist:如果您想创建不存在的用户:

$user=User::where('username',$username)->where('token',$token)->firstOrCreate(['username' => $username,'token'=>$token]);

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

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