Php 使用RESTAPI的Laravel电子邮件验证5.7
如何为RESTAPI重新制作Laravel 5.7电子邮件验证Php 使用RESTAPI的Laravel电子邮件验证5.7,php,laravel,api,email,email-verification,Php,Laravel,Api,Email,Email Verification,如何为RESTAPI重新制作Laravel 5.7电子邮件验证 还是一切从头开始都值得呢?这个案子适合我。完整的项目代码 1) 重新设计验证控制器控制器 删除了重定向并做出了response()->json(…)响应 <?php namespace App\Http\Controllers\API\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\VerifiesEmails; use I
还是一切从头开始都值得呢?这个案子适合我。完整的项目代码 1) 重新设计验证控制器控制器 删除了重定向并做出了
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');
}
}
3) 添加了configfrontend.php
:
4) 添加到用户模型:
及
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(),则会将它们添加到应用程序中代码>
据我所知,RESTAPI不需要控制器中的email/verify
路由及其方法
6) 在我的前端页面/verify email
(从frontend.php
config)中,我向参数queryURL
收到的URL如下所示:
"http://localhost:8000/api/email/verify/6?expires=1537122891&signature=0e439ae2d511f4a04723a09f23d439ca96e96be54f7af322544fb76e3b39dd32"
我的请求(带有授权标头):
代码完美地验证了电子邮件,如果已经验证,我可以捕获错误。此外,我还可以成功地将邮件重新发送到电子邮件
我在什么地方出错了吗?另外,如果您能改进一些,我将不胜感激。我尝试了ааааааааааааааа
public function __construct()
{
$this->middleware('auth')->except(['verify','resend']);
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
否则,laravel需要自动验证才能访问验证和重新发送路由我实现了您代码的一部分,并注意到您使用的是POST路由而不是GET?电子邮件将参数作为GET发送,并在该事件中抛出未知方法异常。我不记得为什么这样做。我重新回答了我的问题,改变了路线。谢谢女贞;)我是api编程新手,我只是想知道,如果你有auth()->user(),它真的是restful吗?@nrkz是的!但是您仍然需要使用tymon/jwt-auth
包(例如),它与内置授权集成,因此auth()->user()
可以工作。我设法让它与您的代码一起工作。非常好。但是你如何使用中间件来检查用户是否被验证?@CharStyle峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈峈,我不知道你是怎么做的?也许你应该删除$this->middleware('auth')->除了(['verify','resend'])代码>全部?由于只有两种方法:verify
,resend
。理论上,您也可以将$this->中间件('throttle:6,1')->仅替换为('verify','resend')代码>与$this->中间件('throttle:6,1')代码>@Бццццццццццццццц?
/**
* Send the email verification notification.
*
* @return void
*/
public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmail); // my notification
}
// 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');
"http://localhost:8000/api/email/verify/6?expires=1537122891&signature=0e439ae2d511f4a04723a09f23d439ca96e96be54f7af322544fb76e3b39dd32"
await this.$get(queryURL) // typical get request
public function __construct()
{
$this->middleware('auth')->except(['verify','resend']);
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}