jeudi 18 octobre 2018

Passing modified user defined properties to auth

I am to trying to evaluate a user's role as soon as they login into the system so i can avoid querying the database every time a user's role needs to be evaluated that is to say running auth()->user()->role->role_name.

Here is a look at my User model

<?php

namespace App;

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

Use App\Library\Hash;

class User extends Authenticatable
{
    use Notifiable;

    public $is_admin;

    public $is_employer;

    // Pagination size
    protected $perPage = 10;

    // Table name
    protected $table = 'user';

    // Primary Key
    protected $primaryKey = 'user_id';

    // Add new datetime column for carbon use
    protected $dates = ['last_login', ];

    // User Role Foreign Key
    protected $userRoleForeignKey = 'role_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password', 'salt', 'email_hash', 'active'
    ];

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

    public function checkUserRole() {
        $role = $this->role->role_name;

        if ( $role == 'admin' ) {
            $this->is_admin = true;
        } else if ( $role == 'employer' ) {
            $this->is_employer = true;
        }

        return $this;
    }

    public function role() {
        return $this->belongsTo(
                'App\UserRole',
                $this->userRoleForeignKey
            );
    }

As you can see i have added two properties at the top of my class $is_admin and $is_employer to help me determine a user's role and i have also added the method checkUserRole run every time a user is logging in.

Below is the login method in the SessionsController

    public function login(Request $request) 
    {
        $this->validate($request, [
                'email' => 'required|email',
                'password' => 'required',
            ], [
                'email.required' => 'Email is required',
                'password.required' => 'Password is required',
            ]);

        $user = User::where('email', '=', $request->email)->first();

        if ( !$user || !($user->checkCredentials($request->password)) ) {
            return back()->with([
                    'class' => 'alert-danger',
                    'message' => 'Please check your credentials'
                ]);
        }

        $user = $user->checkUserRole();

        auth()->login($user);

        dd( auth()->user() );

        return redirect('/dashboard');
    }

Below is a screenshot from when i login as an employer and it hits the dd function. enter image description here

As you see from the screenshot, the $is_employer property is set to true.

Problem is when i comment out the dd function in the login and am redirected to the dashboard controlled by the index method of the DashboardController class as shown below

public function index()
{
    dd( auth()->user()->is_employer );
}

I get the output of null from the dd function.

Why would the output be null when it is clearly showing it is true just before redirection?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire