Skip to content

Commit 419a373

Browse files
committed
Add Fortify
1 parent c92e9ba commit 419a373

File tree

6 files changed

+234
-2
lines changed

6 files changed

+234
-2
lines changed

app/Models/User.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99
use Illuminate\Support\Str;
10+
use Laravel\Fortify\TwoFactorAuthenticatable;
1011

1112
class User extends Authenticatable
1213
{
1314
/** @use HasFactory<\Database\Factories\UserFactory> */
14-
use HasFactory, Notifiable;
15+
use HasFactory, Notifiable, TwoFactorAuthenticatable;
1516

1617
/**
1718
* The attributes that are mass assignable.
@@ -55,7 +56,7 @@ public function initials(): string
5556
return Str::of($this->name)
5657
->explode(' ')
5758
->take(2)
58-
->map(fn ($word) => Str::substr($word, 0, 1))
59+
->map(fn($word) => Str::substr($word, 0, 1))
5960
->implode('');
6061
}
6162
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Actions\Fortify\CreateNewUser;
6+
use App\Actions\Fortify\ResetUserPassword;
7+
use App\Actions\Fortify\UpdateUserPassword;
8+
use App\Actions\Fortify\UpdateUserProfileInformation;
9+
use Illuminate\Cache\RateLimiting\Limit;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Support\Facades\RateLimiter;
12+
use Illuminate\Support\ServiceProvider;
13+
use Illuminate\Support\Str;
14+
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
15+
use Laravel\Fortify\Fortify;
16+
17+
class FortifyServiceProvider extends ServiceProvider
18+
{
19+
/**
20+
* Register any application services.
21+
*/
22+
public function register(): void
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Bootstrap any application services.
29+
*/
30+
public function boot(): void
31+
{
32+
RateLimiter::for('two-factor', function (Request $request) {
33+
return Limit::perMinute(5)->by($request->session()->get('login.id'));
34+
});
35+
}
36+
}

bootstrap/providers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
return [
44
App\Providers\AppServiceProvider::class,
5+
App\Providers\FortifyServiceProvider::class,
56
App\Providers\VoltServiceProvider::class,
67
];

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": "^8.2",
13+
"laravel/fortify": "^1.30",
1314
"laravel/framework": "^12.0",
1415
"laravel/tinker": "^2.10.1",
1516
"livewire/flux": "^2.1.1",

config/fortify.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
use Laravel\Fortify\Features;
4+
5+
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Fortify Guard
10+
|--------------------------------------------------------------------------
11+
|
12+
| Here you may specify which authentication guard Fortify will use while
13+
| authenticating users. This value should correspond with one of your
14+
| guards that is already present in your "auth" configuration file.
15+
|
16+
*/
17+
18+
'guard' => 'web',
19+
20+
/*
21+
|--------------------------------------------------------------------------
22+
| Fortify Password Broker
23+
|--------------------------------------------------------------------------
24+
|
25+
| Here you may specify which password broker Fortify can use when a user
26+
| is resetting their password. This configured value should match one
27+
| of your password brokers setup in your "auth" configuration file.
28+
|
29+
*/
30+
31+
'passwords' => 'users',
32+
33+
/*
34+
|--------------------------------------------------------------------------
35+
| Username / Email
36+
|--------------------------------------------------------------------------
37+
|
38+
| This value defines which model attribute should be considered as your
39+
| application's "username" field. Typically, this might be the email
40+
| address of the users but you are free to change this value here.
41+
|
42+
| Out of the box, Fortify expects forgot password and reset password
43+
| requests to have a field named 'email'. If the application uses
44+
| another name for the field you may define it below as needed.
45+
|
46+
*/
47+
48+
'username' => 'email',
49+
50+
'email' => 'email',
51+
52+
/*
53+
|--------------------------------------------------------------------------
54+
| Lowercase Usernames
55+
|--------------------------------------------------------------------------
56+
|
57+
| This value defines whether usernames should be lowercased before saving
58+
| them in the database, as some database system string fields are case
59+
| sensitive. You may disable this for your application if necessary.
60+
|
61+
*/
62+
63+
'lowercase_usernames' => true,
64+
65+
/*
66+
|--------------------------------------------------------------------------
67+
| Home Path
68+
|--------------------------------------------------------------------------
69+
|
70+
| Here you may configure the path where users will get redirected during
71+
| authentication or password reset when the operations are successful
72+
| and the user is authenticated. You are free to change this value.
73+
|
74+
*/
75+
76+
'home' => '/home',
77+
78+
/*
79+
|--------------------------------------------------------------------------
80+
| Fortify Routes Prefix / Subdomain
81+
|--------------------------------------------------------------------------
82+
|
83+
| Here you may specify which prefix Fortify will assign to all the routes
84+
| that it registers with the application. If necessary, you may change
85+
| subdomain under which all of the Fortify routes will be available.
86+
|
87+
*/
88+
89+
'prefix' => '',
90+
91+
'domain' => null,
92+
93+
/*
94+
|--------------------------------------------------------------------------
95+
| Fortify Routes Middleware
96+
|--------------------------------------------------------------------------
97+
|
98+
| Here you may specify which middleware Fortify will assign to the routes
99+
| that it registers with the application. If necessary, you may change
100+
| these middleware but typically this provided default is preferred.
101+
|
102+
*/
103+
104+
'middleware' => ['web'],
105+
106+
/*
107+
|--------------------------------------------------------------------------
108+
| Rate Limiting
109+
|--------------------------------------------------------------------------
110+
|
111+
| By default, Fortify will throttle logins to five requests per minute for
112+
| every email and IP address combination. However, if you would like to
113+
| specify a custom rate limiter to call then you may specify it here.
114+
|
115+
*/
116+
117+
'limiters' => [
118+
'login' => 'login',
119+
'two-factor' => 'two-factor',
120+
],
121+
122+
/*
123+
|--------------------------------------------------------------------------
124+
| Register View Routes
125+
|--------------------------------------------------------------------------
126+
|
127+
| Here you may specify if the routes returning views should be disabled as
128+
| you may not need them when building your own application. This may be
129+
| especially true if you're writing a custom single-page application.
130+
|
131+
*/
132+
133+
'views' => true,
134+
135+
/*
136+
|--------------------------------------------------------------------------
137+
| Features
138+
|--------------------------------------------------------------------------
139+
|
140+
| Some of the Fortify features are optional. You may disable the features
141+
| by removing them from this array. You're free to only remove some of
142+
| these features or you can even remove all of these if you need to.
143+
|
144+
*/
145+
146+
'features' => [
147+
Features::registration(),
148+
Features::resetPasswords(),
149+
// Features::emailVerification(),
150+
Features::updateProfileInformation(),
151+
Features::updatePasswords(),
152+
Features::twoFactorAuthentication([
153+
'confirm' => true,
154+
'confirmPassword' => true,
155+
// 'window' => 0,
156+
]),
157+
],
158+
159+
];
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('users', function (Blueprint $table) {
15+
$table->text('two_factor_secret')->after('password')->nullable();
16+
$table->text('two_factor_recovery_codes')->after('two_factor_secret')->nullable();
17+
$table->timestamp('two_factor_confirmed_at')->after('two_factor_recovery_codes')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::table('users', function (Blueprint $table) {
27+
$table->dropColumn([
28+
'two_factor_secret',
29+
'two_factor_recovery_codes',
30+
'two_factor_confirmed_at',
31+
]);
32+
});
33+
}
34+
};

0 commit comments

Comments
 (0)