Do you sign the user in? using for instance auth()->attempt(...);? When auth()->user() returns null it usually means the user is not authenticated.
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.
I use Auth::attempt for login and i try dd(Auth::user()) befor redirect i got the user information but other pages got null
@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
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.
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
@ccruz17 Did you fix it ? i also faced the same issue with same versions.
@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();
}
@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.
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 no that is absolutely not the case
@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
@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.
@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 If you use Apache I have a specific fix in mind.
@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.
@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.
@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.