Skip to content

Commit 96b1eee

Browse files
lonnieezellkenjis
authored andcommitted
Fire an event when user uses magic login link
1 parent 6b112c4 commit 96b1eee

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

docs/quickstart.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ NOTE: The examples assume that you have run the setup script and that you have c
1414
- [Enable Account Activation via Email](#enable-account-activation-via-email)
1515
- [Enable Two-Factor Authentication](#enable-two-factor-authentication)
1616
- [Responding to Magic Link Logins](#responding-to-magic-link-logins)
17+
- [Session Notification](#session-notification)
18+
- [Event](#event)
1719
- [Authorization Flow](#authorization-flow)
1820
- [Change Available Groups](#change-available-groups)
1921
- [Set the Default Group](#set-the-default-group)
@@ -131,10 +133,12 @@ public array $actions = [
131133

132134
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.
133135

136+
#### Session Notification
137+
134138
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`.
135139

136140
```php
137-
if (session('magic_link_login')) {
141+
if (session('magic_login')) {
138142
return redirect()->route('set_password');
139143
}
140144
```
@@ -145,6 +149,17 @@ This value sticks around in the session for 5 minutes. Once you no longer need t
145149
session()->removeTempData('magic_link_login');
146150
```
147151

152+
#### Event
153+
154+
At the same time the above session variable is set, a `magic_login` [event](https://codeigniter.com/user_guide/extending/events.html) is fired off that you may subscribe to. Note that no data is passed to the event as you can easily grab the current user from the `user()` helper or the `auth()->user()` method.
155+
156+
```php
157+
Events::on('magic_login', static function () {
158+
// ...
159+
});
160+
```
161+
162+
148163
## Authorization Flow
149164

150165
### Change Available Groups

src/Controllers/MagicLinkController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ public function verify(): RedirectResponse
165165

166166
// Give the developer a way to know the user
167167
// logged in via a magic link.
168-
session()->setTempdata('magic_link_login', true);
168+
session()->setTempdata('magic_login', true);
169+
170+
Events::trigger('magic_login');
169171

170172
// Get our login redirect url
171173
return redirect()->to(config('Auth')->loginRedirect());

tests/Authentication/MagicLinkTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function testMagicLinkVerifySuccess(): void
139139
$this->assertTrue(auth()->loggedIn());
140140

141141
// It should have set temp session var
142-
$this->assertTrue(session()->has('magic_link_login'));
143-
$this->assertTrue(session('magic_link_login'));
142+
$this->assertTrue(session()->has('magic_login'));
143+
$this->assertTrue(session('magic_login'));
144144
}
145145
}

0 commit comments

Comments
 (0)