laravel – fails to redirect multiple authentication to different pages

I use laravel 5.4 and I want to redirect these three different types of users to different pages

Schema

Types
+-------+-------------+
| id    | name        |
+-------+-------------+
| 1     | Super Admin |
| 2     | Admin       |
| 3     | Cashier     |
+-------+-------------+

Users    
+-------+---------+-------------+
| id    | type_id | name        |
+-------+---------+-------------+
| 1     | 1       | Super Admin |
| 2     | 2       | Admin       |
| 3     | 3       | Cashier     |
+-------+---------+-------------+

LoginController
public function redirectTo()
{
    if (Auth::user()->type_id === 1) {
        return '/superAdmin/home';
    }
    elseif (Auth::user()->type_id === 2) {
        return '/admin/home';
    }
    elseif (Auth::user()->type_id === 3) {
        return '/kasir/home';
    }
}

HomeController
public function superAdmin()
{
    return view('superAdmin.home');
}

public function admin()
{
    return view('admin.home');
}

public function kasir()
{
    return view('kasir.home');
}

Routes
Route::get('/', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b7354565e7854554f495457575e497b505a485249">[email protected]</a>');
Route::get('/admin/home', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f3710121a3c10110b0d1013131a0d3f1e1b121611">[email protected]</a>');
Route::get('/superAdmin/home', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="581037353d1b37362c2a3734343d2a182b2d283d2a193c353136">[email protected]</a>');

I followed the answer of my previous question

redirecting three different user types/roles to different pages

I tried to sign in using a user with type_id = 1, which means the user is a super admin, but it always redirects to Route::get('/','[email protected]'); as well as users with other roles

and the problem is that each role can access the page belongs to another role, what should I do to fix it?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can do one thing which i had been use in my last project using laravel.

You have to write this code in home controller’s index method so while load home-page it going to check user type id and redirect to appropriate page.

 if(Auth::check()) {
        if(Auth::user()->in_usertype_id == 1) {
              return view('admin.dashboard');
        }elseif(Auth::user()->in_usertype_id == 2) {
              return view('admin.dashboard1');
       else{
             return view('admin.dashboard3');
       }
 }else{
             return redirect('login')->with('error', 
             Lang::get('message.unauthorize-access'));
}

Hope this will helps you.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x