代码之家  ›  专栏  ›  技术社区  ›  JsWizard

在Laravel中使用两个登录页时发生重定向错误

  •  0
  • JsWizard  · 技术社区  · 8 年前

    我的目标是使用移动和桌面上的两个登录页面。

    手机登录页使用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 回复  |  直到 8 年前
        1
  •  1
  •   Jasonfish    8 年前

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

    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')],
            ]);
       }
    }
    

    is_mobile_login

        2
  •  1
  •   Wreigh    8 年前

    sendFailedLoginResponse AuthenticatesUsers

    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

        3
  •  0
  •   Codex    8 年前

        4
  •  0
  •   sid405    8 年前

    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