-
Notifications
You must be signed in to change notification settings - Fork 0
- Auth #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
- Auth #3
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d0248d7
Add login, logout, register
KacperWalenga 1c4c0c1
Add send reset password email
KacperWalenga dc8d5da
fix code style
KacperWalenga d180308
add password reset
KacperWalenga 584e993
add change password
KacperWalenga 7c98d71
fix code style
KacperWalenga 6d91b07
Update app/Http/Controllers/Auth/PasswordController.php
KacperWalenga f65d1b9
Update routes/api.php
KacperWalenga 51ff2e5
refactor ForgotPasswordNotification.php
KacperWalenga 7e70d82
Merge remote-tracking branch 'origin/Auth' into Auth
KacperWalenga b50a99e
Update app/Http/Requests/Auth/ForgotPasswordRequest.php
KacperWalenga a002af5
Update app/Http/Controllers/Auth/LogoutController.php
KacperWalenga d322d6a
Update app/Http/Requests/Auth/ChangePasswordRequest.php
KacperWalenga e705077
Update app/Http/Requests/Auth/ForgotPasswordRequest.php
KacperWalenga 9e6d1fa
Update app/Http/Requests/Auth/ResetPasswordRequest.php
KacperWalenga 418aa11
Update app/Notifications/ForgotPasswordNotification.php
KacperWalenga efba8a7
Update app/Http/Requests/Auth/ChangePasswordRequest.php
KacperWalenga 37287ee
Update app/Http/Requests/Auth/ForgotPasswordRequest.php
KacperWalenga b0f7b9a
Update app/Http/Requests/Auth/LoginRequest.php
KacperWalenga 16d9ceb
Update app/Notifications/ForgotPasswordNotification.php
KacperWalenga 6171582
Update app/Notifications/ForgotPasswordNotification.php
KacperWalenga 490baf5
Update app/Notifications/ForgotPasswordNotification.php
KacperWalenga 25e7b9e
Update app/Http/Requests/Auth/LoginRequest.php
KacperWalenga 7d3ded7
Update app/Http/Requests/Auth/ResetPasswordRequest.php
KacperWalenga 3b865eb
Update app/Http/Requests/Auth/RegisterRequest.php
KacperWalenga 98936b9
Update app/Http/Requests/Auth/RegisterRequest.php
KacperWalenga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Auth; | ||
|
|
||
| use Illuminate\Support\Facades\Hash; | ||
| use Strava\Models\User; | ||
|
|
||
| class ChangePasswordAction | ||
| { | ||
| public function execute(User $user, string $currentPassword, string $newPassword): bool | ||
| { | ||
| if (!Hash::check($currentPassword, $user->password)) { | ||
| return false; | ||
| } | ||
|
|
||
| $hashedPassword = Hash::make($newPassword); | ||
|
|
||
| $user->password = $hashedPassword; | ||
| $user->save(); | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Actions\Auth; | ||
|
|
||
| use Illuminate\Auth\Events\PasswordReset; | ||
| use Illuminate\Support\Facades\Hash; | ||
| use Illuminate\Support\Facades\Password; | ||
| use Illuminate\Support\Str; | ||
| use Strava\Models\User; | ||
|
|
||
| class ResetPasswordAction | ||
| { | ||
| public function execute(array $credentials): bool | ||
| { | ||
| $status = Password::reset($credentials, function (User $user, string $password): void { | ||
| $user->forceFill([ | ||
| "password" => Hash::make($password), | ||
| ])->setRememberToken(Str::random(60)); | ||
|
|
||
| $user->save(); | ||
|
|
||
| event(new PasswordReset($user)); | ||
| }); | ||
|
|
||
| return $status === Password::PASSWORD_RESET; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Auth; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Strava\Http\Requests\Auth\LoginRequest; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class LoginController extends Controller | ||
| { | ||
| public function login(LoginRequest $request): JsonResponse | ||
| { | ||
| $credentials = $request->validated(); | ||
|
|
||
| if (!Auth::attempt($credentials)) { | ||
| return response()->json([], Response::HTTP_FORBIDDEN); | ||
| } | ||
|
|
||
| $user = Auth::user(); | ||
| $token = $user->createToken("api-token")->plainTextToken; | ||
|
|
||
| return response()->json([ | ||
| "token" => $token, | ||
| "user_id" => $user->id, | ||
| ], Response::HTTP_OK); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Auth; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class LogoutController extends Controller | ||
| { | ||
| public function logout(Request $request): JsonResponse | ||
| { | ||
| $user = $request->user(); | ||
| $token = $user->currentAccessToken(); | ||
|
|
||
| $token->delete(); | ||
|
|
||
| return response()->json([], Response::HTTP_OK); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Auth; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Support\Facades\Password; | ||
| use Strava\Actions\Auth\ChangePasswordAction; | ||
| use Strava\Actions\Auth\ResetPasswordAction; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Strava\Http\Requests\Auth\ChangePasswordRequest; | ||
| use Strava\Http\Requests\Auth\ForgotPasswordRequest; | ||
| use Strava\Http\Requests\Auth\ResetPasswordRequest; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class PasswordController extends Controller | ||
| { | ||
| public function sendResetEmail(ForgotPasswordRequest $request): JsonResponse | ||
| { | ||
| $validated = $request->validated(); | ||
|
|
||
| Password::sendResetLink($validated); | ||
|
|
||
| return response()->json([], Response::HTTP_OK); | ||
| } | ||
|
|
||
| public function resetPassword(ResetPasswordRequest $request, ResetPasswordAction $resetPasswordAction): JsonResponse | ||
| { | ||
| $validated = $request->validated(); | ||
| $success = $resetPasswordAction->execute($validated); | ||
|
|
||
| return $success | ||
| ? response()->json([], Response::HTTP_OK) | ||
| : response()->json([], Response::HTTP_BAD_REQUEST); | ||
| } | ||
|
|
||
| public function changePassword(ChangePasswordRequest $request, ChangePasswordAction $changePasswordAction): JsonResponse | ||
| { | ||
| $user = $request->user(); | ||
| $validated = $request->validated(); | ||
|
|
||
| $currentPassword = $validated["current_password"]; | ||
| $newPassword = $validated["password"]; | ||
|
|
||
| $success = $changePasswordAction->execute( | ||
| $user, | ||
| $currentPassword, | ||
| $newPassword, | ||
| ); | ||
|
|
||
| return $success ? | ||
| response()->json([], Response::HTTP_OK) | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : response()->json([], Response::HTTP_FORBIDDEN); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Controllers\Auth; | ||
|
|
||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Support\Facades\Hash; | ||
| use Strava\Http\Controllers\Controller; | ||
| use Strava\Http\Requests\Auth\RegisterRequest; | ||
| use Strava\Models\User; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
|
|
||
| class RegisterController extends Controller | ||
| { | ||
| public function register(RegisterRequest $request): JsonResponse | ||
| { | ||
| $validated = $request->validated(); | ||
|
|
||
| $user = new User($validated); | ||
| $user->password = Hash::make($validated["password"]); | ||
| $user->save(); | ||
|
|
||
| return response()->json([], Response::HTTP_CREATED); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests\Auth; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class ChangePasswordRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function authorize(): bool | ||
| { | ||
| return auth()->check(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "current_password" => ["required", "string"], | ||
| "password" => ["required", "string", "min:8", "confirmed"], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests\Auth; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class ForgotPasswordRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "email" => ["email", "string", "required", "max:255"], | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests\Auth; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class LoginRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "email" => ["required", "string", "email", "max:255"], | ||
| "password" => ["required", "string", "max:255"], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests\Auth; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class RegisterRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "name" => ["required", "string", "max:255"], | ||
| "email" => ["required", "string", "email:rfc,dns", "max:255", "unique:users"], | ||
| "password" => ["required", "string", "min:8", "max:255", "confirmed"], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Strava\Http\Requests\Auth; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class ResetPasswordRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Determine if the user is authorized to make this request. | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public function authorize(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the validation rules that apply to the request. | ||
| * | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "token" => ["required", "string"], | ||
| "email" => ["required", "string", "email"], | ||
| "password" => ["required", "string", "min:8", "max:255", "confirmed"], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.