【问题标题】:How to access the password reset token inside `emails.auth.reminder`?如何访问“emails.auth.reminder”中的密码重置令牌?
【发布时间】:2014-08-24 03:24:15
【问题描述】:

我刚刚开始学习 Laravel,我一直在关注文档。我知道 Laravel 使用 emails.auth.reminder 视图作为电子邮件发送给用户,并重置 token。在我的emails.auth.reminder 中,我写了以下内容:

Hello Dear User,<br><br>
We have received a request from your account to reset your password at Larblog. Please use the following link to reset your password.<br><br>

{{ URL::to( 'user/resetpassword/' . Session::get('_token') ) }}<br><br>

If it wasn't you who tried to reset the password, simply ignore this email.<br><br>
Thanks,<br>
- Larblog

请注意,我使用Session::get('_token') 来访问令牌。这是我这样做的正确方式吗?因为它总是生成相同的令牌。Z7vKMT5ssfzeXsQcVkrYodoRmYnbjH0prdP83jBk 一次又一次。当我用它来重置密码时,它说:Invalid token received。另外,我检查了我的数据库的password_reminders 表,它显示了不同的令牌。当我使用存储在数据库中的令牌时,它可以工作。

那么,在通过电子邮件发送的视图中访问令牌的正确方法是什么?

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    我不确定直接访问密码提醒令牌的方法是什么,但通常在发送电子邮件时,它是通过 Password::remind() 函数完成的,而不是通常的电子邮件函数。使用它时,$token 变量会自动传递到电子邮件视图中,以便您可以使用它。

    这个函数的一个例子是:

    Password::remind(Input::only('email'), function ($message)
    {
        $message->subject('Password Reset');
    });
    

    然后在视图中访问就这么简单:

    To reset your password, complete this form: {{ URL::to('reset', array($token)) }}
    

    【讨论】:

    • 太棒了!感谢您的宝贵回答:-)
    【解决方案2】:

    会话中的 _token 不是密码提醒令牌 - 出于安全原因,它是自动插入的 the CSRF token

    由于我不知道您的表格和模型是什么样的,因此很难确切地说您应该如何获得实际的密码提醒令牌,但可能是这样的:

    $user = User::find($someid);    // first fetch your user
    $token = $user->passwordReminder->token; // now get the token
    

    如果您的表格和模型与我的假设不同,请随时更新您的问题,我很乐意更新我的答案。 :)

    【讨论】:

    • 感谢您的回答 :-)
    猜你喜欢
    • 2015-12-18
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多