Login with magic links sent via email
Project description
reflex-magic-link-auth
Generate and verify "magic link" one time passwords for user authentication without complex registration flows.
Installation
pip install reflex-magic-link-auth
Usage
See
magic_link_auth_demo/magic_link_auth_demo.py
for a complete example.
1. Collect the User email
You can use a simple form with a single input field and button, like the example, or present a form prompting for more information, such as a name, address, zip code, etc. Any additional data should be associated with the provided email as the main key for the account.
2. Substate from MagicLinkAuthState
The substate which handles the authentication form submission should be
a substate of reflex_magic_link_auth.MagicLinkAuthState (alternatively, it
may get an instance of this state via .get_state API).
3. Generate the OTP
When the user submits the form, generate a one time password by calling
._generate_otp and providing the email address.
This function returns a 2-tuple:
- The latest record associated with the email address
- The one time password in plaintext
If the record is None, then the email address was invalid, or some other problem occurred (ensure database migration has been applied).
If the record is returned, but the OTP is None, then the user has exceeded the configured rate limit and cannot receive a new token for a while.
4. Send the Magic Link
Either on page on_load or some component on_mount should trigger
reflex_magic_link_auth.MagicLinkAuthState.get_base_url to ensure the state
knows the correct frontend URL when formatting the magic links.
Pass the record and otp to
reflex_magic_link_auth.MagicLinkAuthState._get_magic_link to get a URL that,
when accessed will log the user in to the app.
While this component contains an example mailgun provider, generally it is up to you to actually email the link to the user.
5. User Accesses the Magic Link
The user clicks the link in their email, and the page mounted at
reflex_magic_link_auth.constants.AUTH_ROUTE will validate the token and
redirect to the URL specified in the query param redir.
6. Verifying Access
Any event handlers which depend on user session validity should check the computed var
reflex_magic_link_auth.MagicLinkAuthState.session_is_valid to determine if the user is
logged in and the session has not expired.
A persistent external identifier hashed over the email address is provided at
reflex_magic_link_auth.MagicLinkAuthState.auth_session.persistent_id. When interoperating
with other user information systems, this ID can be used to uniquely identify a user
originating from reflex-magic-link-auth.
7. Logout
To log the user out, trigger the event handler
reflex_magic_link_auth.MagicLinkAuthState.logout.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reflex_magic_link_auth-0.2.0.post1.tar.gz.
File metadata
- Download URL: reflex_magic_link_auth-0.2.0.post1.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f720e3e72cf33b0933e574dbdbdf00a6a1f71337dbc88de73c73fc4e7aedd4e8
|
|
| MD5 |
49eb5ee65726abb99c105f6abe448a55
|
|
| BLAKE2b-256 |
b50d20654133ff58385faac05c46a4e95f96a590d7da5fd0ddf5b7a2e8483e3a
|
File details
Details for the file reflex_magic_link_auth-0.2.0.post1-py3-none-any.whl.
File metadata
- Download URL: reflex_magic_link_auth-0.2.0.post1-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfb430176d59225a33c7cea3e718cdf29b7b12c622e58da64e3e8b65b2bb173f
|
|
| MD5 |
4e6923698a1b89d6233017096c862b22
|
|
| BLAKE2b-256 |
03560f5a1524ca489506cb4e9e90fcb8c59e689cd72af441a721eb6873f82dae
|