我们有 auth 微服务和基于 laravel 的管理面板。我们通过调用 auth api 并返回用户 ID 和 token 来登录用户。我如何让用户在 laravel 5.3 中获得授权?我没有找到任何信息谷歌搜索。
$request = Request();
$authorize = new Authorize();
$response = $authorize->logIn($request->all());
if ($response->status == 'success') {
$user = new User();
$userResponse = $user->getUser($response->data->id);
Session::put('userId', $response->data->id);
Session::put('userToken', $response->data->token);
Session::put('userData', $userResponse);
if ($request->input('save_login')) {
Cookie::queue(Cookie::make('savedLogin', $request->input('login'), 129600, null, null, false, false));
} else {
Cookie::queue(Cookie::forget('savedLogin'));
}
return redirect('/');
}
最佳答案
您可以使用id
或用户实例手动登录用户
。
来自docs :
验证用户实例
If you need to log an existing user instance into your application, you may call the login method with the user instance. The given object must be an implementation of the Illuminate\Contracts\Auth\Authenticatable contract. Of course, the App\User model included with Laravel already implements this interface:
Auth::login($user);
// Login and "remember" the given user...
Auth::login($user, true);
通过 ID 验证用户
To log a user into the application by their ID, you may use the loginUsingId method. This method accepts the primary key of the user you wish to authenticate:
Auth::loginUsingId(1);
// Login and "remember" the given user...
Auth::loginUsingId(1, true);
关于laravel - laravel如何给用户授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52815968/