【问题标题】:Laravel - The token field is required while reset passwordLaravel - 重置密码时需要令牌字段
【发布时间】:2020-06-25 10:41:52
【问题描述】:

我将 laravel 5.2.4 更新到 5.6。 在修复一些错误时,我在重置密码时遇到错误 - “需要令牌字段。”

我在表单中使用参数 - @csrf

                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/password/email') }}">

            @csrf

                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                            <label class="col-md-4 control-label">E-Mail Address</label>

                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">

                                @if ($errors->has('email'))
                                    <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</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">
                                    <i class="fa fa-btn fa-envelope"></i>Send Password Reset Link
                                </button>
                            </div>
                        </div>
                    </form>

在路线中

    Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    Route::post('password/reset', 'Auth\PasswordController@reset');

密码控制器:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;

/**
 * @property string linkRequestView
 * @property string resetView
 */
class PasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */
    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->middleware('guest');
        $this->linkRequestView = 'auth.passwords.email';
        $this->resetView = 'auth.passwords.reset';
        if (strpos($request->path(), 'ex') === 0) {
            $this->linkRequestView = 'ex.auth.passwords.email';
            $this->resetView = 'ex.auth.passwords.reset';
        }
    }
}

我必须改变什么来解决这个问题? 当我在页面上看到 html 代码时 - 令牌字段存在“_token”但当我提交时 - 需要令牌。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要 csrf 令牌,方法是在您的 form 元素中使用它:

    <input type="hidden" name="token" value="{{ csrf_token() }}">
    

    如果 avobe 方法失败,则将 token 值设为:

    <input type="hidden" name="token" value="{{ $token }}">
    

    【讨论】:

    • 我这样做。但出现新错误:此密码重置令牌无效。为什么要通过@csrf 字段“_token”和您的代码“token”?
    • &lt;input type="hidden" name="token" value="{{ $token }}"&gt; 怎么样?
    • 如果 {{ $token }} 它会生成空字段并需要令牌错误 ((
    • 尝试将 @csrf 更改为 {{ csrf_field() }} 并告诉我发生了什么
    • 比我有 _token 字段和“令牌字段是必需的”。错误
    【解决方案2】:

    在我的情况下(Laravel 7.x)我缺少 csrf 令牌和密码重置令牌,所以当我把它们都正常工作时:

    <form action="{{ route('password.update') }}" method="post">
      @csrf
        <input type="hidden" name="token" value="{{ $token }}">
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2019-09-16
      • 2018-09-30
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多