Laravel dd()函数注销用户

时间:2019-03-02 01:35:28

标签: php laravel

我创建了一个自定义登录名和自定义中间件, 当我尝试在任何地方终止转储时,都会注销用户,这似乎是问题所在?

登录控制器: 公共功能登录(请求$ request){

// flash::success('Succesfully login')->important();

$client = new Client();
  try {
    $res = $client->request('POST', 'http://api.fstbx.com/api/user/login', [
      'headers' => [
             'Accept' => 'application/json',
             'Client-Key' => 'p947KVCgE7PyXLdZpfqOSIg4OwIla2BWdSPzdoqf'
         ],
      'form_params' => [
        'username' => $request->get('username'),
        'password' => $request->get('password')
      ]
    ]);
  } catch (\Exception $e) {
    Flash::error('Invalid login credentials.');
    return redirect('/login');
  }



  $info = json_decode((string) $res->getBody(), true);      
  $request->session()->put('authUser',$info['user']);
  $request->session()->put('authToken',$info['access_token']);
  $request->session()->put('authRole',['1','2']);

  $role = [];
  $role = ['1','2'];

  $user =  User::createAuth($info['user'],$info['access_token'],$role);

  return redirect('/');

}

自定义中间件

公共函数句柄($ request,闭包$ next)     {         if(!empty(session('authUser'))){

        // $user = $request->session()->get('authUser');
        $user = session('authUser');

        // $token = $request->session()->get('authToken');
        $token = session('authToken');
        // $role = $request->session()->get('authRole');
        $role = session('authRole');

        User::createAuth($user,$token,$role);
        return $next($request);

    }
    return redirect('/login');

}

用户模型

公共静态函数createAuth($ userData,$ userToken,$ userRole)     {         $ user = new User();

    $user->name = $userData['name'];
    $user->email = $userData['email'];
    $user->avatar = array_rand(User::get_avatar());
    $user->token = $userToken;
    $user->roles = $userRole;

    Auth::login($user);

    return $user;
}

1 个答案:

答案 0 :(得分:0)

将此代码添加到我的自定义中间件上解决了我的问题 $ request-> session()-> regenerate();