Skip to content

Commit 6b112c4

Browse files
lonnieezellkenjis
authored andcommitted
feat: notify devs when user has used magic link login.
1 parent 25f6155 commit 6b112c4

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

docs/quickstart.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ NOTE: The examples assume that you have run the setup script and that you have c
1313
- [Change Access Token Lifetime](#change-access-token-lifetime)
1414
- [Enable Account Activation via Email](#enable-account-activation-via-email)
1515
- [Enable Two-Factor Authentication](#enable-two-factor-authentication)
16+
- [Responding to Magic Link Logins](#responding-to-magic-link-logins)
1617
- [Authorization Flow](#authorization-flow)
1718
- [Change Available Groups](#change-available-groups)
1819
- [Set the Default Group](#set-the-default-group)
@@ -126,6 +127,24 @@ public array $actions = [
126127
];
127128
```
128129

130+
### Responding to Magic Link Logins
131+
132+
Magic Link logins allow a user that has forgotten their password that have an email sent with a unique login link that will provide a one-time login for them. Once they've logged in you can decide how to respond. In some cases, you might want to redirect them to a special page where the must choose a new password. In other cases, you might simply want to display a one-time message prompting them to go to their account page and choose a new password there.
133+
134+
You can detect if a user has finished the magic link login by checking for a session value, `magic_link_login`. If they have recently completed the flow, it will exist and have a value of `true`.
135+
136+
```php
137+
if (session('magic_link_login')) {
138+
return redirect()->route('set_password');
139+
}
140+
```
141+
142+
This value sticks around in the session for 5 minutes. Once you no longer need to take any actions, you might want to delete the value from the session.
143+
144+
```php
145+
session()->removeTempData('magic_link_login');
146+
```
147+
129148
## Authorization Flow
130149

131150
### Change Available Groups

src/Controllers/MagicLinkController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ public function verify(): RedirectResponse
163163

164164
$this->recordLoginAttempt($identifier, true, $user->id);
165165

166+
// Give the developer a way to know the user
167+
// logged in via a magic link.
168+
session()->setTempdata('magic_link_login', true);
169+
166170
// Get our login redirect url
167171
return redirect()->to(config('Auth')->loginRedirect());
168172
}

tests/Authentication/MagicLinkTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public function testMagicLinkVerifyExpired(): void
114114

115115
$result->assertRedirectTo(route_to('magic-link'));
116116
$result->assertSessionHas('error', lang('Auth.magicLinkExpired'));
117+
118+
// It should have set temp session var
119+
$this->assertFalse(session()->has('magic_link_login'));
117120
}
118121

119122
public function testMagicLinkVerifySuccess(): void
@@ -134,5 +137,9 @@ public function testMagicLinkVerifySuccess(): void
134137
$result->assertRedirectTo(site_url());
135138
$result->assertSessionHas('user', ['id' => $user->id]);
136139
$this->assertTrue(auth()->loggedIn());
140+
141+
// It should have set temp session var
142+
$this->assertTrue(session()->has('magic_link_login'));
143+
$this->assertTrue(session('magic_link_login'));
137144
}
138145
}

0 commit comments

Comments
 (0)