首页 文章

在Laravel中使用两个登录页面时出现重定向错误

提问于
浏览
0

我的目标是在移动设备和桌面设备上使用两个登录页面 .

移动登录页面使用ID和密码,原始使用电子邮件和密码 .

所以我创建了一个移动页面,其中包含一个简单的登录表单,其中只包含两个名为In / Out的按钮,此表单的操作目标与原始Laravel Auth登录路由('/ login')相同,以便使用登录验证 .

所以我在下面的代码中添加了在Http / Auth / LoginController中使用另一个登录页面 .

//Http/Auth/LoginController

public function username()
{
    if(request('id')) {
        return 'id'; // if request contains id in mobile then return it
    }
    return 'email'; // else return email
}

protected function validateLogin(Request $request)
{
    if(request('id')){
        $this->validate($request, [
            $this->username() => 'required|numeric',
            'password' => 'required|string',
        ]);
    }
}

protected function authenticated(Request $request, $user)
{
    if ($request->in == 'in') {
        return redirect()->route('mobiles_start', ['in' => 'in']);
    } // route('mobiles_start') is target to logic controller 
      // and after worked then return to mobile login view.

    elseif ($request->out == 'out') {
        return redirect()->route('mobiles_destroy', ['out' => 'out']);
    } // route('mobile_destroy) also.
}

public function showLoginForm(Request $request)
{
    if ($request->in == 'in' || $request->out == 'out') {
        return view('mobiles.login');
    }

    else return view('auth.login');
}

但问题是如果登录在移动登录页面中失败,那么总是重定向到原始登录页面('auth.login')而不是移动登录页面 .

我怎样才能进行移动登录页面的重定向?

4 回答

  • 0

    您需要在此LoginController中覆盖sendFailedLoginResponse .

    Laravel 5.3的代码:

    protected function sendFailedLoginResponse(Request $request)
        {
            return redirect()->back()
                ->withInput($request->only($this->username(), 'remember','in','out'))
                ->withErrors([
                    $this->username() => Lang::get('auth.failed'),
                ]);
        }
    

    Laravel 5.5的代码(类似于Wreigh的帖子):

    protected function sendFailedLoginResponse(Request $request)
    {
       if ($request->in == 'in' || ) {
            throw ValidationException::withMessages([
                $this->username() => [trans('auth.failed')],
            ])->redirectTo('/login?in=in');
       } else if($request->out == 'out'){
            throw ValidationException::withMessages([
                $this->username() => [trans('auth.failed')],
            ])->redirectTo('/login?out=out');
       } else {
            throw ValidationException::withMessages([
                $this->username() => [trans('auth.failed')],
            ]);
       }
    }
    

    由于原始sendFailedLoginResponse仅使用用户名重新定向并记住参数,因此添加和输出会使其工作我相信 .

    但是,以上是一个快速的解决方法 . 对于桌面和移动设备,您应该使用响应式网页设计并使用刀片模板为登录功能提供不同的参数 .

    此外,我会使用更好的结构来确定它是来自移动登录还是正常的登录页面 . 例如,给出像 is_mobile_login 这样的变量会使代码更具可读性 .

    希望能帮助到你 .

  • 1

    尝试从 AuthenticatesUsers 特征覆盖 sendFailedLoginResponse .

    在LoginController中添加它 .

    use Illuminate\Validation\ValidationException;
    
    protected function sendFailedLoginResponse(Request $request)
    {
        $validationException =  ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    
        if ($request->in == 'in' || $request->out == 'out') {
            return $validationException->redirectTo('mobile/url');
        }
        return $validationException->redirectTo('auth/url');
    }
    

    请注意 redirectTo 接受 URL 而不是视图或路线名称 .

  • 1

    我猜测路线'mobiles_destroy'导致了错误的方法 . 检查该方法的重定向 .

  • 0

    失败/丢失身份验证的重定向在 Illuminate\Foundation\Exceptions\Handler.php 中定义

    /**
     * Convert an authentication exception into a response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
                    ? response()->json(['message' => $exception->getMessage()], 401)
                    : redirect()->guest(route('login'));
    }
    

    您感兴趣的功能是 unauthenticated()invalid() .

    根据您的平台位置,您可以覆盖 App\Exceptions\Handler.php 中的那些,从而控制失败的身份验证和重定向的流程 .

    希望有所帮助 .

相关问题