Laravel 5.1 multiple authentication

前端 未结 5 1201
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 14:09

How can you authenticate multiple types of users in Laravel 5.1 e.g. Jobseeker, Recruiter, Admin etc.

Some of you have suggested using a single users table to store

5条回答
  •  渐次进展
    2020-12-06 14:42

    As another solution, i can suggest you to use a polymorphic relation between User and Account, like

    class User extends Eloquent {
    
      ...
    
      public function account() {
        return $this->morphTo();
      }
    
    }
    
    class Account extends Eloquent {
    
      ...
    
      public function user() {
        return $this->morphOne(App\User::class, 'account');
      }
    }
    
    class JobSeeker extends Account { ... }
    
    class Recruiter extends Account { ... }
    

    For different types of Account, you can use route prefixes and different auth controllers, specially for registration who differs for each account instances :

    // Recruiter Authentication routes...
    Route::group(['prefix' => 'recruiter'], function() {
      Route::controller('auth', 'Auth\RecruiterAuthController');
    });
    

    At last, you can access the authenticated account directly from auth()->user()->account. it will return any instance of Account (Recruiter, Admin, ....)

    hope it helps you ;)

提交回复
热议问题