单击电子邮件中包含的验证电子邮件地址按钮后如何填充数据库列?

发布于 2025-01-15 23:11:27 字数 3288 浏览 0 评论 0原文

我想知道当用户点击正在发送的验证电子邮件中包含的验证电子邮件地址时,如何在 email_verified_at 列中填充时间戳。

我认为这是一件简单的事情,因此我在 EmailVerificationController.php 文件中进行了尝试,但它不起作用。

我做错了什么以及如何解决它?如有任何反馈,我们将不胜感激:)

这是api.php

Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {

Route::post('email/verification-notification', [EmailVerificationController::class, 'sendVerificationEmail']);

Route::get('email/verify/{id}/{hash}',            function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/');
})->name('verification.verify');

});

,这是EmailVerificationController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;

class EmailVerificationController extends Controller
{
    public function sendVerificationEmail(Request $request)
    {

        if ($request->user()->hasVerifiedEmail()) {
            return [
                'message' => 'Already Verified'
            ];
        }

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

        return ['status' => 'verification-link-sent'];
    }

    public function verify(EmailVerificationRequest $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return [
                'message' => 'Email already verified'
            ];
        } 

        if ($request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
            $time             = Carbon::now();
            $timeStamp        = $time->toDateTimeString();

            User::create([
               'email_verified_at' => $timeStamp
            ]);
        }

        return [
            'message'=>'Email has been verified'
        ];
    }
}

这是AuthController.php

    public function register(Request $request)
    {

        try {
            $request->validate([
                'name'           => 'required',
                'email'          => 'required|email|unique:users',
                'age'            => 'required',
                'password'       => 'required|min:6',
            ]);

            $data                = $request->all();

            $dbData = [
                'name'           => $data['name'],
                'email'          => $data['email'],
                'age'            => $data['age'],
                'password'       => Hash::make($data['password'])
            ];

            $user                = User::create($dbData);

            event(new Registered($user));

            return response()->json([
                "status"         => true,
                "message"        => "Registered Successfully!",
                'token'          => $user->createToken('tokens')->plainTextToken
            ]);
        } catch(\Exception $e) {
            Log::error($e->getMessage());
            throw new \Exception($e->getMessage(), $e->getCode(), $e);
        }

    }

I'd like to know how I can populate the email_verified_at column with a timestamp upon the user clicking on the Verify Email Address contained in the verification email that's being sent.

I thought it'd be a straightforward thing to do hence my attempt below in the EmailVerificationController.php file but it didn't work.

What am I doing wrong and how can I fix it? Any feedback is appreciated :)

Here's api.php

Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {

Route::post('email/verification-notification', [EmailVerificationController::class, 'sendVerificationEmail']);

Route::get('email/verify/{id}/{hash}',            function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/');
})->name('verification.verify');

});

Here's EmailVerificationController.php:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;

class EmailVerificationController extends Controller
{
    public function sendVerificationEmail(Request $request)
    {

        if ($request->user()->hasVerifiedEmail()) {
            return [
                'message' => 'Already Verified'
            ];
        }

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

        return ['status' => 'verification-link-sent'];
    }

    public function verify(EmailVerificationRequest $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return [
                'message' => 'Email already verified'
            ];
        } 

        if ($request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
            $time             = Carbon::now();
            $timeStamp        = $time->toDateTimeString();

            User::create([
               'email_verified_at' => $timeStamp
            ]);
        }

        return [
            'message'=>'Email has been verified'
        ];
    }
}

Here's AuthController.php

    public function register(Request $request)
    {

        try {
            $request->validate([
                'name'           => 'required',
                'email'          => 'required|email|unique:users',
                'age'            => 'required',
                'password'       => 'required|min:6',
            ]);

            $data                = $request->all();

            $dbData = [
                'name'           => $data['name'],
                'email'          => $data['email'],
                'age'            => $data['age'],
                'password'       => Hash::make($data['password'])
            ];

            $user                = User::create($dbData);

            event(new Registered($user));

            return response()->json([
                "status"         => true,
                "message"        => "Registered Successfully!",
                'token'          => $user->createToken('tokens')->plainTextToken
            ]);
        } catch(\Exception $e) {
            Log::error($e->getMessage());
            throw new \Exception($e->getMessage(), $e->getCode(), $e);
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2025-01-22 23:11:27

您需要在 API 注册中实现 Laravel 的必须验证电子邮件功能

按照本教程操作

You need to implement Laravel’s Must Verify Email feature in the API registration

Follow this tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文