Laravel 密码重置错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44297669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel password reset error
提问by Diego
I'm developing an e-commerce application and it is using laravel's authentication. It gives me all the login/register/resetpassword logic and views. Login and register is working fine but the reset view is throwing me some errors. Reset view:
我正在开发一个电子商务应用程序,它使用 laravel 的身份验证。它给了我所有的登录/注册/重置密码逻辑和视图。登录和注册工作正常,但重置视图给我带来了一些错误。重置视图:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset password</div>
<div class="panel-body">
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ route('password.request') }}">
{{ csrf_field() }}
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-mail</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ $email or old('email') }}" required autofocus>
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
<label for="password-confirm" class="col-md-4 control-label">Confirm password</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
When i'm not logged in, i get: Undefined variable: tokenin the following piece of code:
当我没有登录时,我得到:未定义变量:以下代码中的令牌:
<input type="hidden" name="token" value="{{ $token }}">
But when i'm logged in I do not get any error but it simply redirects me to the home page. I know it must be because of the middleware but it makes no sense that a logged user can't have access to the "password reset" view. Any ideias?
但是当我登录时,我没有收到任何错误,但它只是将我重定向到主页。我知道这一定是因为中间件,但登录用户无法访问“密码重置”视图是没有意义的。有什么想法吗?
TL;DR
TL; 博士
Auth route (password reset):
身份验证路由(密码重置):
Route::group(['namespace' => 'Auth'], function () {
Route::get('/reset', 'ResetPasswordController@getPasswordResetView');
});
Password reset controller:
密码重置控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
public function getPasswordResetView()
{
return view("auth/passwords/reset");
}
}
回答by Sandeesh
You're using the wrong view. The view reset.blade.php
is used to display the password reset page when a user clicks on the reset link in the email. What you're looking for is email.blade.php
view which shows the password reset form where a user can enter their email to reset the password.
您使用了错误的视图。该视图reset.blade.php
用于在用户单击电子邮件中的重置链接时显示密码重置页面。您正在寻找的是email.blade.php
显示密码重置表单的视图,用户可以在其中输入他们的电子邮件来重置密码。
These are the built in auth routes
这些是内置的 auth 路由
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
The password reset request route uses showLinkRequestForm
method on ForgotPasswordController
.
密码重置请求路由使用上的showLinkRequestForm
方法ForgotPasswordController
。
public function showLinkRequestForm()
{
return view('auth.passwords.email');
}
The email password reset route uses showResetForm
method on ResetPasswordController
.
电子邮件密码重置路由使用 上的showResetForm
方法ResetPasswordController
。
public function showResetForm(Request $request, $token = null)
{
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
As you can see the $token
gets set by the controller based on the value received from the reset link.
如您所见,$token
控制器根据从重置链接收到的值设置了 get。
Also these two routes use guest
middleware. Which means you can view them only when you're not logged in. Trying to access them when you're already logged in would redirect you to the route set in the RedirectIfAuthenticated
middleware which is nothing but the guest
middleware.
这两条路线也使用guest
中间件。这意味着您只能在未登录时查看它们。在您已经登录时尝试访问它们会将您重定向到RedirectIfAuthenticated
中间件中设置的路由,该路由只是guest
中间件。
回答by Ayub
The password reset view you're thinking of is a forgotten password reset, so that's why it redirects home when you're already logged in.
您正在考虑的密码重置视图是忘记密码重置,因此当您已经登录时它会重定向到主页。
The token error you're getting is probably because you're not following the token link Laravel generates in the password reset email.
您收到的令牌错误可能是因为您没有遵循 Laravel 在密码重置电子邮件中生成的令牌链接。