通过API进行电子邮件验证

时间:2020-05-18 22:46:03

标签: laravel-7

在用户通过api注册后,我尝试验证电子邮件,我能够将验证码发送到我在mailtrap中有响应的电子邮件,现在,当我单击“验证电子邮件”按钮时,出现此错误在null上调用成员函数getKey() 这是我的代码

 public function verify(Request $request)
{
    if (! hash_equals((string) $request->route('id'), (string) $request->user()->getKey())) {
        throw new AuthorizationException;
    }

    if (! hash_equals((string) $request->route('hash'), sha1($request->user()->getEmailForVerification()))) {
        throw new AuthorizationException;
    }

    if ($request->user()->hasVerifiedEmail()) {
        return $request->wantsJson()
            ? new Response(['message' => 'Already Verified'], 204)
            : redirect($this->redirectPath());
    }

    if ($request->user()->markEmailAsVerified()) {
        event(new Verified($request->user()));
    }

    if ($response = $this->verified($request)) {
        return $response;
    }

    return $request->wantsJson()
        ? new Response(['message' => 'Successfully Verified'], 204)
        : redirect($this->redirectPath())->with('verified', true);

}

这是路线

Route::get('/email/verify/{id}/{hash}', 'Api\VerificationController@verify')->name('verification.verify');

由于前端尚未准备就绪,我正在使用邮递员来测试API

这是发送到邮件陷阱的链接

http://127.0.0.1:8000/api/email/verify/3/258fb90b7a21cd79eaea6c39c88059af0090924d?expires=1589842576&signature=5a7b6463514c6dc8404f20cba54e5e8e7aae013eecec3af0e0f34d58aa7e993e

2 个答案:

答案 0 :(得分:0)

如果这是经过身份验证的用户,则可以尝试使用Auth::user()代替$request->user()

答案 1 :(得分:0)

也许现在回答为时已晚,但我遇到了同样的问题,唯一为我解决了这个问题的是:

use App\Model\User;

然后在这个“验证”方法中

$user = User::findOrFail($request->route("id"));

最后,用这个 $user 替换 $request()->user,