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
That property is referenced in a trait
RedirectsUsers
in the methodredirectPath()
. That trait is used in the traitAuthenticatesUsers
which is being used in theLoginController
.With that said, what you need to do is fairly simple. In the login controller, we need to override the method in the trait so that it also flashes your message to session. But because that method is in a trait, we don't just want to override it, we still want to use the method in the trait so we first need to alias it as something else.
Now we can safely override this method to flash data to session.
If you don't wish to go through this, another thing you can do is listen for the
auth.login
event and in your handler, flash the data there.If you want to have user sign in with ajax and have a flash message on successfully login and then reload/redirect, you will have to make some changes to the
Auth\Login Controller
Add this two methods to
Auth\Login Controller
, this first one will send response on successful login, while the second one will produce error message.Then with jQuery (if you are using it), you can do this
And of course, if you wish to set custom error/flash message on server you can add it to response json.
Hope it helps.
app/Http/Controllers/Auth/LoginController.php
to reference the HomeController route and flash your message:use Session;
Then, in your view add the following:
@if (session()->has('flash_notification.success')) <div class="alert alert-success">{!! session('flash_notification.success') !!}</div> @endif
Finally, make sure you include bootstrap for the css
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">