Hesto/multi-auth

How to determine the type of currently logged-in user?

Opened this issue · 3 comments

I'm trying to do something like:

if( Auth::user()->isAdmin ) {
    // some admin stuff...
} else if (Auth::user()->isTeacher ) {
    // some teacher stuff
}

and so on.
Is there a way we can accomplish this?

Since Auth::user() returns back a user model object, it will be nice if you create probably a function in your User Model class (or as it is named). See a sample.

In your case, it might not be User Model, depending on what you named your provider/ multi-auth.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model 
{
    ... // other model fields and method here

    public function isTeacher() {
        $teacher = App\Teacher::where('email', $this->email)->first();

        return is_null($teacher);
    }
}

This is definitely a very good answer! And totally works. It is a very nice way of simplifying the stuff on the controllers' code. Maybe it will be worth adding this method when generating model on php artisan multi-auth:install ?
Anyway, thank you very much :)

You're welcome. I'll do a PR for that feature and hope it gets accepted.