1

我正在向基本 Laravel 登录路线发布普通的惯性帖子:

submit() {
      this.$inertia.post("/login", {
        email: this.emailAddress,
        password: this.password,
      }, {
        preserveState: true,
        preserveScroll: true,
      });
}

我能够按预期捕获验证错误,但我试图避免的是成功用户身份验证后的重定向,而是继续进入“登录”状态(更新标题以显示用户信息等)。

Laravel AuthenticatesUserstrait 包含 this 包含两个关键方法,它们作为开箱即用登录流程的一部分被调用

public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        return $request->wantsJson()
                    ? new Response('', 204)
                    : redirect()->intended($this->redirectPath());
    }

我正在努力弄清楚是否甚至可以在不以这种方式重定向的情况下对用户进行身份验证。

4

2 回答 2

1

您需要使用 javascript 前端,而不是 Inertia::post() 。一种方法是使用 Axios:

submit() {
    const data = {...this.form.data()};
    axios.post('/auth/login', data, {
       headers: {
          'Content-Type': 'application/json',
       },
    })
    .then(res => {
       console.log('login success!', res);
    });
于 2021-02-05T10:35:54.083 回答
0

检查您的表单和提交方式 - 您是否阻止表单提交的默认行为?似乎您正在发送 POST,但也触发了本机表单行为。

你也可以$redirectTo在你的 LoginController 中设置一个,如果没有给出任何其他内容,还可以检查是否RouteServiceProviderpublic const HOME = '/'触发重定向。

于 2020-08-11T14:41:17.780 回答