feat: add optional email verification flow
This commit is contained in:
56
app/Http/Controllers/Auth/EmailVerificationController.php
Normal file
56
app/Http/Controllers/Auth/EmailVerificationController.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Auth;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Foundation\Auth\EmailVerificationRequest;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Inertia\Inertia;
|
||||||
|
use Inertia\Response;
|
||||||
|
|
||||||
|
class EmailVerificationController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
if (! config('auth-ui.features.email_verification')) {
|
||||||
|
abort(404);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the email verification notice.
|
||||||
|
*/
|
||||||
|
public function notice(Request $request): Response|RedirectResponse
|
||||||
|
{
|
||||||
|
if ($request->user()->hasVerifiedEmail()) {
|
||||||
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Inertia::render('Auth/VerifyEmail');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the email verification.
|
||||||
|
*/
|
||||||
|
public function verify(EmailVerificationRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
$request->fulfill();
|
||||||
|
|
||||||
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resend the email verification notification.
|
||||||
|
*/
|
||||||
|
public function resend(Request $request): RedirectResponse
|
||||||
|
{
|
||||||
|
if ($request->user()->hasVerifiedEmail()) {
|
||||||
|
return redirect()->intended(config('auth-ui.redirects.login', '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$request->user()->sendEmailVerificationNotification();
|
||||||
|
|
||||||
|
return back()->with('status', 'verification-link-sent');
|
||||||
|
}
|
||||||
|
}
|
||||||
48
resources/js/Pages/Auth/VerifyEmail.vue
Normal file
48
resources/js/Pages/Auth/VerifyEmail.vue
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { useForm } from '@inertiajs/vue3'
|
||||||
|
import { computed } from 'vue'
|
||||||
|
import { useAuth } from '@/composables/useAuth'
|
||||||
|
import AuthLayout from '@/layouts/AuthLayout.vue'
|
||||||
|
|
||||||
|
const { flash } = useAuth()
|
||||||
|
|
||||||
|
const form = useForm('VerifyEmailForm', {})
|
||||||
|
|
||||||
|
function resend() {
|
||||||
|
form.post('/email/verification-notification')
|
||||||
|
}
|
||||||
|
|
||||||
|
const linkSent = computed(() => flash.status === 'verification-link-sent')
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<AuthLayout>
|
||||||
|
<div class="flex flex-col items-center gap-4 text-center">
|
||||||
|
<UIcon name="i-lucide-mail-check" class="text-primary size-12" />
|
||||||
|
<h2 class="text-xl font-semibold">
|
||||||
|
Verify your email
|
||||||
|
</h2>
|
||||||
|
<p class="text-muted text-sm">
|
||||||
|
We've sent a verification link to your email address. Please check your inbox and click the link to verify your account.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<UAlert
|
||||||
|
v-if="linkSent"
|
||||||
|
color="success"
|
||||||
|
icon="i-lucide-check-circle"
|
||||||
|
title="A new verification link has been sent to your email address."
|
||||||
|
class="w-full"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<UButton
|
||||||
|
:loading="form.processing"
|
||||||
|
:disabled="form.processing"
|
||||||
|
variant="soft"
|
||||||
|
block
|
||||||
|
@click="resend"
|
||||||
|
>
|
||||||
|
Resend verification email
|
||||||
|
</UButton>
|
||||||
|
</div>
|
||||||
|
</AuthLayout>
|
||||||
|
</template>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\Auth\CompleteProfileController;
|
use App\Http\Controllers\Auth\CompleteProfileController;
|
||||||
|
use App\Http\Controllers\Auth\EmailVerificationController;
|
||||||
use App\Http\Controllers\Auth\ForgotPasswordController;
|
use App\Http\Controllers\Auth\ForgotPasswordController;
|
||||||
use App\Http\Controllers\Auth\LoginController;
|
use App\Http\Controllers\Auth\LoginController;
|
||||||
use App\Http\Controllers\Auth\RegisterController;
|
use App\Http\Controllers\Auth\RegisterController;
|
||||||
@@ -23,7 +24,7 @@ Route::middleware('guest')->group(function () {
|
|||||||
|
|
||||||
// Socialite routes
|
// Socialite routes
|
||||||
Route::get('auth/{provider}', [SocialiteController::class, 'redirect'])->name('socialite.redirect');
|
Route::get('auth/{provider}', [SocialiteController::class, 'redirect'])->name('socialite.redirect');
|
||||||
Route::get('auth/{provider}/callback', [SocialiteController::class, 'callback'])->name('socialite.callback');
|
Route::get('auth/{provider}/callback', [SocialiteController::class, 'callback'])->name('socialite.callback')->middleware('throttle:10,1');
|
||||||
|
|
||||||
// Complete profile after social login (when username is taken)
|
// Complete profile after social login (when username is taken)
|
||||||
Route::get('complete-profile', [CompleteProfileController::class, 'create'])->name('auth.complete-profile');
|
Route::get('complete-profile', [CompleteProfileController::class, 'create'])->name('auth.complete-profile');
|
||||||
@@ -32,4 +33,9 @@ Route::middleware('guest')->group(function () {
|
|||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
Route::post('logout', [LoginController::class, 'destroy'])->name('logout');
|
Route::post('logout', [LoginController::class, 'destroy'])->name('logout');
|
||||||
|
|
||||||
|
// Email verification routes
|
||||||
|
Route::get('email/verify', [EmailVerificationController::class, 'notice'])->name('verification.notice');
|
||||||
|
Route::get('email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])->middleware('signed')->name('verification.verify');
|
||||||
|
Route::post('email/verification-notification', [EmailVerificationController::class, 'resend'])->middleware('throttle:6,1')->name('verification.send');
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user