Redirect Laravel 8 Fortify - laravel

I need some help with redirects after login with Laravel 8 Fortify. I know how to do it in a login controller but with Fortify there is a LoginResponse that I am not sure how to do this. I know in RouteService Provider I can change it to whereever but I have roles that I want to redirect to different dashboards based on role.
In the old Login Controller I would do the following. How would I change this to the LoginResponse for Fortiy?
public function redirectTo()
{
if(Auth::user()->hasRole('admin')){
$this->redirectTo = route('admin.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('manager')){
$this->redirectTo = route('manager.dashboard');
return $this->redirectTo;
}
if(Auth::user()->hasRole('employee')){
$this->redirectTo = route('employee.dashboard');
return $this->redirectTo;
}
}

You can Customize Redirects with Fortify by binding your own implementation of the LoginResponse. You could add the following to the register method of your FortifyServiceProvider:
use Laravel\Fortify\Contracts\LoginResponse;
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->instance(LoginResponse::class, new class implements LoginResponse {
public function toResponse($request)
{
if($request->user()->hasRole('admin')){
return redirect()->route('admin.dashboard');
}
if($request->user()->hasRole('manager')){
return redirect()->route('manager.dashboard');
}
if($request->user()->hasRole('employee')){
return redirect()->route('employee.dashboard');
}
}
});
}
If you'd prefer you can always create the class in an actual file instead of using an anonymous class.
Here is a Laravel News post that goes into it in a little more details.

Since you seem to have a custom dashboard for each role, if it were me instead of changing the under the hood as Rwd suggested I would make my default redirect page which is in /config/fortify.php as the home directory to be the page that handles the redirect. So your route('dashbord') will decide which dashboard the user needs:
Route::middleware([...])->group(function () {
Route::get('/dashboard', function () {
if(Auth::user()->hasRole('admin')){
$redirectTo = route('admin.dashboard');
}
if(Auth::user()->hasRole('manager')){
$redirectTo = route('manager.dashboard');
}
if(Auth::user()->hasRole('employee')){
$redirectTo = route('employee.dashboard');
}
return redirect()->to($redirectTo);
})->name('dashboard');
});

Related

laravel condition of user type on login

