Laravel API注册电子邮件验证

时间:2020-04-14 13:21:39

标签: laravel

我正在尝试在Laravel API中进行验证后实施Laravel电子邮件验证

但是我无法在注册后发送它。但是我可以手动发送

在我的API路由中,我有这个

Route::get('/email/resend', 'Api\VerificationController@resend')->name('verification.resend');
Route::get('/email/verify/{id}/{hash}', 'Api\VerificationController@verify')->name('verification.verify');

在我的控制器中,我有这个

<?php

namespace App\Http\Controllers\Api;

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;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api')->only('resend');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }

    /**
     * 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(['message'=>'Already verified']);
        }

        $request->user()->sendEmailVerificationNotification();

        if ($request->wantsJson()) {
            return response(['message' => 'Email Sent']);
        }

        return back()->with('resent', true);
    }


    /**
     * Mark the authenticated user's email address as verified.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function verify(Request $request)
    {
        auth()->loginUsingId($request->route('id'));

        if ($request->route('id') != $request->user()->getKey()) {
            throw new AuthorizationException;
        }

        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']);

    }


}

我希望能够在用户注册其在网络上的工作方式时自动发送

1 个答案:

答案 0 :(得分:0)

我通过此操作将其修复

public function register(Request $request)
{
/*     $validatedData = $request->validate([
         'name'=>'required|max:55',
         'email'=>'email|required|unique:users',
         'password'=>'required|confirmed'
     ]);

     $validatedData['password'] = bcrypt($request->password);
     $user = User::create($validatedData);
     $accessToken = $user->createToken('authToken')->accessToken;
     return response(['user'=> $user, 'access_token'=> $accessToken]);
    */
    $request->validate([
        'name' => 'required|max:55',
        'email' => 'required|string|email|unique:users',
        'password' => 'required|string|confirmed',
    //    'phone_number' => 'required|string|min:6',

    ]);
    $user = new User([
        'name' => $request->name,
        'email' => $request->email,
        'password' => bcrypt($request->password),
   //     'phone_number' => $request['phone_number'],
    ]);
    $user->save();
    $accessToken = $user->createToken('authToken')->accessToken;

        Auth::login($user,true);
        $user->sendEmailVerificationNotification();

        $success = 'Please confirm yourself by clicking on verify user button sent to you on your email';

        return response(['user'=> $user, 'access_token'=> $accessToken, 'message' =>  $success,]);

}