I have Laravel 5.3 with three different types of users. I want them to be redirected to different dashboard pages after logging in. For example:
user -> login -> user-dashboard
admin -> login -> admin-dashboard
I have created a middleware called CheckRole
:
public function handle($request, Closure $next)
{
if($request->user() === null) {
return response("Insufficient Permissions" , 401);
}
$actions = $request->route()->getAction();
$roles = isset($actions['roles']) ? $actions['roles'] : null;
if($request->user()->hasAnyRole($roles) || !$roles) {
return $next($request);
}
return response("Insufficient Permissions" , 401);
}
Routes
Route::group(['middleware' => ['auth','roles'], 'roles' => 'Admin'], function () {
// Routes here
}
Roles are working perfectly.
Now redirectTo= '';
in the LoginContoller
points to one view only. I have checked the documentation and I believe this has something to do with guards which have no explanation on how to set it up.
I have also seen multiauth, but I do not think it is wise to create different tables for different users and hence looking for an alternate answer.
Any Suggestion would be appreciated.
My tables are like:
Table users
id | name | email
---------
1 | John | [email protected]
2 | Michael | [email protected]
Table roles
id | name
---------
1 | Admin
2 | PrivilegedMember
3 | Subscriber
Table user_role
id | user_id | role_id
----------------------
1 | 1 | 1
2 | 2 | 2
This might be a duplicate of the below question but the answer provided leaves without explaining multiple redirections.
Multiple Authentication in Laravel 5.3
Implement an authenticated()
method in your LoginController
and add the redirection logic there:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
// ...
/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
*
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
if($user->hasRole('Admin')) {
return redirect()->intended('admin');
}
if ($user->hasRole('PrivilegedMember')) {
return redirect()->intended('PriviligedMember/index');
}
}
// ...
}
The method is called after the user is authenticated. See the last two lines of sendLoginResponse
:
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}
So it's a perfect candidate for such logics.
One other note on your own answer, the AuthenticatesUser
is a trait that horizontally extends the LoginController
, you can safely override any of its methods in your controller without touching the core files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With