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

laurijirilla's avatar

Auth login in Controller laravel 9

I am trying to login my user from a controller, and then redirect it to the necessary page.

My problem is that from my controller I can log in correctly, but when doing the redirect it doesn't preserve the session and I can't access the user information.

My Controller

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

class AuthUniqueController extends Controller
{
    public function unique_login($user_id, $module)
    {
        $user = User::find($user_id);
        Auth::guard('web')->login($user);
        //dd(Auth::user()->id);
        
        return redirect()->route($module);
    }
}

(Auth::user()->id print correct user id);

My route:

Route::get('/requests', [MyController::class, 'my_function'])->name('requests');

And my_function in MyController:

public function my_function ()
    {
		dd(Auth::user()->id);
        $user = Auth::user();
		
        //return view('list_requests')->with('user_id',  $user->id);
    }

Here Auth::user()->id is null;

Please any ideas?

0 likes
3 replies
laurijirilla's avatar

Hi @hareshc . Thanks for your answer. I don't have a login form because this is done by an api, that's why I create my auth->login($user) manually. When calling the view from my controller after auth->login($user) the middleware doesn't recognize the created session so it doesn't allow me to continue.

Please or to participate in this conversation.