php - 使用 REST API 的 Laravel 电子邮件验证 5.7

标签 php laravel api email email-verification

如何为 Rest API 重制 Laravel 5.7 邮箱验证?

还是一切都值得从头开始?

最佳答案

这个案例适合我。完整项目代码 here .

1) 重新设计的VerificationController Controller

删除重定向并生成 response()->json(...) 响应。

<?php

namespace App\Http\Controllers\API\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;

class VerificationController extends Controller
{
    use VerifiesEmails;

    /**
     * Show the email verification notice.
     *
     */
    public function show()
    {
        //
    }

    /**
     * Mark the authenticated user's email address as verified.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function verify(Request $request)
    {
        // ->route('id') gets route user id and getKey() gets current user id() 
        // do not forget that you must send Authorization header to get the user from the request
        if ($request->route('id') == $request->user()->getKey() &&
            $request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
        }

        return response()->json('Email verified!');
//        return redirect($this->redirectPath());
    }

    /**
     * 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()->json('User already have verified email!', 422);
//            return redirect($this->redirectPath());
        }

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

        return response()->json('The notification has been resubmitted');
//        return back()->with('resent', true);
    }

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
}

2) 添加了我的通知:

我这样做是为了让电子邮件中的链接指向我的前端,并包含请求的临时 SignedRoute 链接。

use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailBase;

class VerifyEmail extends VerifyEmailBase
{
//    use Queueable;

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        $prefix = config('frontend.url') . config('frontend.email_verify_url');
        $temporarySignedURL = URL::temporarySignedRoute(
            'verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]
        );

        // I use urlencode to pass a link to my frontend.
        return $prefix . urlencode($temporarySignedURL);
    }
}

3) 添加配置 frontend.php:

return [
    'url' => env('FRONTEND_URL', 'http://localhost:8080'),
    // path to my frontend page with query param queryURL(temporarySignedRoute URL)
    'email_verify_url' => env('FRONTEND_EMAIL_VERIFY_URL', '/verify-email?queryURL='),
];

4) 添加到用户模型:

use App\Notifications\VerifyEmail;

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new VerifyEmail); // my notification
}

5) 添加路由

Laravel 中使用了以下路由:

// Email Verification Routes...
Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth\VerificationController@verify')->name('verification.verify');
Route::get('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');

如果使用 Auth::routes();,它们将被添加到应用程序中。

据我了解,Rest API 不需要 email/verify 路由及其在 Controller 中的方法。

6) 在我的前端页面/verify-email(来自frontend.php 配置)我向参数queryURL< 中包含的地址发出请求

收到的 URL 如下所示:

"http://localhost:8000/api/email/verify/6?expires=1537122891&signature=0e439ae2d511f4a04723a09f23d439ca96e96be54f7af322544fb76e3b39dd32"

我的请求(带有授权 header ):

await this.$get(queryURL) // typical get request

该代码完美地验证了电子邮件,如果已经验证,我可以捕获错误。我也可以成功地将消息重新发送到电子邮件。

我是不是哪里弄错了?如果您能改进一些东西,我也将不胜感激。

关于php - 使用 REST API 的 Laravel 电子邮件验证 5.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52362927/

相关文章:

api - Shopware 6 通过 store-api 获取父产品

javascript - 通过 AJAX 设置 fusioncharts 数据集

javascript - JSON 错误 : SyntaxError: JSON. 解析:JSON 数据第 2 行第 1 列出现意外字符

php - 使用 Eclipse 3.5 PDT,如何阻止它自动转换 <?进入 <?php ?>?

php - 从 Laravel 中的 whereHas 查询返回数据

api - 如何从 Asp.net 核心向 Flutter 应用程序发送推送通知

PHP 和浏览器 'Back' 按钮 - 到底发生了什么

laravel - 如何在 Laravel MIX 上创建 svg Sprite ?

php - Laravel Eloquent 查询从另一个模型获取用户

Azure:可以将委托(delegate)的 API 权限分配给托管标识吗?