Laravel Email Verification via Api

默认情况下,Laravel提供了一个web的邮箱验证路由,

Laravel 的 Auth\VerificationController 类包含了发送验证链接和验证 Email 的必要逻辑。通过将 verify 选项传给 Auth::routes 方法,就能为这个控制器注册所需要的路由:

Auth::routes(['verify' => true]);

没有api请求的。

先复制一下Auth文件夹内的VerificationController.php文件,到Api文件夹下,主要用来参考。

记得修改namespace

批注 2020-04-15 002823

然后在api.php文件中添加:

Route::get('email/resend', 'Api\VerificationController@resend')->name('verification.resend');

resend方法位于

VerifiesEmails trait内:

批注 2020-04-15 003225

然后在api.php文件中添加:

Route::get('email/verify/{id}/[hash]', 'Api\VerificationController@verify')->name('verification.verify');
该方法同样位于VerifiesEmails trait内:

批注 2020-04-15 003500

如果需要重写上述两个方法,在Api\VerificationController.php文件中添加:

/**
 * Mark the authenticated user's email address as verified.
 *
 * @param \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 *
 * @throws \Illuminate\Auth\Access\AuthorizationException
 */

public function verify(Request $request)
     {
         auth()->loginUsingId($request->route('id'));

        if ($request->route('id') != $request->user()->getKey()) {
             throw new AuthorizationException;
         }

        if ($request->user()->hasVerifiedEmail()) {
             return \response(['message' => 'Already verified!']);
         }

        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);
     }

/**
 * Resend the email verification notification.
 *
 * @param \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 */
public function resend(Request $request)
{
    if ($request->user()->hasVerifiedEmail()) {
        return \response(['message' => 'Already verified!'], 204);
    }

    $request->user()->sendEmailVerificationNotification();

    return \response(['message' => 'Email Sent!'], 200);
}

因为是api请求,所以构造函数中修改

$this->middleware('auth');

为:

$this->middleware('auth:api')->only('resend');

记得将user模型类实现MustVerifyEmail接口:

批注 2020-04-15 005108

这个接口实现后才能使用user发送验证邮件:

<?php

namespace Illuminate\Contracts\Auth;

interface MustVerifyEmail
{
    /**
     * Determine if the user has verified their email address.
     *
     * @return bool
     */
    public function hasVerifiedEmail();

    /**
     * Mark the given user's email as verified.
     *
     * @return bool
     */
    public function markEmailAsVerified();

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification();

    /**
     * Get the email address that should be used for verification.
     *
     * @return string
     */
    public function getEmailForVerification();
}

接下来用Postman测试注册请求:

批注 2020-04-15 005334

注册成功,拿到token:

批注 2020-04-15 005414

然后设置token,发送请求至http://laravelauth.test/api/email/resend

批注 2020-04-15 005636

结果如下:

批注 2020-04-15 005715

打开Laravel.log文件可以看到发送了:

批注 2020-04-15 005833

拷贝这个链接,在postman中测试发送get请求至这个链接:

注:图上id已经是4了,因为我测试了几次发送再截的图。

批注 2020-04-15 012929

因为请求了第二次:

批注 2020-04-15 013020

猜你喜欢

转载自www.cnblogs.com/dzkjz/p/12702598.html