Yep, I managed to figure out the problem and it's the silliest thing.
The url cannot have https:// nor the port if it has. In my case, since I'm running localhost, it looked like this:
APP_URL=127.0.0.1
SESSION_DOMAIN=127.0.0.1
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've tried logging in several ways, but it doesn't work. The way I found it easier, which performs the login is Auth::login($user);, but it doesn't keep the user logged in after the redirect.
I think it's some configuration missing, I'll put the files here.
I had seen here in laracast that the problem was the information in the .env related to SESSION_DOMAIN. But I tested a few things and it didn't work, I'll leave info on that as well.
About SESSION_DOMAIN.
I'm running on localhost and laravel creates this url: http://127.0.0.1:8000
Currently in my .env it looks like this:
APP_URL=127.0.0.1:8000
SESSION_DOMAIN=127.0.0.1:8000
This part of the code is what logs in. It's the second step. The user enters the email, then it is validated and sends a code. When he types the code, he comes to this function to validate the code and proceed with the login. Login controller:
public function validateCode(Request $request)
{
$email = $request->email;
// Get user
$user = User::where('email', $email)->first();
if ($user->count() > 0) {
$validateCode = $request->input('validateCode');
// Check the codes
if ($validateCode == $user['codConfirm']) {
// Auth::login($user, true);
$credentials = [
'email' => $user['email'],
'password' => $user['password'],
];
// dd(Auth::login($user), Auth::attempt($credentials), Auth::guard('web')->attempt($credentials), $credentials, $request->only('email', 'password'));
// dd(Auth::attempt((array)$user));
// Auth::guard('web')->login($user, true);
Auth::loginUsingId($user->id, true);
if(Auth::check())
return redirect()->route('home');
else{
dd('else');
return redirect()->action('Auth\LoginController@index', ['validator' => ['Não foi possível fazer o login']]);
}
} else {
return view('confirmar-usuario', ['email' => $email]);
}
}
else {
flash('Usuário inválido')->error();
return view('login');
}
}
Authenticate:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Auth;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
// dd($request, Auth::check(), Auth::user());
if (!$request->expectsJson()) {
return route('login');
}
}
}
RedirectIfAuthenticated
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use App\User;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
}
Auth
<?php
return [
/*
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
I think that's all guys, if I'm missing something, just comment. I really need help I've been trying to solve this for days
Please or to participate in this conversation.