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

nutkani1337's avatar

How to get redirected to a welcome page with a passed username prop using Inertia.js in Laravel 9?

I'm working on an Application with Laravel, vue.js (Inertia). There is a login page when the user enter the credentials and if he is authenticated then he should be redirected to the "Welcome.vue" and i want to pass username with it.

I have a login component that is accessible at "http://127.0.0.1:8000/Login" when the user fills out the form and hit the "login" now button following controller code is executed (which is working fine).

    public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    if (Auth::attempt($credentials)) {
        $username =Auth::user();
        return Inertia('Welcome', [
            'username' => $username
          ]);
    } else {
        return back()->with('message','Authenticaiton Failed');
    }
}

Now I can receive it as a propos. But the problem with this method is that the page URL remain the same which is "http://127.0.0.1:8000/Login" I want to change it to "http://127.0.0.1:8000/Welcome". I can use the return redirect('Welcome')->with('username',$username); but I can't receive it as a prop. I don't know what's the exact issue.

Following is the additional code that you might need:

Route::get('/Welcome', function () {
    return inertia('Welcome');
    
})->name('Welcome')->middleware('auth', 'AuthenticateCustom');

Route::get('/Login', function () { return inertia('Login'); })->name('Login');
Route::post('/Login', [UserController::class, 'login'])->name('login');`
0 likes
2 replies
franklinIwobi's avatar

You can pass the prop by default in the \App\Http\Middleware\HandleInertiaRequests.php class. Like so..

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
         'auth' => [
            'user' => $request->user(),
        ],            
    ]);
} 

Then accept the prop and access the value with auth.user.username

or

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
         'user' => [
             'username' => Auth::user()->username ,
         ],
      
    ]);
}

Then access it with user.username.

Hope this helps.

1 like

Please or to participate in this conversation.