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

linktoahref's avatar

Laravel not creating authenticated session for user

I am using LARAVEL 5.2 I am doing user authentication using a custom table. I am able to login successfully as when I do in AuthController.php

public function authenticated(Request $request, $user)
{
    dd($user);
}

I am getting the user details.

But when I access some other route and in the respective controller when I do

  • dd(Auth::user()) returns null
  • dd(session()->all()) returns _token
  • dd(Auth::check()) returns false

What am I doing wrong? How could I create user session? Any help appreciated! Thanks!

0 likes
5 replies
WebKenth's avatar

When you say custom table, what do you mean?

show us how you handle your user login and we can pinpoint where you need to start a session

linktoahref's avatar

I am using public_users table for authentication and public_email , public_password fields for doing the authentication so I have changed the App\User.php file as follows:

class User extends Authenticatable
{
    protected $table = 'public_users';
    protected $primaryKey = 'public_users_id';
    protected $fillable = [
        'public_email',
        'public_password'
    ];
    protected $hidden = [
        'public_password'
    ];

    // Override required, Otherwise existing Authentication system will not match credentials
    public function getAuthPassword()
    {
        return $this->public_password;
    }
}

and in the AuthController.php file I have added the below code

    public function loginUsername()
    {
        return property_exists($this, 'username') ? $this->username : 'public_email';
    }

    public function authenticated(Request $request, $user)
    {
        dd($user);
    }

and this would be in config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  => App\User::class,
        ]
    ]
1 like
linktoahref's avatar

So what could be the resolution to the problem? In Manually Authenticating Users it states an authenticated session will be started for the user if the attempt method is successful. In vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
            return $this->handleUserWasAuthenticated($request, $throttles);
        }

shouldn't this start an authenticated session if the attempt is successful? as I am able to get inside the If clause.

Plus when I do dd(session()->all()) it generates a new token on every refresh. Shouldn't token remain same for a session?

linktoahref's avatar

Solved!

Just commented out the following code from AuthController.php

/*
public function authenticated(Request $request, $user)
{
    dd($user);
}
*/

Please or to participate in this conversation.