Skip to content

Commit 1b58bf5

Browse files
author
Tony Lea
committed
Adding confirm password functionality
1 parent 9c55858 commit 1b58bf5

File tree

10 files changed

+244
-20
lines changed

10 files changed

+244
-20
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Validation\ValidationException;
10+
use Inertia\Inertia;
11+
use Inertia\Response;
12+
13+
class ConfirmablePasswordController extends Controller
14+
{
15+
/**
16+
* Show the confirm password view.
17+
*/
18+
public function show(): Response
19+
{
20+
return Inertia::render('Auth/ConfirmPassword');
21+
}
22+
23+
/**
24+
* Confirm the user's password.
25+
*/
26+
public function store(Request $request): RedirectResponse
27+
{
28+
if (! Auth::guard('web')->validate([
29+
'email' => $request->user()->email,
30+
'password' => $request->password,
31+
])) {
32+
throw ValidationException::withMessages([
33+
'password' => __('auth.password'),
34+
]);
35+
}
36+
37+
$request->session()->put('auth.password_confirmed_at', time());
38+
39+
return redirect()->intended(route('dashboard', absolute: false));
40+
}
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Http\Request;
8+
9+
class EmailVerificationNotificationController extends Controller
10+
{
11+
/**
12+
* Send a new email verification notification.
13+
*/
14+
public function store(Request $request): RedirectResponse
15+
{
16+
if ($request->user()->hasVerifiedEmail()) {
17+
return redirect()->intended(route('dashboard', absolute: false));
18+
}
19+
20+
$request->user()->sendEmailVerificationNotification();
21+
22+
return back()->with('status', 'verification-link-sent');
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Http\Request;
8+
use Inertia\Inertia;
9+
use Inertia\Response;
10+
11+
class EmailVerificationPromptController extends Controller
12+
{
13+
/**
14+
* Display the email verification prompt.
15+
*/
16+
public function __invoke(Request $request): RedirectResponse|Response
17+
{
18+
return $request->user()->hasVerifiedEmail()
19+
? redirect()->intended(route('dashboard', absolute: false))
20+
: Inertia::render('Auth/VerifyEmail', ['status' => session('status')]);
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Auth\Events\Verified;
7+
use Illuminate\Foundation\Auth\EmailVerificationRequest;
8+
use Illuminate\Http\RedirectResponse;
9+
10+
class VerifyEmailController extends Controller
11+
{
12+
/**
13+
* Mark the authenticated user's email address as verified.
14+
*/
15+
public function __invoke(EmailVerificationRequest $request): RedirectResponse
16+
{
17+
if ($request->user()->hasVerifiedEmail()) {
18+
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
19+
}
20+
21+
if ($request->user()->markEmailAsVerified()) {
22+
event(new Verified($request->user()));
23+
}
24+
25+
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
26+
}
27+
}

app/Models/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace App\Models;
44

5-
// use Illuminate\Contracts\Auth\MustVerifyEmail;
5+
use Illuminate\Contracts\Auth\MustVerifyEmail;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99

10-
class User extends Authenticatable
10+
class User extends Authenticatable implements MustVerifyEmail
1111
{
1212
/** @use HasFactory<\Database\Factories\UserFactory> */
1313
use HasFactory, Notifiable;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Components
2+
import InputError from "@/Components/InputError";
3+
import AuthLayout from "@/Layouts/AuthLayout";
4+
import { Button } from "@/components/ui/button";
5+
import { Input } from "@/components/ui/input";
6+
import { Label } from "@/components/ui/label";
7+
8+
import { Head, useForm } from '@inertiajs/react';
9+
import { FormEventHandler } from 'react';
10+
11+
export default function ConfirmPassword() {
12+
const { data, setData, post, processing, errors, reset } = useForm({
13+
password: '',
14+
});
15+
16+
const submit: FormEventHandler = (e) => {
17+
e.preventDefault();
18+
19+
post(route('password.confirm'), {
20+
onFinish: () => reset('password'),
21+
});
22+
};
23+
24+
return (
25+
<AuthLayout title="Confirm Your Password" description="This is a secure area of the application. Please confirm your
26+
password before continuing.">
27+
<Head title="Confirm Password" />
28+
29+
30+
<form onSubmit={submit}>
31+
<div class="space-y-6">
32+
<div className="grid gap-2">
33+
34+
<Input
35+
id="password"
36+
type="password"
37+
name="password"
38+
placeholder="Password"
39+
value={data.password}
40+
autoFocus
41+
onChange={(e) => setData('password', e.target.value)}
42+
/>
43+
44+
<InputError message={errors.password} />
45+
</div>
46+
47+
<div className="flex items-center">
48+
<Button className="w-full" disabled={processing}>
49+
Confirm Password
50+
</Button>
51+
</div>
52+
</div>
53+
</form>
54+
</AuthLayout>
55+
);
56+
}

resources/js/Pages/Auth/ForgotPassword.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ export default function ForgotPassword({ status }: { status?: string }) {
5555
<hr />
5656
</form>
5757
<div className="text-center text-sm space-x-1">
58+
59+
<span>Return back to the</span>
5860
<Link href={route("login")} className="underline underline-offset-4">
59-
Click here
61+
login page
6062
</Link>
61-
<span>to return back to the login page</span>
6263
</div>
6364
</AuthLayout>
6465
);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Components
2+
import AuthLayout from "@/Layouts/AuthLayout";
3+
import { Button } from "@/components/ui/button";
4+
5+
import { Head, Link, useForm } from '@inertiajs/react';
6+
import { FormEventHandler } from 'react';
7+
8+
export default function VerifyEmail({ status }: { status?: string }) {
9+
const { post, processing } = useForm({});
10+
11+
const submit: FormEventHandler = (e) => {
12+
e.preventDefault();
13+
14+
post(route('verification.send'));
15+
};
16+
17+
return (
18+
<AuthLayout>
19+
<Head title="Email Verification" />
20+
21+
<div className="mb-4 text-sm text-gray-600">
22+
Thanks for signing up! Before getting started, could you verify
23+
your email address by clicking on the link we just emailed to
24+
you? If you didn't receive the email, we will gladly send you
25+
another.
26+
</div>
27+
28+
{status === 'verification-link-sent' && (
29+
<div className="mb-4 text-sm font-medium text-green-600">
30+
A new verification link has been sent to the email address
31+
you provided during registration.
32+
</div>
33+
)}
34+
35+
<form onSubmit={submit}>
36+
<div className="mt-4 flex items-center justify-between">
37+
<Button disabled={processing}>
38+
Resend Verification Email
39+
</Button>
40+
41+
<Link
42+
href={route('logout')}
43+
method="post"
44+
as="button"
45+
className="rounded-md text-sm text-gray-600 underline hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
46+
>
47+
Log Out
48+
</Link>
49+
</div>
50+
</form>
51+
</AuthLayout>
52+
);
53+
}

routes/auth.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

33
use App\Http\Controllers\Auth\AuthenticatedSessionController;
4-
// use App\Http\Controllers\Auth\ConfirmablePasswordController;
5-
// use App\Http\Controllers\Auth\EmailVerificationNotificationController;
6-
// use App\Http\Controllers\Auth\EmailVerificationPromptController;
4+
use App\Http\Controllers\Auth\ConfirmablePasswordController;
5+
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
6+
use App\Http\Controllers\Auth\EmailVerificationPromptController;
77
use App\Http\Controllers\Auth\NewPasswordController;
88
// use App\Http\Controllers\Auth\PasswordController;
99
use App\Http\Controllers\Auth\PasswordResetLinkController;
1010
use App\Http\Controllers\Auth\RegisteredUserController;
11-
// use App\Http\Controllers\Auth\VerifyEmailController;
11+
use App\Http\Controllers\Auth\VerifyEmailController;
1212
use Illuminate\Support\Facades\Route;
1313

1414
Route::middleware('guest')->group(function () {
@@ -36,21 +36,21 @@
3636
});
3737

3838
Route::middleware('auth')->group(function () {
39-
// Route::get('verify-email', EmailVerificationPromptController::class)
40-
// ->name('verification.notice');
39+
Route::get('verify-email', EmailVerificationPromptController::class)
40+
->name('verification.notice');
4141

42-
// Route::get('verify-email/{id}/{hash}', VerifyEmailController::class)
43-
// ->middleware(['signed', 'throttle:6,1'])
44-
// ->name('verification.verify');
42+
Route::get('verify-email/{id}/{hash}', VerifyEmailController::class)
43+
->middleware(['signed', 'throttle:6,1'])
44+
->name('verification.verify');
4545

46-
// Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
47-
// ->middleware('throttle:6,1')
48-
// ->name('verification.send');
46+
Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
47+
->middleware('throttle:6,1')
48+
->name('verification.send');
4949

50-
// Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
51-
// ->name('password.confirm');
50+
Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
51+
->name('password.confirm');
5252

53-
// Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);
53+
Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);
5454

5555
// Route::put('password', [PasswordController::class, 'update'])->name('password.update');
5656

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
return Inertia::render('Dashboard');
2020
})->middleware(['auth', 'verified'])->name('dashboard');
2121

22-
Route::middleware('auth')->group(function () {
22+
Route::middleware(['auth', 'password.confirm'])->group(function () {
2323
Route::redirect('settings' , 'settings/profile');
2424

2525
Route::get('settings/profile', [ProfileController::class, 'edit'])->name('profile.edit');

0 commit comments

Comments
 (0)