首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用REST API的Laravel电子邮件验证5.7

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

Stack Overflow用户
提问于 2018-09-17 15:40:05
回答 2查看 16.1K关注 0票数 14

如何为Rest API重新制作Laravel 5.7电子邮件验证?

还是说一切都值得从头做起?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-11 01:03:25

这个案子对我很管用。完整的项目代码here

1)重新设计VerificationController控制器

已删除重定向并发出response()->json(...)响应。

代码语言:javascript
复制
<?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)添加了我的通知:

我这样做了,电子邮件消息中的链接指向我的前端,并包含请求的temporarySignedRoute链接。

代码语言:javascript
复制
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

代码语言:javascript
复制
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)添加到用户模型中:

代码语言:javascript
复制
use App\Notifications\VerifyEmail;

代码语言:javascript
复制
/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new VerifyEmail); // my notification
}

5)添加路由

Laravel中使用以下路由:

代码语言:javascript
复制
// 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路由及其方法。

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

收到的URL如下所示:

代码语言:javascript
复制
"http://localhost:8000/api/email/verify/6?expires=1537122891&signature=0e439ae2d511f4a04723a09f23d439ca96e96be54f7af322544fb76e3b39dd32"

我的请求(带Authorization头部):

代码语言:javascript
复制
await this.$get(queryURL) // typical get request

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

我在什么地方弄错了吗?另外,如果你能改进一些东西,我将不胜感激。

票数 37
EN

Stack Overflow用户

发布于 2019-03-09 16:23:14

我尝试了ИльяЗеленькоanswer,但我必须修改VerificationController构造方法,如下所示

代码语言:javascript
复制
public function __construct()
{
    $this->middleware('auth')->except(['verify','resend']);
    $this->middleware('signed')->only('verify');
    $this->middleware('throttle:6,1')->only('verify', 'resend');
}

否则,laravel需要进行身份验证才能访问、验证和重新发送路由

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52362927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档