sudyodcup's avatar

Auth::user() return null

I need to help, This is the first time to build my web app with Laravel and Livewire. Now i'm struck after login and i need to get user information after login and redirect to other pages. Auth::user() alway return as null, Before login Auth::user() has value but after redirect is null value.

0 likes
17 replies
dennisprudlo's avatar

Do you sign the user in? using for instance auth()->attempt(...);? When auth()->user() returns null it usually means the user is not authenticated.

sudyodcup's avatar

I use Auth::attempt for login and i try dd(Auth::user()) befor redirect i got the user information but other pages got null

SilenceBringer's avatar

@sudyodcup possible you have some middleware that logout user?

you can make test route (at the top of your routes file)

Route::get('/authcheck', function () {
    dd(auth()->user());
});

and redirect to this route after login to see if user still will be logged in

sudyodcup's avatar

I set redirect to that route it show null also.

Login Controller

if(Auth::attempt(array('email' => $this->email, 'password' => $this->password))){ 
                session()->flash('fn-create');
                return redirect()->route('authcheck');
        }

Route web

Route::get('/authcheck', function () {
    dd(auth()->user());
})->name('authcheck');

Route::get('/', Dashboard::class)->name('dashboard');

Route::get('/register', RegisterationUser::class)->name('register');
Route::get('/login', Login::class)->name('login');
Route::get('/logout', [Logout::class,'logout'])->name('logout');

i try to add ->middleware('auth') after dashboard but it can't redirect after login successful.

ccruz17's avatar

I have the same problem, with Laravel 8 and Livewire 2. For some reason the user or the session is lost after doing the redirect.

Did you manage to fix it? @sudyodcup

aghorbanmehr's avatar

@ccruz17 I found the fix you must mount the parameters that you need In your Livewire Component!

public function mount()
    {
        $this->user_id = auth()->id();
    }
pinsard's avatar

@aghorbanmehr I'm not sure if you understood the problem. When you suggest to access auth()->id() from the component, after the redirect(), you're implying that the auth() object wasn't changed in the process. This is the problem

// In the Login Component, whatever method that's used to execute the login
if (auth()->attempt( $credentials )) { 
  return redirect( route( 'dashboard' ) );
}

// In the destination Component, render() method
dd( auth()->user() );

the result of dd() is null, be it for the id() or user().

Anyways, I came here by chance because I too, am facing the problem. I'm sure it is one of those little details that will probably be found only by looking at the source code. I'm going to get a look at it and will report back.

pinsard's avatar

Well... as said, I went look at the code and noticed the attempt() call needs to include the "Remember Me" option if we need to retrieve auth()->user() later on the flow.

Setting that, all done.

pinsard's avatar

@Snapey it worked for me right away. What would be the case, then?

Maybe some info is in order:

Laravel: 10.44.0
php: 8.3.3
Composer: 2.7.1
Debug Mode: ENABLED
Config cache: NOT CACHED
Events cache: NOT CACHED
Routes cache: NOT CACHED
Views: CACHED
Session: file
Livewire: v3.4.4
Herd: 1.4.1 build 21
Snapey's avatar

@pinsard The problem is indicative of not being able to save the session cookie on the client.

Your 'solution' of using remember-me just means that the user is re-logged in on every request with a new session. This means you can never save any data in session.

You should check for missing session cookie and then investigate reasons.

pinsard's avatar

@Snapey hmmmm. THAT'S some really good advice. I foresee a very fun weekend ahead hahaha. In two days it will be my marriage anniversary but I suspect I will forget about it and prefer to dive deep in what is really fun: code inspection and forensics hahahaha. With some luck, I can even provoke a divorce HAHAHAHAHA

pinsard's avatar

@Snapey You rock. This kind of guidance is much better than simply throwing away a ready answer. I learned to pay attention to Herd's option to secure a site, also to .env APP_URL and the SESSION_SECURE_COOKIE options.

BTW, I use both Safari and FF, and now I know why sometimes it worked, sometimes it didn't.

Bad side of it is that I won't need to spend the whole weekend reading and watching tutorials on YT, and probably won't get divorced -- although I'm pretty confident I will, eventually, face a harder challenge.

Thanks a lot.

pinsard's avatar

@Snapey I saw your reply about Apache after I sent my previous message. No, I don't use it. I prefer to have a harder time configuring lighttpd than using Apache. When in production, though, many projects will use Apache, sure, but I leave this to the devops team.

HOWEVER, it is always advisable to be aware of possible solutions. If you don't mind sharing your suggestion for Apache, I would love to know about that. I keep an Obsidian workbook always at hand, just in case.

Snapey's avatar

@pinsard Sure

There is an issue with Apache in that if any characters are sent to the client before the headers (and cookies) then the cookies are discarded by the browser.

This can happen with something as apparently innocent as a stray character before the opening <?php in a file, or if using ?> to close the php and then having trailing characters. This can be a problem in any file that gets executed in the request, not just views.

https://stackoverflow.com/questions/71615199/laravel-session-cookie-not-being-set

Please or to participate in this conversation.