AdRock
4 months ago
195
7
Laravel

Add custom attribute to Auth:user() during login

Posted 4 months ago by AdRock

I know this has been asked before but it doesn't help in my situation.

I have a similar problem to this https://laracasts.com/discuss/channels/laravel/add-a-new-attribute-in-authuser-session-in-laravel-53

I need to add some attributes to my User object when the user logs in.

I've tried using $user->setAttribute() but it doesn't work

Here is my LoginController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use \DB;
// Models
use App\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
     public function __construct()
     {
         $this->middleware('guest')->except('logout');
     }

     public function login(Request $request)
     {
         // Check validation
         $this->validate($request, [
             'username' => 'required',
             'password' => 'required'
         ]);

         $xyz = \Config::get('constants.salt1');

         $user = User::whereRaw('password = AES_ENCRYPT("' . $request->input('password') . '", "' . $xyz . '")')
            ->where('username', $username)
            ->where('isActive', 1)
            ->first();

        
        $user->setAttribute('orgId', $organisation->id);
        // $user->orgId = $organisation->id;
        
        // Set Auth Details
        \Auth::login($user);

        // Redirect home page
        return redirect()->route('reports.index');
    }
}

and my User model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $appends = ['orgId'];

    protected $connection = 'tenant';

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'people';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [''];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];

    public function getOrgIdAttribute($value)
    {
        return $value;
    }
}

Any idea how do i get this working. It seems pointless creating a session like in the post i referenced and the values aren't coming from another model?

Please sign in or create an account to participate in this conversation.