How to implement Remember me in Laravel 8?(如何在《拉威尔8》中实现《记住我》?)
本文介绍了如何在《拉威尔8》中实现《记住我》?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个登录表单和一个"记住我"复选框。如果用户在上次登录时选中了"记住我"复选框,我希望自动填写用户名和密码,但目前不起作用。我正在使用在Laravel中构建的LoginController。
LoginController:
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppProvidersRouteServiceProvider;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
use IlluminateValidationValidationException;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::DASHBOARD;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Check either username or email.
* @return string
*/
public function username()
{
$identity = request()->get('identity');
$fieldName = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
request()->merge([$fieldName => $identity]);
return $fieldName;
}
/**
* Validate the user login.
* @param Request $request
*/
protected function validateLogin(Request $request)
{
$this->validate(
$request,
[
'identity' => 'required|string',
'password' => 'required|string',
],
[
'identity.required' => 'Username or email is required',
'password.required' => 'Password is required',
]
);
}
/**
* @param Request $request
* @throws ValidationException
*/
protected function sendFailedLoginResponse(Request $request)
{
$request->session()->put('login_error', trans('auth.failed'));
throw ValidationException::withMessages(
[
'error' => [trans('auth.failed')],
]
);
}
}
login.blade.php:
@extends('layouts.app')
@section('content')
<div class="topMargin container">
<br><br>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header" text-align="center">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
@csrf
<div class="form-group row">
{{-- <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label> --}}
<div class="col-md-12">
<input id="identity" type="identity" class="form-control{{ $errors->has('identity') ? ' is-invalid' : '' }}" name="identity" value="{{ old('identity') }}" placeholder="{{ __('Email or Username') }}" required autofocus>
@if ($errors->has('identity'))
<span class="invalid-feedback">
<strong>{{ $errors->first('identity') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
{{-- <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label> --}}
<div class="col-md-12">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" placeholder="Password" required>
@if ($errors->has('password'))
<span class="invalid-feedback">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div style="text-align:center">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }}
</label>
</div>
</div>
</div>
<div class="form-group">
<div style="text-align:center">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
</div>
<br>
<div style="text-align:center">
<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
如有任何帮助,我们将不胜感激!
推荐答案
看起来您使用的是Laravel提供的默认身份验证,这意味着您不需要在Http/Controllers/Auth/LoginController
到中执行任何操作,如果您在views/auth/login.blade.php
表单中具有默认表单输入名称值,则请记住用户!
您唯一需要确保的是在users
表中存储remember_token
字段。
参见:https://laravel.com/docs/8.x/authentication#remembering-users
这篇关于如何在《拉威尔8》中实现《记住我》?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在《拉威尔8》中实现《记住我》?
基础教程推荐
猜你喜欢
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01