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

galih56's avatar

Auth::user() returns null value

0

I'm working on username authentication. I thought my LoginController is working perfectly.But when i go to the page and do login.The page will be stay on login page.I checked my code,when i use dd(Auth::user()) in my HomeController. it always return null value.

i read some discussion on stackoverflow.People said i have to use this in the loginController's constructor.

$this->middleare(function($request,$next){
    $this->user=Auth::user();
    return $next($request)
})

but it doesn't work in my project.

I'm sure the only reason why my login page cannot redirect to the next page is because the Auth::user() have null value.But i don't know how to fix that.

can someone help me??

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\User;

use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Auth\Validator;

class LoginController extends Controller
{


    use AuthenticatesUsers;


    protected $redirectTo = '/index';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function authenticated(Request $request)
    {
        if (Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {

            return redirect('/');
        }
    }
    public function username()
    {
        return 'username';
    }
    protected function validateLogin(Request $request)
    {
        /*$this->validate($request, [
            "user_name" => 'required', 'user_password' => 'required',
        ]);*/
        $messages = [
            'username.required' => 'Username harus diisi',
            'username.unique' => 'Username sudah ada',
            'password.required' => 'Tolong isi password terlebih dahulu',
            'password.min' => 'Password harus diisi minimal 6 karakter.'
        ];
        return $this->validate($request, [
            'username' => ['required', 'string', 'max:150'],
            'password' => ['required', 'string', 'min:6'],
        ], $messages);
    }
}

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{

    public function __construct()
    {
        dd(Auth::user()); //this return false
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}
0 likes
11 replies
Nakov's avatar

In your HomeController constructor you can place what you have read on StackOverflow. So you first should register the middleware and then use a callback to get the user from the session, so your HomeController constructor should look like this:

public function __construct()
{
    $this->middleware('auth');

    $this->middleare(function($request,$next){
        $this->user=Auth::user(); // here the user should exist from the session
        return $next($request)
    });
}

munazzil's avatar

In your middleware your missing variable assign for user change as like below your assigned just user not $user,

    $this->middleare(function($request,$next){
    $this->$user=Auth::user(); //here
    return $next($request)
    })
galih56's avatar

@NAKOV - i don't get the part "here the user should exist from the session" How to pass Auth::user() to the session? is that not an object??

Nakov's avatar

@GALIH56 - It is an object, but it won't be null is what I was saying.

munazzil's avatar

Simple thing your 'middleware' type error,

    $this->middleware(function($request,$next){ //here
    $this->user=Auth::user();
    return $next($request)
    })

else just remove the $this because it can't be called,

    $this->middleware(function($request,$next){ 
    $user=Auth::user(); //here
    return $next($request)
    })
jlrdw's avatar

Did you change email to username at all required places. Including adding a username field in the database.

I also use "username", mine is working. Laravel 5.8

But as @wilk_randall stated, that's probably the problem.

galih56's avatar

@WILK_RANDALL - i tried to access the Auth session not just in the constructor,i also tried to access the session through index() in the HomeController but it return null value.

Fortunately i found a discussion about the same issue.The Auth::atempt works successfully but it return null in index().it make middleware block the controller.and then i found the problem was from the database.I changed the 'id' dolumn from bigincrement to string via migration.It make Auth can't read the table properly when i do login.So i decided to change it back from varchar to integer.And then the loginController works correctly

1 like
jlrdw's avatar

I changed the 'id' dolumn from bigincrement to string via migration.

That would have been nice to know at first. But we are all Human.

1 like
galih56's avatar

@JLRDW - I didn't know that was the problem,usually if i make website with native php.datatype of user's id must be not a big deal.I'm still wondering why should we care about datatype of id in authentication

Please or to participate in this conversation.