Иль*_*ько 6 php email api email-verification laravel
如何为Rest API重制Laravel 5.7电子邮件验证?
还是值得从头开始做一切?
Иль*_*ько 21
这个案子适合我.整个项目的代码在这里.
删除了重定向并做出了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');
}
}
Run Code Online (Sandbox Code Playgroud)
我做了这个,以便电子邮件中的链接导致我的前端并包含请求的temporarySignedRoute链接.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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='),
];
Run Code Online (Sandbox Code Playgroud)
use App\Notifications\VerifyEmail;
Run Code Online (Sandbox Code Playgroud)
和
/**
* Send the email verification notification.
*
* @return void
*/
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail); // my notification
}
Run Code Online (Sandbox Code Playgroud)
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');
Run Code Online (Sandbox Code Playgroud)
如果使用,它们将添加到应用程序中Auth::routes();.
据我所知email/verify,Rest API不需要控制器中的路由及其方法.
/verify-email(来自frontend.php配置),我向参数中包含的地址发出请求queryURL收到的URL如下所示:
"http://localhost:8000/api/email/verify/6?expires=1537122891&signature=0e439ae2d511f4a04723a09f23d439ca96e96be54f7af322544fb76e3b39dd32"
Run Code Online (Sandbox Code Playgroud)
我的请求(使用Authorization标头):
await this.$get(queryURL) // typical get request
Run Code Online (Sandbox Code Playgroud)
代码完美地验证了电子邮件,如果已经验证,我可以捕获错误.此外,我可以成功地将邮件重新发送到电子邮件.
我在某个地方犯了错误吗?如果你有所改进,我将不胜感激.