Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

grantchesney's avatar

Laravel 5.3 session not persistent

Hi all, for the past couple hours i have been trying to get a laravel app to save the session properly. The problem is: When i call "Auth::login($user);" then right after in the same method i call "return Auth::user();" it works however, when i go to a new route and call "return Auth::user();" it returns null. Here is some of the code:

Handle callback method:

  public function handleProviderCallback() {

        $returnedUser = Socialite::driver('google')->user();

        $user = App\User::find($returnedUser->id);

        if (is_null($user)) {

           $user = new App\User();
           $user->id = $returnedUser->id;
           $user->email = $returnedUser->email;
           $user->confirmed = false;
           $user->role = 'unassigned';

       }


       $user->avatar = $returnedUser->avatar;
       $user->save();

       Auth::login($user, true);


       return Auth::user();

       return redirect(route('home'));
    }

User

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'email', 'role', 'avatar'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * Dates which can be converted into Carbon objects
     * @var array
     */
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

}

Any help would be great!

Thanks, Grant

0 likes
8 replies
jlrdw's avatar

Have you checked GitHub to see if an issue exists on this?

jlrdw's avatar

Search this forum for session not persisting I have seen other post on it possibly an answer there somewhere.

ejdelmonico's avatar

I have a few off topic questions...first, why is "id" fillable? Are you using another field for a unique id? second, why are you returning 2x in the handleProviderCallback function instead of something like view('home', compact['user'])?

grantchesney's avatar

The id is unique to your google account therefore it is unique for each user. Second the function is only to store the user information and verify the login.

jamalnasir's avatar

change kernel.php 'api'

api' => [ 'throttle:60,1', 'bindings', ], to

'api' => [ 'throttle:60,1', 'bindings', \App\Http\Middleware\EncryptCookies::class, \Illuminate\Session\Middleware\StartSession::class, ]

Please or to participate in this conversation.