douao3063 2018-01-02 02:15
浏览 45

Laravel 5.5 Multi-Auth在经过身份验证时不会重定向

I'm creating a multi auth system in laravel where there is two types of users: Admins(created by me) and Users(using the native laravel auth system).

If I login as a User, when I try to access the login page when I'm already logged in, it redirects me back to the dashboard but If I login as an Admin, when I access to the Admin login page again, it let's me login again despite being already logged in as an Admin.

Here is my code for the class RedirectIfAuthenticated:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        switch ($guard)
        {
            case 'admin':
                if(Auth::guard($guard)->check())
                {
                    return redirect()->route('admin.dashboard');
                }
                break;

            default:
                if(Auth::guard($guard)->check())
                {
                    return redirect('/home');
                }
                break;
        }

        /*
        if (Auth::guard($guard)->check())
        {
            return redirect('/home');
        }
        */

        return $next($request);
    }
}

Can someone explain me what is happening?

  • 写回答

1条回答 默认 最新

  • duanshan1856 2018-01-02 05:05
    关注

    Can you show the routes you've registered that are meant to be protected for admins? By default when you use the auth middleware it's going to use the default auth guard - not your admin one.

    When you register routes you want to protect with the admin guard you will need to do it slightly differently. Either by using a route group or the middleware method in a controller.

    // Cover a block of routes...
    
    Route::group(['middleware' => 'auth:admin'], function () {
        //
    });
    
    // Or do it in your controller...
    
    class PageController extends Controller
    {
        public function __construct()
        {
            $this->middleware('auth:admin');
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