1

I already looking for related issue with this but didn't solved yet.

link 1, link 2, link 3

  • Auth::attempt() is success
  • try to register session after Auth::attempt() is success
  • remember_token field on users table is always null

This is my code:

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...

  • Auth::check() return false
  • $request->session()->has('user') return 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
  • file session are generated
  • app timezone already match with server settings and database settings

Any help would be appreciated.

Community
  • 1
  • 1
arhakim
  • 164
  • 2
  • 16
  • I have same problem now. I doubt if `user` table and its fields - `id`, `username`, `password` are could be the problem. I tried to modify `SessionGuard::user()` function, but it seems session is empty. Session has name, but there is no such file under /storage/framework/sessions. Is there no one to solve this problem? – Miron Aug 27 '16 at 09:12
  • I solved the problem. `user` table and its fields - `id`, `username`, `password` was the problem. Try to change `$primaryKey` and `$table` of `User` model according to your DB. – Miron Aug 27 '16 at 10:40

3 Answers3

0

Why are you adding logged user data manually to the session, You can user use Auth::user() to get user data anytime after attempt

Ravisha Hesh
  • 1,504
  • 13
  • 19
  • It's just temporary to make sure that the session is working in middleware, but it's still no luck even I am trying to using session – arhakim Sep 17 '15 at 15:01
0

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

Alex sof
  • 1
  • 1
  • It's just temporary, just to make sure that the session is worked. But Auth::check() always return false even I remove those script. – arhakim Sep 18 '15 at 23:27
-2

I think I resolve this issue.

Laravel authentication are not working when using Ajax. Just follow the documentation from http://laravel.com/docs/5.1/authentication and all should be worked!

But it's strange! Authentication with ajax are worked well in my localhost but no when I upload it to server.

arhakim
  • 164
  • 2
  • 16