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

alexeightsix's avatar

How do I set a flash message on a successful login?

Hey,

I'm using Laravel 5.4 with the pre-built auth library.

I would like to have a sweet alert (or popup) come up when a user sucessfully logins but I can't find where the logic is where the redirect to happens.

So:

  • User logins successfully
  • Gets forwarded to home controller (need to find this in the code and attach a flash message or something else?)
  • In my view detect if there's a flash message and make a if there's a flash message in memory so I can then query it with Javascript and show an alert box.

Edit:

I already have this set and it's fine:

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/';

but I need to find where this is referenced? and add a flash message

Thanks

0 likes
12 replies
SaeedPrez's avatar

Add this to your LoginController.php and put the code there..

   /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        //
    }

Laravel will call this method upon successful authentication.

alexeightsix's avatar

Awesome thanks! I added the code above and now see this tough:

ErrorException in LoginController.php line 40: Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request, instance of Illuminate\Http\Request given, called in /home/vagrant/Projects/Loader/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php on line 102 and defined

alexeightsix's avatar

I added this line:

use App\Http\Controllers\Auth\Request;

but now I see this error:

Sorry, the page you are looking for could not be found.

1/1 NotFoundHttpException in RouteCollection.php line 161:

cviv's avatar

how about this?

use Illuminate\Foundation\Auth\AuthenticatesUsers;
alexeightsix's avatar

@SaeedPrez

Same issue, here's my full code:

https://gist.github.com/alexeightsix/f6837067631a192dabe41f15c19fe5d4

Here's the message:

ErrorException in LoginController.php line 42: Argument 2 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\User, instance of App\User given, called in /home/vagrant/Projects/Loader/vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php on line 102 and defined

cviv's avatar

try

use App\User;
 //since you included it
public function authenticated(Request $request, User $user) {

        dd($request);

}
1 like
SaeedPrez's avatar
Level 50

@alexeightsix I'm not sure why you changed the code I gave you?,.. Anyways, either change User $user to just $user as the provided code, or add use App\User; to the top like @cviv suggested.

I would suggest you remove it though, since there is really no need for it since Laravel will automatically pass in the authenticated user.

1 like
wirli's avatar

I tried the same, but the function authenticated seems to be ignored: no errors, no dd() output. Any ideas why?

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }
    
    
    protected function authenticated(Request $request, $user)
    {
        dd($request);
    }
}

SaeedPrez's avatar

@wirli try not to revive topics which have best answer because everyone will think it is already solved.. post your question in a new discussion and people will try to help you.

Please or to participate in this conversation.