-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b238238
commit 4f6413e
Showing
8 changed files
with
242 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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 Keating\Actions; | ||
|
||
use Carbon\Carbon; | ||
use Keating\Models\PasswordlessAttempt; | ||
|
||
class PasswordlessCheckAndClearAttemptAction | ||
{ | ||
public function handle(string $email, string $sessionId): bool | ||
{ | ||
$passwordlessAttempt = PasswordlessAttempt::query() | ||
->where("email", $email) | ||
->where("can_login", true) | ||
->where("expires_at", ">", Carbon::now()) | ||
->where("session_id", $sessionId) | ||
->first(); | ||
|
||
if ($passwordlessAttempt === null) { | ||
return false; | ||
} | ||
|
||
$passwordlessAttempt->delete(); | ||
|
||
return true; | ||
} | ||
} |
This file contains 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
This file contains 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
106 changes: 106 additions & 0 deletions
106
app/Http/Controllers/Public/PasswordlessLoginController.php
This file contains 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,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keating\Http\Controllers\Public; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Auth\AuthManager; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Inertia\Response; | ||
use Keating\Actions\PasswordlessCheckAndClearAttemptAction; | ||
use Keating\Actions\SendLoginLink; | ||
use Keating\Models\PasswordlessAttempt; | ||
use Keating\Models\User; | ||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; | ||
|
||
class PasswordlessLoginController | ||
{ | ||
public function create(): Response | ||
{ | ||
return inertia("Public/PasswordlessLogin", [ | ||
"universityLogo" => asset("cwup-full.png"), | ||
]); | ||
} | ||
|
||
public function store(Request $request, SendLoginLink $action): RedirectResponse | ||
{ | ||
$user = User::query()->where("email", $request->get("email"))->first(); | ||
|
||
if ($user === null) { | ||
return $this->redirectToPasswordlessCreate(); | ||
} | ||
|
||
$time = Carbon::now()->addMinutes(5); | ||
PasswordlessAttempt::query() | ||
->updateOrCreate( | ||
attributes: [ | ||
"email" => $request->get("email"), | ||
], | ||
values: [ | ||
"email" => $request->get("email"), | ||
"session_id" => $request->session()->getId(), | ||
"can_login" => false, | ||
"expires_at" => $time, | ||
], | ||
); | ||
|
||
$action->handle( | ||
email: $request->email, | ||
time: $time, | ||
); | ||
|
||
return $this->redirectToPasswordlessCreate(); | ||
} | ||
|
||
public function check(Request $request, string $email, PasswordlessCheckAndClearAttemptAction $action, AuthManager $auth): JsonResponse | ||
{ | ||
$canLogin = $action->handle( | ||
email: $email, | ||
sessionId: $request->session()->getId(), | ||
); | ||
|
||
if (!$canLogin) { | ||
return new JsonResponse([ | ||
"can_login" => false, | ||
], SymfonyResponse::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
$user = User::query() | ||
->where("email", $email) | ||
->first(); | ||
|
||
$auth->login($user); | ||
$request->session()->regenerate(); | ||
|
||
return new JsonResponse([ | ||
"can_login" => true, | ||
], SymfonyResponse::HTTP_OK); | ||
} | ||
|
||
public function login(Request $request, string $email): RedirectResponse | ||
{ | ||
if (!$request->hasValidSignature()) { | ||
abort(SymfonyResponse::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
PasswordlessAttempt::query() | ||
->where("email", $email) | ||
->where("can_login", false) | ||
->where("expires_at", ">", Carbon::now()) | ||
->update([ | ||
"can_login" => true, | ||
]); | ||
|
||
return redirect()->route("passwordless.create") | ||
->with("success", "Potwierdzono logowanie."); | ||
} | ||
|
||
private function redirectToPasswordlessCreate(): RedirectResponse | ||
{ | ||
return redirect()->route("passwordless.create") | ||
->with("success", "Jeśli podany adres e-mail istnieje w naszej bazie, otrzymasz link do logowania."); | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,12 +1,21 @@ | ||
<x-mail::message> | ||
# Login Link | ||
<x-mail::layout> | ||
|
||
Use the link below to log into the {{ config('app.name') }} application. | ||
<x-slot:header> | ||
<x-mail::header url="{{config('app.url')}}"> | ||
Keating | ||
</x-mail::header> | ||
</x-slot:header> | ||
|
||
Zaloguj się, klikając w poniższy przycisk: | ||
|
||
<x-mail::button :url="$url"> | ||
Login | ||
Zaloguj | ||
</x-mail::button> | ||
|
||
Thanks,<br> | ||
{{ config('app.name') }} | ||
</x-mail::message> | ||
<x-slot:footer> | ||
<x-mail::footer> | ||
Keating © {{ date('Y') }} | ||
</x-mail::footer> | ||
</x-slot:foot> | ||
|
||
</x-mail::layout> |
This file contains 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.