zoukankan      html  css  js  c++  java
  • Laravel 单设备登录

    https://laravel-china.org/articles/10605/laravel-single-device-login

    前几天在 laracasts 看了laravel5.6的新功能视频 logoutOtherDevices 用于使在其他设备上处于活动状态的用户会话无效并“注销”,而不会使其当前设备上的会话无效。,今天抽空把它应用到了系统里,在这里记录下吧。
    在使用此功能前需要先把 app/Http/Kernel.php web 中间件中的 IlluminateSessionMiddlewareAuthenticateSession::class 注释取消掉。

    LoginController 控制器中继承 IlluminateFoundationAuthAuthenticatesUsers 类中的 authenticated 方法。

    protected function authenticated(Request $request, $user)
    {
        $this->guard()->logoutOtherDevices($request->password);
        return response()->json(['message' => '登录成功']);
    }

    logoutOtherDevices 接受两个参数,第一个参数是来自表单提交的数据,然后经过加密保存到第二个参数指定的字段里,在经过中间件储存 session

    public function logoutOtherDevices($password, $attribute = 'password')
    {
        if (! $this->user()) {
            return;
        }
    
        return tap($this->user()->forceFill([
            $attribute => Hash::make($password),
        ]))->save();
    }
    }
     

    这样在用户登录成功后,即会注销其他设备上的会话而实现单设备登陆。

  • 相关阅读:
    数组、向量、矩阵的区别
    vue-cli3没有config.js文件的解决方法
    通用JS六
    通用JS五
    通用JS四
    通用JS三
    vue中sort排序与revers数据反序
    通用JS二
    VueX存储与本地存储以及会话存储的区别
    通用JS(一)
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9597937.html
Copyright © 2011-2022 走看看