0

I have a code default built-in Auth LoginController@login The login function is override also I have a users table. Basically, I'm creating Panels(Admin, Staff) from a single login page. When user_role admin exists then open an admin page otherwise don't log-in the page. same as a staff page.

Mubashir
  • 75
  • 1
  • 3
  • 9
  • Admin exists means `users` table at least one user has an `admin` role? Am I right? – Amit Senjaliya Feb 05 '20 at 13:07
  • 1
    yes, I have entered dummy records for users table. – Mubashir Feb 05 '20 at 13:11
  • What is the field name for the user role? Please show your controller code so I will solve your problem. – Amit Senjaliya Feb 05 '20 at 13:13
  • Here I have LoginController@login – Mubashir Feb 05 '20 at 13:14
  • protected function login() { if (Auth::user()->user_role == 'admin') { return 'index/admin'; } elseif (Auth::user()->user_role == 'staff') { return 'index/staff'; } elseif (Auth::user()->user_role == 'student') { return 'index/student'; } else { return redirect('/home'); } } – Mubashir Feb 05 '20 at 13:14
  • Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('full_name'); $table->string('user_name')->unique(); $table->string('phone'); $table->string('user_role')->nullable(); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); – Mubashir Feb 05 '20 at 13:16

3 Answers3

0

Try this in your login redirect page:

@auth
    @if(\Auth::user()->user_role == 'admin')
      //admin panel
    @elseif(\Auth::user()->user_role == 'staff')
     //staff panel
    @endif
@endauth

You don't need to change anything in your LoginController cause it's already handled by middleware('auth').This will hide components that doesn't belong to the current user and user cannot log if not registered.

user10971804
  • 253
  • 2
  • 8
  • actually, I don't know auth code where we need to paste the auth code? I have created many pages. login-page, student, staff, admin page. – Mubashir Feb 05 '20 at 13:35
  • I thought you are just making a panel for each user role and view their respective data after login. So you have to redirect each user role to their respective page. You have to use the Model approach and use Relationships. You need to have a separate table for each user role and this will make it more complex. My approach was simple cause you only need to add a column in the users table. – user10971804 Feb 05 '20 at 14:06
  • ` Route::middleware(['admin'])->group(function () { Route::get('/admin',function (){ return view('admin'); }); Route::get('/student',function (){ return view('student'); }); Route::get('/staff',function (){ return view('staff'); }); }); ` – Mubashir Feb 05 '20 at 14:16
  • The most Laravel way to approach this is using Model and Relationships method. Try my approach above just add a condition for each user role or you can try Amit's answer but there's a problem in his approach. Other user role can access the other user role's page after login cause there's no more checking of the user role. – user10971804 Feb 05 '20 at 14:30
0

If you are having "trying to get property 'user_role' of non object" on this code

Auth::user()->user_role

It basically means that your user isn't logged in and thus, Auth::user() returns null, try to dd(Auth::user()) to verify this.

If that's the case, you will have to dig deeper on this lead.

LeNiglo
  • 71
  • 8
0

In LoginController.php file

Remove

 protected $redirectTo = '/home';

Add

Illuminate\Support\Facades\Auth; /*Add facade above controller class name*/

public function redirectTo(){

    // User role
    $role = Auth::user()->user_role; 

    // Check user role
    switch ($role) {
        case 'admin':
            return 'index/admin';
        break;
        case 'staff':
            return 'index/staff';
        break; 
        case 'student':
            return 'index/student';
        break; 
        default:
            return '/home'; 
        break;
     }
}
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24