I want to allow users to verify email addresses without being logged in.
When the user registers, he gets a verification link. If he clicks on it he gets verified and automatically logged in because he gets logged in at registration. But if a user uses a different device to verify, lets say smartphone, he's unable to directly verify email because he's not logged in. How can I change my controller to be able to do that?
this is my controller:
use Illuminate\Foundation\Auth\EmailVerificationRequest;
public function verify(EmailVerificationRequest $request)
{
if ($request->user()->hasVerifiedEmail()) {
return [
'message' => 'Email already verified'
];
}
if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return [
'message' => 'Email has been verified'
//then front-end redirects user to login page
];
}
and this is API:
Route::get('verify-email/{id}/{hash}', [EmailVerificationController::class, 'verify'])->name('verification.verify')->middleware('auth:sanctum');
If I remove middleware('auth:sanctum');
I get Call to a member function getKey() on null.
Thank you!
CodePudding user response:
public function verify(Request $request)
{
$user = User::findOrFail($request->id);
if ($user->email_verified_at) {
return '';
}
if ($user->markEmailAsVerified()) {
event(new Verified($user));
}
return redirect()->away('app://open'); // The deep link
}