Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sozyar's avatar

redirect-admin-to-back-admin-panel-after-login-in-laravel-5-8

I have configured auth to multi auth one is users table and another one is admins table and i did some changes both of tables have their own login separately when i logged in on user login page then will redirect me to admin panel the path is /back and i want for both case admin and user login should redirect me to admin panel same path means after logged in on admin login page redirect me to admin panel to same /back path please help.

web.php


Route::group(['prefix'=>'back','middleware'=>'auth'],function(){
        Route::get('/', 'Admin\DashboardController@index');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::prefix('admins')->group(function() {


Route::get('/login', 'Auth\AdminLoginController@showLoginForm')->name('admins.login');

Route::post('/login', 'Auth\AdminLoginController@login')->name('admin.login.submit');

Route::get('/', 'AdminController@index')->name('admins');

});

Auth/AdminLoginController.php


<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class AdminLoginController extends Controller
{

    public function __construct()
    {

        $this->middleware('guest:admin'); 

    }


    public function showLoginForm()
    {


        return view('auth.admin-login');

    }


    public function login(Request $request)
    {

         $this->validate($request, [
        'email'   => 'required|email',
        'password' => 'required|string|min:6'
      ]);


         if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {

        return redirect()->intended(route('admins')); 
      }



        return redirect()->back()->withInput($request->only('email', 'remember'));
    }


}


0 likes
40 replies
jlrdw's avatar

auth one is users table and another one is admins table

One "users" table is all that is needed. Anyone is a user.

I have an app with admin, bookkeeper, user, yet one users table.

The role and authorization determines who can do what.

Also you can determine a redirect in the authenticated method, see the trait usage.

By modifying the "out of box" auth, it makes it harder to troubleshoot when something isn't working.

Sozyar's avatar

if not can i make admin that can login on different page just two login page form user table how can i do it ?

jlrdw's avatar

That's exactly what I'd do, if you want the user login page to look different than admin, just have a separate login form. See this https://laracasts.com/discuss/channels/laravel/i-want-to-create-two-types-user-panel-for-two-types-user

It might help. Many sites have separate "looking" pages.

Doctors login is a separate form from patients login, that is the "look" of the page.

All can still be done with "out of box" Auth.

Also the authenticated method can be used to redirect:

pseudocode

protected function authenticated(Request $request, $user)
{
    if logged in user role is admin redirect to admin area
    if logged in user role is bookkeeper redirect to accounting area
    if logged in user role is user redirect to user area
}

Just quick example, as there is more to it, I have one admin who is also a bookkeeper, so I have a link that shows up if both roles are true.

1 like
Sozyar's avatar

in users table i have admin , editor and blogger i want admin can login from different page than editor and blogger login page all users exist in users table but blogger or editor shouldn't login from admin login page and same for admin too how can make this please help .

jlrdw's avatar
  • design a login page for admin
  • design a login page for editor and blogger
  • design a login page for whoever else

Just different designed pages.

Now just submit the forms as usual, the form designs have nothing to do with what happens in the controller and model.

As I said in example, a Doctor's login page can look and be designed different from a patient's login page. But still have the same table, controller, model to handle login.

Once logged in, authorization (policies or gates) will determine what the logged in user can or cannot do.

Sozyar's avatar

i want after logged in for admin , editor and blogger for all users redirect me to admin panel , only different login from should have

shami003's avatar

@jlrdw How will I assign roles to them if I use a single login page and table?

jlrdw's avatar

As I said, you can fine-tune this stuff in authenticated method

pseudocode

protected function authenticated(Request $request, $user)
{
    Put  code here to check the role.

    if logged in user role is admin redirect to admin area
    if logged in user role is blogger redirect to where ever area
    if logged in user role is user redirect to user area
}

What you are wanting to do is real simple, you may be just overthinking the steps.

Sozyar's avatar

How can use users table for this blade ? This is admin-login.blade

@include('layouts.app')


    <div class="sufee-login d-flex align-content-center flex-wrap">
        <div class="container">
           
                
            
            <div class="login-content">
                <div class="login-logo">
                    <a href="{{ route('login') }}">
                        <img class="align-content" src="{{ asset('others') }}/{{  $shareData['admin_logo'] }}" alt="">
                    </a><div style="color: white; text-align: center;">Admin Login</div>
                </div>
                <div class="login-form">
                    <form class="form-horizontal" method="POST" action="{{ route('admin.login.submit') }}">
                        {{ csrf_field() }} <!-- means you are still using middleware-->
                        <div class="form-group">
                            <label>Email address</label>
                            
                             <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                        </div>
                        <div class="form-group">
                            <label>Password</label>
                            <input id="password" type="password" class="form-control" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
                            </label>
                            <label class="pull-right">
                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    Forgot Your Password?
                                </a>
                            </label>

                        </div>
                        <button type="submit" name="login" class="btn btn-success btn-flat m-b-30 m-t-30">Sign in</button>
                        <div class="register-link m-t-15 text-center">
                             <br>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>


    <script src="{{ asset('admin/assets/js/vendor/jquery-2.1.4.min.js') }}"></script>
    <script src="{{ asset('admin/assets/js/popper.min.js') }}"></script>
    <script src="{{ asset('admin/assets/js/plugins.js') }}"></script>
    <script src="{{ asset('admin/assets/js/main.js') }}"></script>


</body>
</html>


jlrdw's avatar
<form method="POST" action="{{ route('login') }}">

As I said don't modify stuff, use out of the box authentication.

The email and password will work, determine the role once logged in.

Redirect based on what role user has.

This:

action="{{ route('admin.login.submit') }}"

Is not part of Taylor's scaffolded authentication. So don't use it.

Sozyar's avatar

I did and it works but all user roles can login from both login page how to make it one login particular to admin role other roles could not login from admin login please answer clearly it's very important to me

jlrdw's avatar

As said it gets more tricky the more things need added or modified.

You could also retrieve role when attempting a login. so if blogger tried to login from admin page, immediately redirect back with an error, "you are on the wrong login page".

Something like that.

But it would be easier to have the normal out of box login page and redirect to their area once logged in.

Like I already said:

pseudocode

protected function authenticated(Request $request, $user)
{
    Put  code here to check the role.

    if logged in user role is admin redirect to admin area
    if logged in user role is blogger redirect to where ever area
    if logged in user role is user redirect to user area
}

But yes what you need can be done, look at the attemptLogin method on the trait.

Backup your login controller before changing too many things. And don't modify the vendor files, use trait methods in your login controller.

Edit:

The least you have to modify now, the easier it will be for you to maintain later. You already are going to have to deal with custom login error messages. Whereas out of box it's already there. Just an observation.

Sozyar's avatar

can you come to my pc with anydesk to check it please ??

jlrdw's avatar

Not at this time. But if you do a little trial and error with the trait methods you should be fine.

Like I said, I stay with the out of box, except I do use the authenticated method in some apps.

I have separate admin and bookkeeping main pages, but I DO NOT use separate login forms.

You can customize authentication any way you want, my suggestions were only meant to make it easier to program and maintain.

I have seen some folks new to laravel that got Authentication and authorization so cross wired (messed up) that me and others told them the only fix is a new project.

Study how those trait methods can be used.

A side note, even though I use Authentication that come with laravel, I do use custom authorization. But only recommend customizing things to someone who has programmed for years, not someone new.

It's sort of like: https://gist.github.com/jimgwhit/ed44a6c81815804f1ab910ce9eb88d84

Still authenticating in laravel, but custom RBAC protecting controller methods.

Sozyar's avatar

I have checked you have distinguished users by role , but what i did i have distinguished users by type in users table only admin has" type 1 " others have "type 2" i don't know how to code ?

jlrdw's avatar

It doesn't matter if it's called type or role.

// instead of

if (Auth::user()->role == 'admin')

//Just

if (Auth::user()->type  == 1)

//  or keep name role and number them

if (Auth::user()->role  == 1)


Something like that.

Sozyar's avatar

This is AdminLoginController where should write code


<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class AdminLoginController extends Controller
{

	public function __construct()
	{

		$this->middleware('guest')->except('logout'); //who want to have access to this  // before //$this->middleware('guest:admin');

	}


    public function showLoginForm()
    {


    	return view('auth.admin-login');

    }


    public function login(Request $request)
    {
    	// Validate the form data
    	 $this->validate($request, [
        'email'   => 'required|email',
        'password' => 'required|string|min:6'
      ]);

    	// Attemp to log the user in
    	 if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
        // if successful, then redirect to their intended location
        return redirect()->intended(route('admins')); // we redirected them where they wanted to go  // admin.dashboard  // route('admins'); should be same with ->name('admins'); in page of web 
      }
    	

    	// If unsuccessful, then redirect back to the login with the form data
    	return redirect()->back()->withInput($request->only('email', 'remember'));
    }


}

jlrdw's avatar

You don't need a separate AdminLoginController. You only need the LoginController.

If you have modified the authentication, I don't know where to place code.

But in the LoginController you can have custom redirects in the authenticated method.

You can try your custom authentication with your AdminLoginController, I just don't know how it will work. But just try it out.

Also you are missing some use statements.

Sozyar's avatar

I have putted all the codes please check it and modify it then write the modified codes here

Sozyar's avatar

if not , can you come to my pc to check codes using remote controller ?

jlrdw's avatar

If you are new to laravel Authentication I would highly advise you to use the out of Box Authentication. As I said it works great.

Then you can redirect to whatever panel or dashboard you need.

And of course you have to also set up authorization.

Sozyar's avatar

Yeah I am new to laravel , but it's clear what i want admin should login from admin login and others should login from users login after logged in for all users should redirect to same admin panel and also i have putted all codes here asked that how to modify the code i got the idea about that but don't know how to code

jlrdw's avatar

also I'm on mobile device right now, when I get to desktop I will try to send you a sample.

    public function authenticated(Request $request, $user)
    {
        $role = $user->role;
        if ($role === 'admin') {
            return redirect('pet/admin');
        }
        if ($role === 'bkeep') {
            return redirect('account/index');
        }
        return redirect('pet/index');
    }

But that's the regular login controller, I don't how it works in a custom controller.

Sozyar's avatar

can i add public function authenticated method to regular login controller like your upper example for admin and users ?

Sozyar's avatar

it seems configured auth not Taylor's original auth so confused

jlrdw's avatar

What do you mean configured auth? I apologize I do not understand. And If you watch the free videos, it will help you so much.

Sozyar's avatar

This is default LoginController.php


<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/back'; // redirect to back page

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}


jlrdw's avatar

Read this part

This controller handles authenticating users for the application and redirecting them to your home screen. The controller uses a trait to conveniently provide its functionality to your applications.

Also there was an article on multiple auth, have a look:

https://laravelarticle.com/laravel-multi-authentication

But I don't know how to explain any better. You do know about traits?

Next

Please or to participate in this conversation.