简体   繁体   English

Laravel 5.1:Auth :: attemp()成功,但是Auth :: check()返回false

[英]Laravel 5.1: Auth::attemp() success but Auth::check() return false

I already looking for related issue with this but didn't solved yet. 我已经在寻找与此相关的问题,但尚未解决。

link 1 , link 2 , link 3 链接1链接2链接3

  • Auth::attempt() is success Auth :: attempt()成功
  • try to register session after Auth::attempt() is success 尝试在Auth :: attempt()成功之后注册会话
  • remember_token field on users table is always null users表上的Remember_token字段始终为null

This is my code: 这是我的代码:

AuthController: AuthController:

protected function login(Request $request){
    $result = [];
    $rules = ['email' => 'required|email', 'password' => 'required|alphaNum|min:3'];
    $validator = Validator::make($request->all(), $rules);

    if($validator->fails()) {
        $result['message'] = 'Login Failed';

    }else{
        $userdata = ['email' => $request->email, 'password' => $request->password];
        if (Auth::attempt($userdata, $request->has('auth_remember'))) {
            // dd(Auth::user()); << THIS DATA IS EXIST
            // $request->session()->push('user.id', Auth::user()->id);
            // $request->session()->push('user.name', Auth::user()->name);
            // $request->session()->push('user.email', Auth::user()->email);

            // dd($request->session()->all()); << THIS DATA IS EXIST
            $result['message'] = 'Login successfull. Redirecting ...';
        }else{
            $result['message'] = 'User not found';
        }
    }
    echo json_encode($result);
}

I have a middleware Auth when I go to http://.../dashboard , but... 我去http://.../dashboard时有中间件Auth,但是...

  • Auth::check() return false Auth :: check()返回false
  • $request->session()->has('user') return false $ request-> session()-> has('user')返回false

Auth Middleware: 身份验证中间件:

    public function handle($request, Closure $next){
      if($this->auth->viaRemember()) return $next($request);

      if($this->auth->guest()){
        if($request->ajax()){
            return response('Unauthorized.', 401);
        }else{
            return redirect()->guest('login');
        }
      }

      return $next($request);
  }
  • storage/framework/session already set to 777 存储/框架/会话已设置为777
  • file session are generated 文件会话已生成
  • app timezone already match with server settings and database settings 应用时区已与服务器设置和数据库设置匹配

Any help would be appreciated. 任何帮助,将不胜感激。

为什么要手动将记录的用户数据添加到会话中,您可以在尝试后随时使用Auth::user()获取用户数据

Delete this lines: 删除以下行:

$request->session()->push('user.id', Auth::user()->id);
$request->session()->push('user.name', Auth::user()->name);
$request->session()->push('user.email', Auth::user()->email);

and verify with dd($request) the auth_remember 并用dd($ request)验证auth_remember

I think I resolve this issue. 我想我解决了这个问题。

Laravel authentication are not working when using Ajax. 使用Ajax时Laravel身份验证不起作用。 Just follow the documentation from http://laravel.com/docs/5.1/authentication and all should be worked! 只需按照http://laravel.com/docs/5.1/authentication中的文档进行,一切都应该起作用!

But it's strange! 但这很奇怪! Authentication with ajax are worked well in my localhost but no when I upload it to server. 在我的本地主机中,使用ajax进行身份验证的效果很好,但是当我将其上传到服务器时却无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM