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 occured (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 identifer 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
File details
Details for the file reflex_magic_link_auth-0.0.4.tar.gz
.
File metadata
- Download URL: reflex_magic_link_auth-0.0.4.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb04d79c5a1393cf9dbad338b22f08a88a38b7a0107f53b78b330eae80a4abf2 |
|
MD5 | 42f74a6e216d45ca2abff418046ed0bd |
|
BLAKE2b-256 | a4d46cda0763ef777c5ce31322f259b056a192385f6ed4a98b2fe58d472b2881 |
File details
Details for the file reflex_magic_link_auth-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: reflex_magic_link_auth-0.0.4-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc570d2f7696c0f70883db2b06dbbf23f0ce2852accf5b4711d34e10f17d46ef |
|
MD5 | b51af0f796b2e3dae085c214c40836a0 |
|
BLAKE2b-256 | d3cb1a30ebccde8bc50317211e1565f583a73b111ff5da1b99dc26397e4afd5c |