gpt4 book ai didi

php - 在身份验证尝试中传递第二个参数后,记住登录页面上的用户凭据不起作用

转载 作者:行者123 更新时间:2023-11-28 02:23:49 25 4
gpt4 key购买 nike

这是在使用 artisan 命令 make::auth 的帮助下制作的登录页面,它生成了所有必需的代码,但请记住我这个登录页面无法正常工作。这是登录页面代码

<form id="login-form" method="post" role="form" style="display:@if($errors->has('NotMobileRegisterd')) {{ 'none' }} @else {{ 'block' }} @endif ;" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="_token" value="{{csrf_token()}}">
@if(Session::has('ErrorLogin'))
<div class="alert alert-danger">
<strong><i class="fa fa-ban"></i></strong> {{ Session::get('ErrorLogin')}}.
</div>
@endif
<div class="form-group form-group {{ $errors->has('email') ? ' has-error' : '' }}">
<input type="text" name="email" id="username" tabindex="1" class="form-control" placeholder="Username" 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 {{ $errors->has('password') ? ' has-error' : '' }}">
<input type="password" name="password" id="password" autocomplete="off" tabindex="2" class="form-control" placeholder="Password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group text-center">
<input type="checkbox" tabindex="3" class="" name="remember_me" id="remember">
<label for="remember"> Remember Me</label>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="submit" id="submit" tabindex="4" class="form-control btn btn-login" value="Log In">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<div class="text-center">
<a href="{{ route('password.request') }}" tabindex="5" class="forgot-password" >Forgot Password?</a>
</div>
<button type="button" class="btn btn-primary pull-left" onclick="displayOTP()" style="padding-top:10px; padding-bottom:10px;"><i class="fa fa-mobile-alt"> Login with OTP</i></button>
<a href="{{URL::to('auth/google')}}"><button type="button" class="btn btn-danger pull-right" style="padding-top:10px; padding-bottom:10px;"><i class="fa fa-at"> Login with Gmail</i></button></a>
</div>
</div>
</div>
</form>

将此发送到 authenticationUser 特征后,检查用户是否点击了记住我,然后在 auth::attempt 中传递值。代码工作正常,但是当用户注销时,登录表单中没有存储凭据

trait AuthenticatesUsers
{
use RedirectsUsers, ThrottlesLogins;

/**
* Show the application's login form.
*
* @return \Illuminate\Http\Response
*/
public function showLoginForm()
{
return view('auth.login');
}

/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
*/
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 ($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);
}

/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}

/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin(Request $request)
{
$remember_me=$request->has('remember_me')?true:false;
return $this->guard()->attempt(
$this->credentials($request),$remember_me
);
}

/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}

/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();

$this->clearLoginAttempts($request);

return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($this->redirectPath());
}

/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
//
}

/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];

if ($request->expectsJson()) {
return response()->json($errors, 422);
}

return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}

/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'email';
}

/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();

$request->session()->invalidate();

return redirect('/');

}

/**
* Get the guard to be used during authentication.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard();
}
}

而User表有remembering token属性,但在退出页面后仍然不记得用户凭证。

最佳答案

我认为您混淆了 Laravel 的记住我功能。

它不记住用户凭据,而是为登录用户设置更大的 session 时间限制。

因此,按照标准,用户将在 2 小时不活动后注销,勾选记住我框, session 设置为更长的时间,因此您可以离开该站点,5 小时后返回,您将仍处于登录状态。

但如果用户注销,则 session 将被销毁。

希望阐明功能。

关于php - 在身份验证尝试中传递第二个参数后,记住登录页面上的用户凭据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55610539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com