Skip to content

Commit

Permalink
feat: auth
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardotglobal committed Jul 23, 2024
1 parent dfd3aad commit 46a8468
Show file tree
Hide file tree
Showing 25 changed files with 1,093 additions and 54 deletions.
220 changes: 220 additions & 0 deletions .phpstorm.meta.php

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions app/Actions/Fortify/PasswordValidationRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Actions\Fortify;

use Illuminate\Validation\Rules\Password;

trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
*/
protected function passwordRules(): array
{
return ["required", "string", Password::default(), "confirmed"];
}
}
37 changes: 37 additions & 0 deletions app/Actions/Fortify/UpdateUserPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;

class UpdateUserPassword implements UpdatesUserPasswords
{
use PasswordValidationRules;

/**
* Validate and update the user's password.
*
* @param array<string, string> $input
* @throws ValidationException
*/
public function update(User $user, array $input): void
{
Validator::make(
$input,
[
"current_password" => ["required", "string", "current_password:web"],
"password" => $this->passwordRules(),
],
[
"current_password.current_password" => __("The provided password does not match your current password."),
]
)
->validateWithBag("updatePassword");

$user->forceFill(["password" => Hash::make($input["password"])])->save();
}
}
40 changes: 40 additions & 0 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;

class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
/**
* Validate and update the given user's profile information.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input,
[
"name" => ["required", "string", "max:255"],

"username" => [
"required",
"string",
"max:255",
Rule::unique("users")->ignore($user->id),
],
]
)
->validateWithBag("updateProfileInformation");

$user->forceFill([
"name" => $input["name"],
"email" => $input["email"],
])
->save();
}
}
8 changes: 0 additions & 8 deletions app/Http/Controllers/Controller.php

This file was deleted.

20 changes: 20 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Jdenticon\Identicon;

class UserController
{
public function identicon(string $username): Response
{
$icon = new Identicon();
$icon->setValue($username);
$icon->setSize(100);

return response($icon->getImageData("svg"), 200)
->header("Cache-Control", "max-age=604800")
->header("Content-Type", "image/svg+xml");
}
}
2 changes: 2 additions & 0 deletions app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
*
*
* @property int|null $id
* @property string|null $timestamp
* @property string|null $author_sort
Expand Down
3 changes: 2 additions & 1 deletion app/Models/BookTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
*
*
*
* @property string $name the name of the tag
* @property int $book the id of the book this tag belongs to
Expand All @@ -18,6 +18,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|BookTag whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|BookTag whereLink($value)
* @method static \Illuminate\Database\Eloquent\Builder|BookTag whereName($value)
* @property string $link
* @mixin \Eloquent
*/
class BookTag extends Model
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Series.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
*
*
*
* @property string $name the name of the series
* @property string $sort the name of the series in a sortable format
Expand All @@ -22,6 +22,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|Series whereLink($value)
* @method static \Illuminate\Database\Eloquent\Builder|Series whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Series whereSort($value)
* @property-read Collection<int, \App\Models\Book> $books
* @mixin \Eloquent
*/
class Series extends Model
Expand Down
36 changes: 36 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
/**
*
*
* @property int $id
* @property string $name
* @property string $username
* @property string $password
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|User query()
* @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUsername($value)
* @mixin \Eloquent
*/
class User extends Authenticatable
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = "users";
}
51 changes: 51 additions & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Providers;

use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Fortify\Fortify;

class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);

Fortify::authenticateUsing(function (Request $request) {
$user = User::whereUsername($request->username)->first();

if ($user && Hash::check($request->password, $user->password)) {
return $user;
}

return null;
});

Fortify::loginView(fn () => view("login"));

RateLimiter::for("login", function (Request $request) {
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())) . "|" . $request->ip());

return Limit::perMinute(5)->by($throttleKey);
});
}
}
4 changes: 3 additions & 1 deletion app/View/Components/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Header extends Component
*/
public function render(): View|Closure|string
{
return view("components.header");
return view("components.header", [
"username" => request()->user()->username,
]);
}
}
26 changes: 26 additions & 0 deletions app/View/Components/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Layout extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.layout');
}
}
5 changes: 3 additions & 2 deletions app/View/Pages/BookPage.php → app/View/Pages/BooksPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use App\Http\Resources\BookResource;
use App\Interfaces\PageComponent;
use App\Models\Book;
use App\Models\User;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class BookPage extends PageComponent
class BooksPage extends PageComponent
{
/**
* Get the view / contents that represent the component.
Expand All @@ -18,7 +19,7 @@ protected function _render(Request $request): View|Closure|string
{
return view("books", [
"title" => "All books",
"books" => BookResource::collection(Book::all())->toArray($request)
"books" => BookResource::collection(Book::all())->toArray($request),
]);
}
}
1 change: 1 addition & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
App\Providers\AppServiceProvider::class,
App\Providers\FortifyServiceProvider::class,
];
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"license": "MIT",
"require": {
"php": "^8.2",
"ext-pdo": "*",
"jdenticon/jdenticon": "^1.0",
"laravel/fortify": "^1.21",
"laravel/framework": "^11.9",
"laravel/tinker": "^2.9",
"ext-pdo": "*"
"laravel/tinker": "^2.9"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^3.1",
Expand Down
Loading

0 comments on commit 46a8468

Please sign in to comment.