通过电子邮件发送的验证链接提供了未经身份验证的解决方法? 虽然在网络路由中工作正常,但在api方法中却给出了错误消息?
verificationController
<?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//
我尝试了所有方法,但仍然给出错误和我的应用程序环境。网址为http://localhost 并且我尝试将其与通知一起使用并添加frontend.php,但仍然给我同样的错误,如何解决? 如果有人知道,请让我知道吗????
答案 0 :(得分:0)
您应该删除Route::middleware('auth:api')->group(function () {
代码,因为这需要URL具有API令牌(http://example.com/?api_token=[token])。如果此令牌不在URL中,则Laravel将抛出401-未经身份验证的错误。
您还可以添加API令牌,例如通过使用数据库API令牌(https://laravel.com/docs/5.8/api-authentication),但是我不建议您通过电子邮件进行验证。
由于您使用的是默认的Laravel电子邮件验证,因此您还可以按照Laravel文档(https://laravel.com/docs/5.7/verification)中的步骤启用电子邮件验证。通过从Internet复制和粘贴代码,您很容易出错。