laravel - laravel如何给用户授权

标签 laravel authentication refactoring

我们有 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/

相关文章:

laravel - CSS 未在 Laravel 5 中加载发送电子邮件

php - mysql查询返回所有数据,即使where不满足LARAVEL

authentication - Flask 登录机制对每个 token 进行身份验证我的调用

c++ - 将 C 源代码转换为 C++

c# - C#重构问题

php - Laravel和Vue-handler.call不是函数

php - laravel中的列增量值

apache - 自定义 apache tomcat 身份验证所需的弹出窗口

没有全局范围的 Laravel 身份验证

php - 重构 20K 行库的技巧