注册后进行Laravel + Angular电子邮件验证

时间:2020-11-12 14:50:32

标签: angular laravel email-verification

public function signup(SignUpRequest $request) {
    $user = User::create($request->all());

    $verification_code = str_random(30); //Generate verification code
    DB::table('user_verifications')->insert(['user_id'=>$user->id,'token'=>$verification_code]);

    $email = $user->email;
    $name = $user->name;

    $subject = "Please verify your email address.";
    Mail::send('email.verify', ['name' => $name, 'verification_code' => $verification_code],
        function($mail) use ($email, $name, $subject){
            $mail->from(getenv('MAIL_FROM_ADDRESS'), "hello@example.com");
            $mail->to($email, $name);
            $mail->subject($subject);
        });

        return response()->json(['success'=> true, 'message'=> 'Thanks for signing up! Please check your email to complete your registration.']);
}

/**
 * API Verify User
 *
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function verifyUser()
{
    if (isset($_GET['verification_code']))
    {
        $verification_code = $_GET['verification_code'];

        $check = DB::table('user_verifications')->where('token',$verification_code)->first();

        if(!is_null($check)){
            $user = User::find($check->user_id);

            if($user->is_verified == 1){
               return redirect('login');
            }

            $user->update(['is_verified' => 1]);
            DB::table('user_verifications')->where('token',$verification_code)->delete();
            return redirect('login');
        }

        return redirect('login');
    }

}

因此具有注册和verifyUser功能,可以向用户发送电子邮件,用户必须单击以验证其帐户,这是他们收到的URL

http:// localhost:8000 / user / verify?verification_code = 6ZSKlRt8si4WNoVNr0o7RT0ambZ4FV

并且我尝试检索verify_code变量并验证用户,但我找不到了

enter image description here

0 个答案:

没有答案