-
Notifications
You must be signed in to change notification settings - Fork 0
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
#42 - passwordless login #138
Open
kamilpiech97
wants to merge
4
commits into
main
Choose a base branch
from
#42-passwordless-login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keating\Actions; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\URL; | ||
use Keating\Mail\LoginLink; | ||
|
||
class SendLoginLink | ||
{ | ||
public function handle(string $email, Carbon $time): void | ||
{ | ||
Mail::to( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you replace facades with dependency injection (as in other actons)? |
||
users: $email, | ||
)->send( | ||
mailable: new LoginLink( | ||
url: URL::temporarySignedRoute( | ||
name: "passwordless.login", | ||
expiration: (int)Carbon::now()->diffInSeconds($time), | ||
parameters: [ | ||
"email" => $email, | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keating\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
final class LoginLink extends Mailable | ||
{ | ||
use Queueable; | ||
use SerializesModels; | ||
|
||
public function __construct( | ||
public readonly string $url, | ||
) {} | ||
|
||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: "Link do logowania w aplikacji Keating", | ||
); | ||
} | ||
|
||
public function content(): Content | ||
{ | ||
return new Content( | ||
markdown: "emails.auth.login-link", | ||
with: [ | ||
"url" => $this->url, | ||
], | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Keating\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property string $id | ||
* @property string $email | ||
* @property string $session_id | ||
* @property bool $can_login | ||
* @property Carbon $expires_at | ||
*/ | ||
class PasswordlessAttempt extends Model | ||
{ | ||
use HasFactory; | ||
use HasUlids; | ||
|
||
protected $fillable = [ | ||
"email", | ||
"session_id", | ||
"can_login", | ||
"expires_at", | ||
]; | ||
} |
26 changes: 26 additions & 0 deletions
26
database/migrations/2024_08_28_123906_create_passwordless_attempts_table.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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create("passwordless_attempts", function (Blueprint $table): void { | ||
$table->ulid("id")->primary(); | ||
$table->string("email")->unique(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should make it unique? |
||
$table->string("session_id"); | ||
$table->boolean("can_login")->default(false); | ||
$table->timestamp("expires_at"); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists("passwordless_attempts"); | ||
} | ||
}; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would refactor this to
->sole()
instead of->first()
.