简体   繁体   中英

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 ). 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

{
 username: username,
 token: token
}

MyAPI will POST this information on to the hostAPI . This will return either success or a 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 . 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]);

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