I want to make condition on login. The system will have different types of users.
Example: admin, manager, user.
I have tried all solutions given but it redirects the user to the same page.
Database: Table - users : column: role.
The condition will be based on the role when they register.
Here is my code:
Logincontroller.php
protected $redirectTo = '/dashboard';
public function __construct() {
$this->middleware('guest')->except('logout');
$this->redirectTo = '/dashboard';
$user = \Auth::user();
if ( ($role->admin() ) {
// an admin
$this->redirectTo = '/admin';
} elseif ( ($role->manager() ) {
// it's a manager
$this->redirectTo = '/manager/home';
}
else {
// it's a user
$this->redirectTo = '/dashboard';
}
}
Thanks in advance!
You cannot add the redirect logic in the constructor because it will be called before the user authentication is even attempted, therefore it will always be "/dashboard".
You could add the authenticated() method to your LoginController, this method will be run after the user was successfully authenticated.
protected function authenticated(Request $request, User $user)
{
if ($user->admin()) {
// an admin
$redirect = '/admin';
} elseif ($user->manager()) {
// it's a manager
$redirect = '/manager/home';
} else {
// it's a user
$redirect = '/dashboard';
}
return redirect($redirect);
}
Why not go for custom middleware for authentication and filtering user
Middleware
register in kernel and then apply your logic in middleware handle function
And as per your logic ,kindly confirm if you are checking your role properly because this may cause it end up in else condition.
I suggest making an isAdmin() and isManager()in the user model.
Do something like this:
In App\User.php
const ADMIN_TYPE = 'admin';
const MANAGER_TYPE = 'manager';
public function isAdmin() {
return $this->role === self::ADMIN_TYPE;
}
public function isManager() {
return $this->role === self::MANAGER_TYPE;
}
In the LoginController
use AuthenticatesUsers;
protected function authenticated() {
if (auth()->user()->isAdmin()) {
// an admin
$this->redirectTo = '/admin';
}
}
Thank you everyone for the solutions. After trying your code i modify a bit and get the solutions (but might occur problem later).
use Illuminate\Http\Request;
protected function authenticated($request, $user)
{
if($user->position = 'manager') {
// change the redirectTo variable as needed
$this->redirectTo = ('/manager/home');
}
else //($user->position = 'admin')
{
// change the redirectTo variable as needed
$this->redirectTo = ('/admin/dashboard');
}
return redirect()->intended($this->redirectPath());
}

how can i redirect user , seller and admin to their panel in laravel 5

i have level column in user table , i wanna redirect to their panel in laravel after login but i dont know where check and redirect user???
i try to login safty
Can this code be correct?
protected function authenticated(Request $request, $user)
{
if(auth()->user()->isAdmin())
{
return redirect('/admin/panel');
} elseif(auth()->user()->isWriter())
{
return redirect('/writer/panel');
}
return redirect('/user/panel');
}
Overwrite redirectPath() method to your LoginController.
class LoginController extends Controller
{
use AuthenticatesUsers;
public function redirectPath()
{
if (auth()->user()->isAdmin())
{
return '/admin/panel';
}
elseif (auth()->user()->isWriter())
{
return '/writer/panel';
}
return '/user/panel';
}
}
In LoginController that uses Illuminate\Foundation\Auth\AuthenticatesUsers trait for authentication you must override the redirectTo() method and write your own logic. so you must something like this code:
protected function redirectTo(){
$user = User::whereId(Auth::id())->first();
if(isset($user)){
if($user->role == 'admin'){
return '/admin/panel'; //path to admin panel
}elseif($user->role == 'writer'){
return '/writer/panle'; //path to writer panel
}
}
return '/';
}

Laravel 5: Redirect Not Working on Password Reset

I am trying to override Laravel's default function for resetting passwords. Before I go ahead with resetting the password, I want to dynamically set $redirectTo based on certain user information, so that the user is properly redirected to a different page based on said info. However, when I go try to go through with it I end up at a completely white page with the path ".../password/reset". The user's password is being properly reset, but the redirect isn't working as intended.
My code is referenced below. What am I doing wrong?
ResetPasswordController.php
class ResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo;
public function __construct()
{
$this->middleware('guest');
}
public function resetPasswordByUser(Request $request, Users $users)
{
$user = $users->findUserByEmail($request->input('email'));
if ($user->case == 1) {
$this->redirectTo = '/case1';
} elseif ($user->case == 2) {
$this->redirectTo = '/case2';
} elseif ($user->case == 3) {
$this->redirectTo = '/case3';
}
$this->reset($request);
}
}
web.php
Auth::routes();
.......
Route::post('/password/reset', 'Auth\ResetPasswordController#resetPasswordByUser');

Laravel 5.2 Redirect admin page after login as admin

Laravel 5.2 has been out for some time now. Yes, it has new auth function which is very good. Specially for beginners.
My question,
How to check if user is admin and then redirect safely to admin/dashboard properly? I know one way is to use admin flag in database but can any of you show some example?
go to AuthController.php and add this method
where role is the user role as defined in the database.
protected function authenticated($request,$user){
if($user->role === 'admin'){
return redirect()->intended('admin'); //redirect to admin panel
}
return redirect()->intended('/'); //redirect to standard user homepage
}
As in Laravel 5.3 / 5.4
Add following line to create_users_table migration.
$table->boolean('is_admin');
Add following method to LoginController.
protected function authenticated(Request $request, $user)
{
if ( $user->is_admin ) {
return redirect('/admin/home');
}
return redirect('/home');
}
Additional note don't forget to add the following lines to RedirectIfAuthenticated middleware:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
// the following 3 lines
if (Auth::user()->is_admin) {
return redirect('/admin/home');
}
return redirect('/home');
}
return $next($request);
}
Otherwise if you e.g. type yourdomain/login and your logged in as admin it would be redirected to home instead of admin/home.
AuthController extends the AuthenticatesAndRegistersUsers trait, which has a public method named redirectPath. In my case I would extend that method on the AuthController and I'd put my logic there:
public function redirectPath()
{
if (Auth::user->myMethodToCheckIfUserHasAnAdminRoleLikeAnEmailOrSomethingLikeThat()) {
redirect('admin/dashboard');
}
redirect('home');
}
in Auth/LoginController there is protected $redirectTo = '/home';
change '/home' to '/loginin' for example, and create a new controller and in the controller get the information of the user and check if he is a admin or not and then redirect him to the proper page

How to redirect intended user to a different route based on their role?

I'd like to redirect my user to different route, based on their role. I have two secured area in my app, "admin" and "dashboard". I'd like to check if user is authenticated, then redirect to intended, but if user has role editor it should be redirected to dashboard, otherwise if he has role admin should be redirected to admin area.
I'm using AuthenticatesAndRegistersUsers class in my login. I have this on my custom controller:
/**
* The default redirecTo path.
*
*/
protected $redirectTo = '/dashboard';
So when a user is authenticated it will be redirected to dashboard, but I'd like to check if the intended url is on admin group route and if user has admin role it should be redirected to admin area.
I'm using this middleware to redirect to login:
public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('auth/login');
}
}
return $next($request);
}
You could overwrite the redirectPath method used by the trait in your AuthController to inject the logic you need. Something like this:
/**
* Get the post register / login redirect path.
*
* #return string
*/
public function redirectPath()
{
// Logic that determines where to send the user
if (\Auth::user()->type == 'admin') {
return '/admin';
}
return '/dashboard';
}
EDIT:
Laravel uses the following declaration in the AuthenticatesAndRegistersUsers trait to redirect the user after successful login:
return redirect()->intended($this->redirectPath());
This will try to redirect the user to the previously attempted URL.
If you need to redirect users to the right place when they're already logged in, that would be best done by adding more logic to your authentication middleware.
Another approach is to override authenticated method
public function authenticated()
{
if(Auth::check()) {
if(\Auth::user()->hasRole('Super Admin')) {
return redirect('/admin-dashboard');
} else {
return redirect('/user-dashbaord');
}
}
}
I use this one. You also need to modify middleware RedirectIfAuthenticated so that it won't route home. Just to different user's dashboard.
public function authenticated()
{
if($request->user()->hasRole('admin'))
{
// return redirect()->intended(route('admin.index'));
return redirect()->route('admin.index');
}
if($request->user()->hasRole('super'))
{
return redirect()->route('super.index');
}
if($request->user()->hasRole('officer'))
{
return redirect()->route('officer.index');
}
}

Resources