(I'm a beginner of Laravel)
I'm using Laravel 5.2
. I have successfully enabled the Authentication; by doing the php artisan make:auth
and stuffs.
So my login is working.
Now i need to do something once someone has logged in. For an simple example:
LOGIN:
$request->session()->put('UserAgent', $ClientUserAgent);
LOGOUT:
$request->session()->forget('UserAgent');
I'm not sure whether there are (things like) hooks
or Event Listeners
, Event Handlers
, or something like that.
How can i do it please?
Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns true if a user is logged-in and false if a user is not. Check here for more about how Facades work in Laravel.
Guards define how users are authenticated for each request. For example, Laravel ships with a session guard which maintains state using session storage and cookies. Providers define how users are retrieved from your persistent storage.
Auth::routes() is just a helper class that helps you generate all the routes required for user authentication. You can browse the code here https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/Router.php instead.
The attempt method accepts an array of key / value pairs as its first argument. The password value will be hashed. The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column.
For newer versions of Laravel
If you are only doing something very simple then creating an event handler seems overkill to me. Laravel has an empty method included in the AuthenticatesUsers
class for this purpose.
Just place the following method inside app\Http\Controllers\LoginController
(overriding it):
protected function authenticated(Request $request, $user) { // stuff to do after user logs in }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With