Laravel-permission: Redirect Page Help

Created on 29 Dec 2018  路  4Comments  路  Source: spatie/laravel-permission

I am having problem with 5.7 authentication,

super-admin, admin, members have the same login and after logged in it redirect to different routes.
The super-admin and admin have the same redirect. So I put this code.

app\Http\Controllers\Auth\LoginController.php

protected function authenticated(Request $request, $user)
{
    if ( $user->hasAnyRole(['super-admin', 'admin']) ) {// do your margic here
        return redirect()->route('admin.dashboard');
    }

     return redirect('/home');
}

and then this is my routes/web.php

Auth::routes();
Route::group(['middleware' => ['role:member']], function () {
    Route::get('/', 'HomeController@index')->name('home');
});
Route::group(['middleware' => ['role:super-admin|admin'], 'prefix' => 'admin'], function () {
    Route::get('/', 'Admin\HomeController@dashboard')->name('admin.dashboard');
});

After login, what I want to do is when a super-admin/admin visit the site.com/* it should redirect to site.com/admin/ cause he is not authorize and also when visiting member visit the site.com/admin/*, he redirect to site.com/, the rest will go to login page when not authenticated.

It displays like this, I don't know where to add the redirect code
image

It should redirect based on their role homepage instead display 403 error.

support

Most helpful comment

I've just recreated your app, based on what you posted here, and it works fine, even with the authenticated() instead of redirectTo().

  • Login as Member = redirects to /
  • Login as Admin = redirects to /admin
    (There are other cases not covered here, such as what to do when the user from the wrong role is logged in and tries to go to the unauthorized route, but that's beyond the scope of what you posted about here.)

Here's the entire code, with commits split out for reference: https://github.com/drbyte/permtesting-jpunx/commits/master

Hopefully it'll help you find what else is interfering from within your app.

All 4 comments

Instead of overriding authenticated, shouldn't you be doing it with redirectTo()?

Something like:

    /**
     * determine the redirect URL after login
     *
     * @param  \Illuminate\Http\Request $request
     * @return string
     */
    protected function redirectTo(Request $request)
    {
        if ( $request->user()->hasAnyRole(['super-admin', 'admin']) ) {
            return route('admin.dashboard');
        }

        return '/home';
    }

@jeraldpunx
besides the route naming and redirection url dont match:

return redirect('/home');

and

Route::group(['middleware' => ['role:member']], function () {
    Route::get('/', 'HomeController@index')->name('home');
});

What's the difference between these two though? It seems still go the same result though.
image

I've just recreated your app, based on what you posted here, and it works fine, even with the authenticated() instead of redirectTo().

  • Login as Member = redirects to /
  • Login as Admin = redirects to /admin
    (There are other cases not covered here, such as what to do when the user from the wrong role is logged in and tries to go to the unauthorized route, but that's beyond the scope of what you posted about here.)

Here's the entire code, with commits split out for reference: https://github.com/drbyte/permtesting-jpunx/commits/master

Hopefully it'll help you find what else is interfering from within your app.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tripex picture tripex  路  3Comments

NattananWs picture NattananWs  路  3Comments

bbdangar picture bbdangar  路  4Comments

notflip picture notflip  路  3Comments

enghelewa picture enghelewa  路  4Comments