1

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
2
  • Is there a reason you want to do it via the User object returned by Auth::user()? Why not use Session facade directly? Commented Oct 28, 2016 at 4:28
  • how would i do that once a user logs? where would i put that? Commented Oct 28, 2016 at 5:14

1 Answer 1

2

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',
  ],
];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks i did that but nothing is getting stored in the session. the session doesnt even exist. i even added teh provider info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.