Skip to content

Commit 3807340

Browse files
authored
Merge pull request #413 from codeigniter4/magic-link-notice
feat: notify devs when user has used magic link login.
2 parents 769c725 + f941915 commit 3807340

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

docs/events.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Shield fires off several events during the lifecycle of the application that you
99
- [login](#login)
1010
- [failedLogin](#failedlogin)
1111
- [logout](#logout)
12+
- [magicLogin](#magiclogin)
13+
- [Event Timing](#event-timing)
1214

1315
## Responding to Events
1416

@@ -66,6 +68,18 @@ When the magic link login fails, the following array will be provided:
6668

6769
Fired immediately after a successful logout. The only argument is the `User` entity.
6870

71+
#### magicLogin
72+
73+
Fired when a user has been successfully logged in via a magic link. This event does not have any parameters passed in. The authenticated user can be discovered through the `auth()` helper.
74+
75+
```php
76+
Events::on('magicLogin', function() {
77+
$user = auth()->user();
78+
79+
//
80+
})
81+
```
82+
6983
### Event Timing
7084

7185
To learn more about Event timing, please see the list below.

docs/quickstart.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ 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)
17+
- [Session Notification](#session-notification)
18+
- [Event](#event)
1619
- [Authorization Flow](#authorization-flow)
1720
- [Change Available Groups](#change-available-groups)
1821
- [Set the Default Group](#set-the-default-group)
@@ -126,6 +129,37 @@ public array $actions = [
126129
];
127130
```
128131

132+
### Responding to Magic Link Logins
133+
134+
Magic Link logins allow a user that has forgotten their password to have an email sent with a unique, one-time login link. 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 they 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.
135+
136+
#### Session Notification
137+
138+
You can detect if a user has finished the magic link login by checking for a session value, `magicLogin`. If they have recently completed the flow, it will exist and have a value of `true`.
139+
140+
```php
141+
if (session('magicLogin')) {
142+
return redirect()->route('set_password');
143+
}
144+
```
145+
146+
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.
147+
148+
```php
149+
session()->removeTempdata('magicLogin');
150+
```
151+
152+
#### Event
153+
154+
At the same time the above session variable is set, a `magicLogin` [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('magicLogin', static function () {
158+
// ...
159+
});
160+
```
161+
162+
129163
## Authorization Flow
130164

131165
### Change Available Groups

docs/session_auth_event_and_logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ The following is a list of Events and Logging for Session Authenticator.
3838
- OK → no event
3939
- NG → no event
4040
2. Send request with token
41-
- OK → event `login` / table `auth_logins`
41+
- OK → event `login` and `magicLogin` / table `auth_logins`
4242
- NG → event `failedLogin` / table `auth_logins`

src/Controllers/MagicLinkController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ 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('magicLogin', true);
169+
170+
Events::trigger('magicLogin');
171+
166172
// Get our login redirect url
167173
return redirect()->to(config('Auth')->loginRedirect());
168174
}

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('magicLogin'));
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('magicLogin'));
143+
$this->assertTrue(session('magicLogin'));
137144
}
138145
}

0 commit comments

Comments
 (0)