Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 : Do something after user has logged in?

(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:

  • Once a user has logged in, write a value into Session.
  • For example: $request->session()->put('UserAgent', $ClientUserAgent);

LOGOUT:

  • Same thing to do, once a user has logged out, delete the custom Session value.
  • For example: $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?

like image 412
夏期劇場 Avatar asked Apr 08 '16 03:04

夏期劇場


People also ask

What is auth ()- user () in Laravel?

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.

What is Guard () 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.

What is Auth :: Routes () in Laravel?

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.

How does Laravel Auth attempt work?

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.


1 Answers

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 } 
like image 90
kjdion84 Avatar answered Sep 17 '22 21:09

kjdion84