|
1 | | -# Magic Link Login |
| 1 | +# Magic Login |
2 | 2 |
|
3 | | -Magic Link Login is a feature that allows users to log in if they forget their |
4 | | -password. |
| 3 | +Magic Login is an authentication feature that allows users to sign in using a one-time login link(magic link) or a one-time verification code(opt-code) sent to their email. This method provides a secure, password-less entry option, and is especially valuable for users who have forgotten their password, offering a fast and friction-free recovery experience. |
5 | 4 |
|
6 | 5 | ## Configuration |
7 | 6 |
|
8 | | -### Configure Magic Link Login Functionality |
| 7 | +### Configure Magic Login Functionality |
9 | 8 |
|
10 | | -Magic Link Login functionality is enabled by default. |
| 9 | +Magic Login functionality is enabled by default. |
11 | 10 | You can change it within the **app/Config/Auth.php** file. |
12 | 11 |
|
13 | 12 | ```php |
14 | 13 | public bool $allowMagicLinkLogins = true; |
15 | 14 | ``` |
16 | 15 |
|
17 | | -### Magic Link Lifetime |
| 16 | +### Magic Login Mode |
18 | 17 |
|
19 | | -By default, Magic Link can be used for 1 hour. This can be easily modified |
| 18 | +Defines the format and type of the one-time credential sent to users for magic login. The system supports both password-less login via email link as well as multiple one-time verification code formats. The delivery method and code type are specified using configurable mode patterns. |
| 19 | + |
| 20 | +#### Supported Modes |
| 21 | + |
| 22 | +`clickable` (default) — Sends a secure, one-time login link to the user’s email that can be clicked to authenticate instantly. |
| 23 | + |
| 24 | +`<length>-numeric` — Sends a numeric one-time code with the defined character length. |
| 25 | + |
| 26 | +`<length>-alpha` — Sends an alphabet-only one-time code with the defined character length. |
| 27 | + |
| 28 | +`<length>-alnum` — Sends an alphanumeric one-time code with the defined character length. |
| 29 | + |
| 30 | +`<length>-oneof` — Sends a one-time code with the defined length, while the system automatically selects the random code type from: numeric, alpha, or alnum. |
| 31 | + |
| 32 | +```php |
| 33 | +public string $magicLoginMode = '8-oneof'; |
| 34 | +``` |
| 35 | + |
| 36 | +### Magic Lifetime |
| 37 | + |
| 38 | +By default, Magic can be used for 1 hour. This can be easily modified |
20 | 39 | in the **app/Config/Auth.php** file. |
21 | 40 |
|
22 | 41 | ```php |
23 | 42 | public int $magicLinkLifetime = HOUR; |
24 | 43 | ``` |
25 | 44 |
|
| 45 | +!!! warning |
| 46 | + |
| 47 | + One-time numeric codes (such as a 6-digit `6-numeric` format) are inherently more predictable and may be easier to guess. If you plan to deliver a one-time password (OTP) instead of a magic link, we strongly recommend using the `6-oneof` credential mode, which allows the system to randomly generate numeric, alphabetic, or alphanumeric codes. This approach significantly increases unpredictability and makes OTP guessing more difficult. |
| 48 | + |
| 49 | + Additionally, to further mitigate brute-force and code-guessing risks, it is advisable to reduce the `$magicLinkLifetime` to a short window of only a few minutes (e.g., 120–300 seconds). |
| 50 | + |
| 51 | + |
26 | 52 | ### Bot Detection |
27 | 53 |
|
28 | | -Some apps or devices may try to be "too helpful" by automatically visiting links - for example, to check if they're safe or to prepare for read-aloud features. Since this is a one-time magic link, such automated visits could invalidate it. To prevent this, Shield relies on the framework's `UserAgents::robots` config property (**app/Config/UserAgents.php**) to filter out requests that are likely initiated by non-human agents. |
| 54 | +Some apps or devices may try to be "too helpful" by automatically visiting links - for example, to check if they're safe or to prepare for read-aloud features. Since this is a one-time magic link(`clickable`), such automated visits could invalidate it. To prevent this, Shield relies on the framework's `UserAgents::robots` config property (**app/Config/UserAgents.php**) to filter out requests that are likely initiated by non-human agents. |
29 | 55 |
|
30 | | -## Responding to Magic Link Logins |
| 56 | +Unlike one-time login links, one-time codes (OTP) are not impacted by automated URL visits, since they require manual user input to complete authentication. Therefore, if your application delivers OTP(`6-numeric`,`6-alpha`,`6-alnum`,`6-oneof`) credentials, bot auto-visits do not introduce the same credential-invalidation risk. |
| 57 | + |
| 58 | +## Responding to Magic Logins |
31 | 59 |
|
32 | 60 | !!! note |
33 | 61 |
|
34 | 62 | You need to configure **app/Config/Email.php** to allow Shield to send emails. See [Installation](../getting_started/install.md#initial-setup). |
35 | 63 |
|
36 | | -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. |
| 64 | +Magic Login allows users who have forgotten their password to authenticate using a secure, one-time credential delivered via email. Depending on configuration or product requirements, this credential can be either: |
| 65 | + |
| 66 | +1. unique, single-use login link |
| 67 | + |
| 68 | +2. one-time opt-code for manual entry in the application |
| 69 | + |
| 70 | +After authentication succeeds, the system behavior is up to the developer. Possible responses include redirecting the user to a dedicated password reset screen, or displaying a one-time confirmation message encouraging them to update their password from their account settings. |
37 | 71 |
|
38 | 72 | ### Session Notification |
39 | 73 |
|
40 | | -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`. |
| 74 | +You can detect if a user has finished the magic login by checking for a session value, `magicLogin`. If they have recently completed the flow, it will exist and have a value of `true`. |
41 | 75 |
|
42 | 76 | ```php |
43 | 77 | if (session('magicLogin')) { |
|
0 commit comments