Skip to content

Commit 4144910

Browse files
committed
Merge branch 'feature/ui' into 'master'
add UI for authentication and authorization See merge request mark-heramis/laravel-restful-api!255
2 parents b7da84f + 014bb75 commit 4144910

26 files changed

+24160
-3315
lines changed

app/Actions/Fortify/CreateNewUser.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Hash;
7+
use Illuminate\Support\Facades\Validator;
8+
use Illuminate\Validation\Rule;
9+
use Laravel\Fortify\Contracts\CreatesNewUsers;
10+
11+
class CreateNewUser implements CreatesNewUsers
12+
{
13+
use PasswordValidationRules;
14+
15+
/**
16+
* Validate and create a newly registered user.
17+
*
18+
* @param array<string, string> $input
19+
*/
20+
public function create(array $input): User
21+
{
22+
Validator::make($input, [
23+
'name' => ['required', 'string', 'max:255'],
24+
'email' => [
25+
'required',
26+
'string',
27+
'email',
28+
'max:255',
29+
Rule::unique(User::class),
30+
],
31+
'password' => $this->passwordRules(),
32+
])->validate();
33+
34+
return User::create([
35+
'name' => $input['name'],
36+
'email' => $input['email'],
37+
'password' => Hash::make($input['password']),
38+
]);
39+
}
40+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use Laravel\Fortify\Rules\Password;
6+
7+
trait PasswordValidationRules
8+
{
9+
/**
10+
* Get the validation rules used to validate passwords.
11+
*
12+
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
13+
*/
14+
protected function passwordRules(): array
15+
{
16+
return ['required', 'string', new Password, 'confirmed'];
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Hash;
7+
use Illuminate\Support\Facades\Validator;
8+
use Laravel\Fortify\Contracts\ResetsUserPasswords;
9+
10+
class ResetUserPassword implements ResetsUserPasswords
11+
{
12+
use PasswordValidationRules;
13+
14+
/**
15+
* Validate and reset the user's forgotten password.
16+
*
17+
* @param array<string, string> $input
18+
*/
19+
public function reset(User $user, array $input): void
20+
{
21+
Validator::make($input, [
22+
'password' => $this->passwordRules(),
23+
])->validate();
24+
25+
$user->forceFill([
26+
'password' => Hash::make($input['password']),
27+
])->save();
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Hash;
7+
use Illuminate\Support\Facades\Validator;
8+
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
9+
10+
class UpdateUserPassword implements UpdatesUserPasswords
11+
{
12+
use PasswordValidationRules;
13+
14+
/**
15+
* Validate and update the user's password.
16+
*
17+
* @param array<string, string> $input
18+
*/
19+
public function update(User $user, array $input): void
20+
{
21+
Validator::make($input, [
22+
'current_password' => ['required', 'string', 'current_password:web'],
23+
'password' => $this->passwordRules(),
24+
], [
25+
'current_password.current_password' => __('The provided password does not match your current password.'),
26+
])->validateWithBag('updatePassword');
27+
28+
$user->forceFill([
29+
'password' => Hash::make($input['password']),
30+
])->save();
31+
}
32+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Actions\Fortify;
4+
5+
use App\Models\User;
6+
use Illuminate\Contracts\Auth\MustVerifyEmail;
7+
use Illuminate\Support\Facades\Validator;
8+
use Illuminate\Validation\Rule;
9+
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
10+
11+
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
12+
{
13+
/**
14+
* Validate and update the given user's profile information.
15+
*
16+
* @param array<string, string> $input
17+
*/
18+
public function update(User $user, array $input): void
19+
{
20+
Validator::make($input, [
21+
'name' => ['required', 'string', 'max:255'],
22+
23+
'email' => [
24+
'required',
25+
'string',
26+
'email',
27+
'max:255',
28+
Rule::unique('users')->ignore($user->id),
29+
],
30+
])->validateWithBag('updateProfileInformation');
31+
32+
if ($input['email'] !== $user->email &&
33+
$user instanceof MustVerifyEmail) {
34+
$this->updateVerifiedUser($user, $input);
35+
} else {
36+
$user->forceFill([
37+
'name' => $input['name'],
38+
'email' => $input['email'],
39+
])->save();
40+
}
41+
}
42+
43+
/**
44+
* Update the given verified user's profile information.
45+
*
46+
* @param array<string, string> $input
47+
*/
48+
protected function updateVerifiedUser(User $user, array $input): void
49+
{
50+
$user->forceFill([
51+
'name' => $input['name'],
52+
'email' => $input['email'],
53+
'email_verified_at' => null,
54+
])->save();
55+
56+
$user->sendEmailVerificationNotification();
57+
}
58+
}

app/Http/Controllers/HomeController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public function index()
2222
{
2323
return view('welcome');
2424
}
25+
26+
public function home() {
27+
return view('home');
28+
}
2529
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 Laravel\Fortify\Fortify;
14+
15+
class FortifyServiceProvider extends ServiceProvider
16+
{
17+
/**
18+
* Register any application services.
19+
*/
20+
public function register(): void
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Bootstrap any application services.
27+
*/
28+
public function boot(): void
29+
{
30+
Fortify::createUsersUsing(CreateNewUser::class);
31+
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
32+
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
33+
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
34+
$this->bootView();
35+
RateLimiter::for('login', function (Request $request) {
36+
$email = (string) $request->email;
37+
return Limit::perMinute(5)->by($email.$request->ip());
38+
});
39+
RateLimiter::for('two-factor', function (Request $request) {
40+
return Limit::perMinute(5)->by($request->session()->get('login.id'));
41+
});
42+
}
43+
44+
private function bootView(): void
45+
{
46+
Fortify::loginView(function () {
47+
return view('auth.login');
48+
});
49+
Fortify::registerView(function () {
50+
return view('auth.register');
51+
});
52+
Fortify::requestPasswordResetLinkView(function () {
53+
return view('auth.forgot-password');
54+
});
55+
Fortify::resetPasswordView(function () {
56+
return view('auth.reset-password');
57+
});
58+
}
59+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"fruitcake/laravel-cors": "^3.0",
1717
"gamegos/php-code-sniffer": "^0.6.1",
1818
"guzzlehttp/guzzle": "^7.5",
19+
"laravel/fortify": "^1.16",
1920
"laravel/framework": "^9.3",
2021
"laravel/passport": "^11.0",
2122
"laravel/tinker": "^2.7",

composer.lock

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
* Package Service Providers...
167167
*/
168168
PragmaRX\Google2FALaravel\ServiceProvider::class,
169+
App\Providers\FortifyServiceProvider::class,
169170
/*
170171
* Application Service Providers...
171172
*/

0 commit comments

Comments
 (0)