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

mallorca's avatar

Help adding success message after login

I am using the standard login and my route looks like this:

// Authentication routes...
Route::get('login', 'Auth\AuthController@getLogin');
Route::post('login', 'Auth\AuthController@postLogin');
Route::get('logout', 'Auth\AuthController@getLogout');

I have added sweetalert and my success message looks like this:

flash()->success('Success!', 'You logged in');

I identified that the postLogin & getLogin methods are placed in AuthenticatesUsers.php and they look like this:

    protected function handleUserWasAuthenticated(Request $request, $throttles)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }

        if (method_exists($this, 'authenticated')) {
            return $this->authenticated($request, Auth::user());
        }

        return redirect()->intended($this->redirectPath());
    }

How can I add the success message to that code? I tried with:

flash()->success('Success!', 'User successfully created!');
return redirect()->intended($this->redirectPath());

And some other alternatives but no success. It works for all other sections, like creating articles. Appreciate if someone can help!

0 likes
9 replies
veve286's avatar

Use with method to pass the flash data to view.


return redirect()->intended($this->redirectPath())->with('success','It is successMessage');

Then you can simply call like this in view {!! $success !!}
mallorca's avatar

@veve286 Thanks for you reply! Unfortunately, this didn't work for me. I get the following error:

Undefined variable: success 

Also, I suppose that this way of doing it wouldn't give me the message using sweetalert?

karlos545's avatar

@bobeta Please post your helper method "flash function" and the subsequent Flash class that it references

mallorca's avatar

@karlos545 Thanks for your reply. Here you go:

class Flash {
public function create($title, $message, $level, $key = 'flash_message')
    {
      return session()->flash($key, [
        'title'    =>   $title,
        'message'  =>   $message,
        'level'     =>  $level
        ]);
    }

public function success($title, $message)
    {
      return $this->create($title, $message, 'success');
    }

Helper:

function flash($title = null, $message = null)
{
    $flash = app('App\Http\Flash');

    if (func_num_args() == 0) {
        return $flash;
    }

    return $flash->info($title, $message);
}
skliche's avatar
skliche
Best Answer
Level 42

@bobeta No need to alter AuthenticatesUsers.php, just add this method to your AuthController.php.

public function authenticated( \Illuminate\Http\Request $request, \App\User $user ) {
    flash()->success( 'Logged in', "You have been logged in, {$user->name}" );
    return redirect()->intended($this->redirectPath());
}

AuthenticatesUsers.php automatically calls an authenticated() method if it exists and a user was logged in.

If this still does not work, replace flash()->success() with dd() to make sure you actually get there.

3 likes
karlos545's avatar

@bobeta you are using laravel 5 yeah?

have you tried referencing it from the controller

Auth/AuthController.php


class AuthController {

...

 protected function create(array $data)
    {
        $user = User::create([
        //Register user logic here
        ]);

        flash()->success('Success!', 'You logged in');

        return $user;

    }
}
1 like
mallorca's avatar

@skliche Amazing, that great flawlessly without even tweaking it. You've helped me before, great job once again! :)

Please or to participate in this conversation.