【问题标题】:laravel api Email verification link gives unauthenticated errorlaravel api 电子邮件验证链接给出未经身份验证的错误
【发布时间】:2019-10-28 19:41:38
【问题描述】:

通过电子邮件发送的验证链接提供未经身份验证的如何解决此问题? 虽然在网络路由中工作正常,但在 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//

我已经尝试了所有方法,但它仍然给出错误和我的应用程序环境。网址是http://localhost 我尝试将它与通知一起使用并添加 frontend.php 但仍然给我同样的错误如何解决? 如果有人知道请告诉我????

【问题讨论】:

  • 你使用的是自定义验证,还是默认的 Laravel 邮件验证?另外:确切的错误是什么,因为如果您使用的是 API,它可能与用户令牌有关,因为 API 需要存在用户令牌。
  • 点击重定向链接给出 {"error":"unauthenticated.","code":401}
  • 使用5.7功能不简单,只是复制了web路由控制器并更改了功能
  • 如果您使用默认的 Laravel 电子邮件验证,您应该使用本指南中描述的步骤:laravel.com/docs/5.7/verification。这样,您可以确定您没有错过任何文件。如果你只是复制粘贴一些文件,你可能会错过一些东西(我猜这里就是这种情况)
  • 但我在哪里搞砸了?请让我知道?

标签: laravel postman laravel-passport


【解决方案1】:

您应该删除 Route::middleware('auth:api')-&gt;group(function () { 代码,因为这要求 URL 具有 API 令牌 (http://example.com/?api_token=[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) 中的步骤启用电子邮件验证。通过从互联网上复制和粘贴代码,您很容易出错。

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 2019-05-20
    • 2020-11-15
    • 2019-06-14
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多