2

I have 2 tables for users Users and BackendUsers - One for frontend users (regular users / customers) and one for backend users (admin). Take note that these 2 tables have different structures don't tell me to combine them and use ONE table.

I have two solutions in mind:

  1. Is to duplicate / extend the Authentication to separate the Authentication for backend as seen here: Laravel 5 Implement multiple Auth drivers
  2. Is to create one more table lets say UserPivot. Wherein there's a column for user_id (integer), and user_type (enum('frontend', 'backend')).

My question is what is the best way to implement this? Do you have better solution?

I want the idea of number 1 since it separates the Authentication for the backend users and it separates the session as well.

Community
  • 1
  • 1
aceraven777
  • 4,358
  • 3
  • 31
  • 55

2 Answers2

0

I am working with an auth's infrastructure like this, where clients and admins need to login with the same authentication form. Client and Admin have each's table. The trick here is:

  1. Create a join table between users and admin where a discriminator be used to identify the roll (exactly as @Jake-Opena said)
  2. Extends Auth service provider in order to implement the way you can validate your credentials based in role:

    // returns an user or admin based on a role and id
    retrieveById($identifier); 
    
    // returns an user or admin based on a role and its credentials 
    retrieveByCredentials(array $credentials)
    
    // returns an user or admin based on a role and its token
    retrieveByToken($identifier, $token)
    
    // validate the credentials agains the table data based in a role
    validateCredentials(Authenticatable $user, array $credentials)
    

Another way (ugly option) is, if you want to avoid to create a join table, is to create a new form's input where the user chooses which role be used to authenticate its credentials.

manix
  • 14,537
  • 11
  • 70
  • 107
0

I found a package that solves this problem. I used https://github.com/Kbwebs/MultiAuth.

aceraven777
  • 4,358
  • 3
  • 31
  • 55