Hello everyone,
I have a problem with E-Mail verification. I followed all the steps from the Laravel 11 documentation to set up the verification. Only difference is, that I do not use Blade, but a ReactJS (currently simulated by Postman) frontend. I got everything working, until the link has to be clicked. Then it says message: unauthenticated.
This is what I have:
Routing
api.php
Route::get('/email/verify', [AuthController::class, 'notice'])->middleware('auth')->name('verification.notice');
Route::get('/email/verify/{id}/{hash}', [AuthController::class, 'verify'])->middleware(['auth', 'signed'])->name('verification.verify');
// Route::get('/email/verify/{id}/{hash}', [AuthController::class, 'verify'])->middleware(['auth:sanctum', 'signed'])->name('verification.verify');
SignUp, Notice, Verify
AuthController.php
public function signup(SignUpRequest $request)
{
$data = $request->validated();
$user = User::create([...]);
event(new Registered($user));
return response()->json(['user' => 'verification e-mail sent'], 201);
}
public function notice(Request $request)
{
return $request->user()->hasVerifiedEmail()
? response()->json(['user' => 'mail sent'], 201)
: response()->json(['user' => 'mail error'], 401);
}
public function verify(EmailVerificationRequest $request)
{
$request->fulfill();
return redirect()->away(www.domain.com);
}
As I understand, the auth and signed middleware should allow me to access the /email/verify/{id}/{hash} route even if I am not logged in. But somehow I get an unauthenticated message if I try to verify the link in Postman.
I tried to log the user in when he signs up and use auth:sanctum instead. But this only works, if I verify the mail on the same browser/Postman-instance as the SignUp. Which makes sense. But I want to allow verification from various devices.
Am I missing something? If you need more information, just tell me. Thanks for your tips and help in advance.