How to Add Passwordless Magic Link Login to an AI-Built App
A practical guide to adding magic link login to an AI-built app, covering the security details most tutorials skip and how it stacks up against SSO and social login.
Password reset emails make up a big share of support tickets for small apps, and most users reuse passwords that already leaked somewhere else. If you're building an app with AI tools, you can skip that entire failure mode from day one. A magic link is a single-use, time-limited link sent to a user's email address. They click it, and they're signed in. No password to choose, forget, or leak in the next unrelated breach.
Every user already has an email inbox, so there's no separate account system to stand up first. That's the practical appeal for a new app: you skip configuring an external identity provider or registering OAuth credentials with any platform, and you never become the target of a password database breach, because there's no password database to breach.
Magic link vs SSO vs social login vs password
These four options solve the same problem in different ways, and picking the wrong one for your app's audience causes real friction later.
Password: the user sets a password, you store a hash of it, and you own the entire login screen. It's the most familiar option, but you inherit every problem that comes with passwords: weak choices, reuse across sites, forgotten credentials, and the support load of reset flows.
SSO: the user authenticates through a company's own identity provider, like Okta or Microsoft Entra. This is the right fit for B2B software sold to companies with an IT department that wants to control who can log in. It also means configuring a third-party identity provider and handling its metadata, more setup than a small app usually needs on day one. adding SSO login to an AI-built app covers what that setup involves.
Social login: the user signs in with an existing Google, GitHub, or similar account through OAuth. It removes the password entirely, but it requires registering an OAuth app with each provider and accepting their branding, rate limits, and occasional outages as part of your login flow. adding social login to an AI-built app walks through those tradeoffs.
Magic link: no third-party identity provider, no OAuth app to register anywhere. It's just your app and the user's inbox. It's the fastest of the four to build, and it works for both B2B and consumer products, but it shifts your login reliability onto your email provider. A magic link that lands in spam is a login that doesn't work, and asking someone to leave your app to check email is more friction on a phone than typing a password, so it fits low-frequency tools better than something people open twenty times a day.
The security details that are easy to get wrong
Magic links are simple to describe and easy to implement badly. Most of the actual risk lives in details that don't show up in a quick tutorial.
Token expiry. Give each link a short lifetime, 15 minutes is a reasonable default. A magic link that still works a week later is effectively a password that never expires and sits in someone's inbox forever.
Single-use enforcement. Mark the token as used the moment it's verified, and reject it on any later attempt, even if it hasn't expired yet. A reusable link is a shared credential: anyone who finds it in a forwarded email or a browser history can log in.
Rate limiting. Cap how many login emails a given address or IP can trigger per hour. Without this, someone can email-bomb a user's inbox by repeatedly requesting links for their address, which is annoying at best and a phishing setup at worst.
No account enumeration. Return the same response, something like 'check your email for a sign-in link', whether or not that address has an account. Returning different messages for known versus unknown emails hands attackers a way to map out your user list.
It's worth logging failed verification attempts by email and IP too, not to block anyone automatically, but so you notice if someone is systematically trying to guess valid tokens. These aren't edge cases, they're the baseline. OWASP's authentication guidance covers token handling and session management in more depth if you want the fuller checklist before shipping this to real users.
Building it
The flow itself is short: three steps, each with one job.
Request a token. The user submits their email. The server generates a random, high-entropy token, hashes it, and stores the hash together with the email address and an expiry timestamp, typically 15 minutes out. The raw token never touches your database.
Send the email. The server emails a link containing the raw token to the address on file, through a dedicated transactional email provider such as Postmark, Resend, or Amazon SES, rather than a general-purpose SMTP box, since deliverability and spam placement depend heavily on sender reputation. The server discards the raw token from memory once the email is sent.
Verify and issue a session. When the user clicks the link, the server hashes the incoming token, looks up the match, checks that it hasn't expired and hasn't been used, then marks it used and issues a session cookie or signed token. Any failure at this step returns the same generic error, expired or invalid, without specifying which.
One detail that trips people up: magic links usually handle signup and login through the same flow. If the email doesn't match an existing account, create one at the moment of first successful verification instead of building a separate signup form. It keeps onboarding shorter and removes an entire password-creation screen from the experience.
Prompting an AI coding agent to build this
General-purpose AI coding tools will happily generate a magic link flow that looks correct and quietly skips the parts that matter, a token that never expires, or one that can be replayed after it's already been used. The fix is to put the security requirements directly in the prompt instead of assuming the agent will infer them. That's true whether you're working inside a full app builder or one of the many AI coding tools built for narrower coding tasks.
Implement passwordless email login. Generate a random 32-byte token, store only its SHA-256 hash along with the user's email and an expires_at timestamp 15 minutes from creation. Email the raw token as a sign-in link through the transactional email provider already configured in this project. On verification, hash the incoming token, look it up, reject it if expired or already marked used, mark it used immediately on success, and return the identical response whether or not the email exists in the system. Rate-limit requests to 5 per email address per hour.
Ask it to write a short test for the expired-token and reused-token cases specifically. Those are the two paths most likely to get skipped, and the two that matter most if someone finds a login link that isn't theirs.
FAQ
Is magic link login as secure as a password?
For most small apps, it's more secure in practice, because it removes weak and reused passwords from the equation entirely. The security that remains depends on implementation, short token expiry, single-use enforcement, and rate limiting, and on the security of the user's email account itself, since that inbox now effectively controls access to your app. A well-built magic link flow is a meaningful upgrade over a typical password form; a badly built one just moves the same risks somewhere less visible.
What happens if someone else has access to my email?
They can sign in as that user. This is the real tradeoff of magic links: your app's login security becomes a function of the user's email provider's security. It's usually a reasonable bet, since most people already protect their email account carefully because so much else depends on it, but it's worth stating plainly in your own security documentation rather than assuming users understand the connection.
Can I combine magic links with two-factor authentication?
Yes, and it's a good pattern for anything handling sensitive data. Use the magic link as the first factor to confirm the person controls that email address, then require a second factor, an authenticator app code or a push approval, before unlocking sensitive actions like changing billing details or exporting data. adding two-factor authentication to an AI-built app covers the implementation details for that second layer.
Do magic links work well on mobile?
It depends on how someone reads their email. If they open the link on the same device they're using your app on, it's a smooth tap-through, especially with deep linking configured so the link opens your app directly instead of a browser. If they signed up on a phone but check email on a laptop, or the other way around, the flow breaks the session context and they end up authenticated on the wrong device. That's why magic links tend to work best for tools people open a handful of times a week, and worse for anything expecting frequent logins from a phone throughout the day.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


