单击电子邮件中包含的验证电子邮件地址按钮后如何填充数据库列?
我想知道当用户点击正在发送的验证电子邮件中包含的验证电子邮件地址
时,如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 API 注册中实现 Laravel 的必须验证电子邮件功能
按照本教程操作
You need to implement Laravel’s Must Verify Email feature in the API registration
Follow this tutorial