Reputation: 6386
I used the laravel auth that comes default with laravel 5.3. What i'm wondering is how can i add additional information to the session on a successful login? So that I can fetch the information i need with
$user = Auth::user()->someField
Upvotes: 1
Views: 810
Reputation: 40919
I suggest you use the Session facade directly instead of accessing it via the User object.
In order to get what you need, you'll need to hook into Laravel's event system.
First of all, you need to define a listener that will be triggered when user authenticates and will put all necessary data in session:
<?php namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Session;
class AddDataToUserSession
{
public function handle(Login $loginEvent)
{
Session::put('key', 'some value you want to store in session for that user');
// you can access the User object with $loginEvent->user
Session::put('user_email', $loginEvent->user->email);
}
}
Then you need to register this listener so that it's triggered when user logs in. You can do that in your EventServiceProvider class. You'll need to add your listener to the list of listeners triggered on login event:
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\AddDataToUserSession',
],
];
Upvotes: 2