laravel - laravel api 电子邮件验证链接给出未经身份验证的错误
问题描述
通过电子邮件发送的验证链接未经身份验证如何解决此问题?虽然在网络路由中工作正常但在 api 方法中它给出了错误?
验证控制器
<?php
namespace App\Http\Controllers\Api\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Auth\Access\AuthorizationException;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
protected $redirectTo = '/login';
/**
* Where to redirect users after verification.
*
* @var string
*/
public function show(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return response()->json('Email Verified');
}
else {
return response()->json('Email not verified');
}
}
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function verify(Request $request)
{
auth()->loginUsingId($request->route('id'));
if ($request->route('id') != $request->user()->getKey()) {
throw new AuthorizationException;
return redirect($this->redirectPath());
}
if ($request->user()->hasVerifiedEmail()) {
return response(['message'=>'Already verified']);
// return redirect($this->redirectPath());
}
if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return response(['message'=>'Successfully verified']);
}
/**
* 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:api');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
在我的用户类中
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, HasApiTokens;
这是我的路线
Route::middleware('auth:api')->group(function () {
//email verification//
Route::get('email/resend', 'Api\Auth\VerificationController@resend')->name('verification.resend');
Route::get('/email/verify/{id}/{hash}', 'Api\Auth\VerificationController@verify')->name('verification.verify');
Route::post('email/verify', 'Api\Auth\VerificationController@show')->name('verification.notice');
//email//
我已经尝试了所有方法,但它仍然给出错误和我的应用程序环境。url 是http://localhost ,我尝试将它与通知一起使用并添加 frontend.php 但仍然给我同样的错误如何解决?如果有人知道请告诉我????
解决方案
您应该删除Route::middleware('auth:api')->group(function () {
代码,因为这需要 URL 具有 API 令牌 ( http://example.com/?api_token=[token] )。如果这个令牌不在 url 中,Laravel 会抛出 401 - unauthenticated 错误。
您还可以添加 API 令牌,例如使用数据库 API 令牌 ( https://laravel.com/docs/5.8/api-authentication ),但我不建议将其用于电子邮件验证。
由于您使用的是默认的 Laravel 电子邮件验证,因此您还可以按照 Laravel 文档 ( https://laravel.com/docs/5.7/verification ) 中的步骤启用电子邮件验证。通过从 Internet 复制和粘贴代码,您很容易出错。
推荐阅读
- java - 转出基于文本字段的搜索栏
- powershell - 无法将变量解析到 ForEach($Servers 中的 $Server)循环中?
- php - 复杂对象的基于 PHP 条件的 JSON 输出
- android - 单击导航图的后退按钮时如何显示吐司
- php - Shopware 6:如何在管理模块的 Twig 模板中访问配置参数
- python - 列出我的子组中的所有工人
- laravel - Laravel 5.6 Flash Messages 必须自动关闭
- angular - 角度的动态嵌套菜单
- docker - 错误:根据名称找到 2 个匹配项:网络
_default 不明确 - sql - 如何搜索从某个时间点发生变化的 Oracle DB 条目?