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
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
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
@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.
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);
}
}
@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.