Ploy
Ploy
Authentication

Emails, sign-in links and passwords

Verify users' email addresses, sign them in with a one-time email link or code, let them reset or change their password, customize the emails, and choose the address Ploy Auth sends from.

Emails, sign-in links and passwords

Ploy Auth sends these emails for your app, through Ploy Email:

EmailSent when
Verify your emailA user signs up with a password, or asks for the link again
Sign-in linkA user asks for a sign-in link instead of using a password
Sign-in codeA user asks for a sign-in code
Reset your passwordA user asks for a reset on the Forgot password? page
Password changedA password was reset, changed, or added, as a notice to the account owner

The emails show your project name and work without any code in your app. You can change their wording with email templates.

Auth emails are off until email sending is enabled for your organization under Dashboard → Email. Until then, sign-in links, sign-in codes and password reset are unavailable and email verification is not enforced. Everything else keeps working.

Email verification

After a password sign-up, Ploy Auth emails a verification link. It is valid for 24 hours. Opening it marks the account as verified (emailVerified: true) and sends the user back to your app with ?ploy_auth_status=email_verified; an invalid or expired link adds ?ploy_auth_error=invalid_token instead.

Users who sign in with Google, Apple, or GitHub are verified when the provider confirms their email, and users who sign in with a sign-in link are verified by opening it, so they never get this email.

Require a verified email

Require email verification is on by default for new auth setups. Change it under Auth → Settings → Emails to opt out or enable it for an older setup; existing settings are unchanged. For a new production binding, auth.require_email_verification: false in ploy.yaml seeds an opt-out. When verification is required:

  • Sign-up still creates the account and sends the link, but responds with emailVerificationRequired: true and no session.
  • Password and passkey sign-in for an unverified password account fail with 403 and code email_not_verified. The error only appears after the correct password, so it does not reveal which emails have accounts.
  • The hosted sign-in page and SignInForm then offer Resend verification email.
  • Opening the verification link also signs the user in, so they never type their password twice. It does so on any device the first time it verifies the address. After that, only in the browser that signed up or was refused with email_not_verified, so the link is not a lasting login for anyone it is forwarded to; elsewhere it just reports the address as verified.
  • A page waiting in the sign-up browser can poll POST /email/verify/continue: it answers 403 email_not_verified until the address is verified, wherever that happens, then starts the session. Without the browser's pending-verification cookie it answers 401 no_pending_verification.
  • A sign-in link or code always works: using it proves the address.

The setting has no effect while auth emails are off, so users are never locked out by an email that can't be sent.

A sign-in link (often called a magic link) lets users sign in with just their email address. Turn on Magic link (email sign-in link) under Auth → Settings → Sign-in methods. It counts as a sign-in method, so it can be the only one, but it only works while auth emails are enabled; the dashboard shows a hint when they are not.

When it is on, the hosted sign-in page and SignInForm offer Email me a sign-in link. Without password sign-in it becomes the main action of the email form.

  • The link is valid for 15 minutes and works once. Only a hash of it is stored.
  • Opening the link shows a confirmation page with a Continue as button. The link itself never signs anyone in, so email security scanners that open links ahead of the user don't use it up.
  • For an address without an account, the link creates the account when sign-ups are allowed. Require name applies: send metadata.name with the request, or the request fails with code name_required and the forms ask for the name.
  • The response is the same whether or not an account exists, and no email is sent for unknown addresses while sign-ups are off.
  • Opening the link marks the email as verified. If the account was never verified, its password and passkeys are removed first, so someone who registered an address they don't own loses access. The owner can then set a password.
  • After confirming, the user lands on the redirect passed with the request, signed in. An expired or used link returns there with ?ploy_auth_error=invalid_token.
await fetch("/_ploy/auth/magic-link", {
	method: "POST",
	headers: { "Content-Type": "application/json" },
	body: JSON.stringify({ email: "user@example.com", redirect: "/dashboard" }),
});

Sign-in codes

A sign-in code is a 6-digit one-time code sent by email. It suits apps whose users read their mail on another device, such as a phone, where opening a link would sign in the wrong browser. Turn on Email code under Auth → Settings → Sign-in methods. Like sign-in links, it counts as a sign-in method and only works while auth emails are enabled.

When it is on, the hosted sign-in page and SignInForm offer Email me a sign-in code. After the user enters their email, they see We emailed you a code and a code field that accepts typing or pasting; a complete code signs them in.

  • The code is valid for 10 minutes and works once. It is stored only as a keyed hash, never in plain text.
  • Each address has one active code: requesting a new one replaces the previous code.
  • After 5 wrong attempts the code stops working and the user has to request a new one (code too_many_attempts). Attempts also count toward the per-IP sign-in limit.
  • A wrong, expired, or used code fails with 400 and code invalid_code. The answer is the same for addresses without an account.
  • New accounts, Require name, sign-ups being off, and unverified accounts follow the same rules as sign-in links: using the code verifies the email address.
await fetch("/_ploy/auth/email-code", {
	method: "POST",
	headers: { "Content-Type": "application/json" },
	body: JSON.stringify({ email: "user@example.com" }),
});

const res = await fetch("/_ploy/auth/email-code/verify", {
	method: "POST",
	credentials: "include",
	headers: { "Content-Type": "application/json" },
	body: JSON.stringify({ email: "user@example.com", code: "123456" }),
});
const { user } = await res.json();

With both sign-in links and codes on, users get a single email with the code and a sign-in link, and either one signs them in. POST /magic-link and POST /email-code then send the same email, valid for 10 minutes; the forms request it with /email-code and show the code field.

Password reset

When password sign-in is on and auth emails are enabled, the hosted sign-in page and SignInForm show Forgot password?. It opens /_ploy/auth/forgot-password, which emails a link to /_ploy/auth/reset-password.

  • The link is valid for one hour and works once: it stops working as soon as the password changes.
  • Setting the new password signs the user out on every device and marks their email as verified.
  • The forgot-password page responds the same whether or not an account exists, so it can't be used to find out who has an account. Accounts that only use passkeys, social sign-in, or sign-in links have no password and get no email.

After the reset the user lands on the sign-in page with a confirmation and continues to the redirectTo you passed to /forgot-password.

Change or set a password

Signed-in users can change their password, or add one to an account created with a sign-in link, a passkey, or a social provider. UserProfile has a Password section for this; from your own UI, POST /password/change with the session cookie or an Authorization: Bearer header:

await fetch("/_ploy/auth/password/change", {
	method: "POST",
	credentials: "include",
	headers: { "Content-Type": "application/json" },
	body: JSON.stringify({ currentPassword, newPassword }),
});
  • currentPassword is required when the account already has a password; a wrong one fails with 401 and code invalid_credentials. GET /me returns hasPassword so you know whether to ask for it.
  • The new password must meet the same rules as at sign-up (code weak_password otherwise).
  • Every other session of the user is signed out; the current one stays signed in.
  • The owner gets a notice email when auth emails are enabled.

Password sign-in must be on for the project.

Sender settings

Configure the sender under Auth → Settings → Emails:

SettingDescription
Send fromYour organization's <name>.ploy.email domain (the default) or one of its verified custom domains
AddressThe part before the @, for example noreply (the default) or hello
Display nameThe name shown in the inbox. Defaults to the project name

The settings show a preview of the From address, for example Acme <noreply@acme.ploy.email>. If a custom domain stops being verified, emails are sent from the ploy.email domain until it is verified again. See Email to add a custom domain.

Email templates

Change the wording of each auth email under Auth → Settings → Email templates. Pick the email (Verify email, Sign-in link, Sign-in code, Reset password, Password changed) and override any of these fields; empty fields keep the default text:

FieldMax lengthDescription
Subject200The subject line
Heading200The title at the top of the email
Body2000The message. Separate paragraphs with a blank line
Button label50The text of the button (not in Password changed)

The fields are plain text and can use these placeholders:

PlaceholderValue
{{app_name}}Your project name
{{email}}The recipient's address
{{link}}The link in the email, in emails that have one
{{code}}The sign-in code, in sign-in code emails
{{expires_in}}How long the link or code is valid, for example 10 minutes

Placeholders that don't apply to an email, and unknown ones, are left as written. Reset to default clears the overrides of the selected email, and the preview next to the fields shows the email as it will be sent.

Your text is always placed into the standard email layout and escaped, so HTML in a template shows up as text. The button, the link, the code, and the security note at the bottom are always included, so a template can't break sign-in or hide what the email is for.

Rate limits

To protect your users' inboxes, verification, sign-in link, sign-in code, and reset emails are limited to 3 per hour per email address and 10 per hour per IP address. Further requests return 429 with code: "rate_limited" and a Retry-After header. Password changes share the sign-in limits. Emails also count toward your project's daily email limit.

Endpoints

EndpointMethodBodyDescription
/email/verify/requestPOST{ email, redirect? }Send the verification email again. Always returns 200
/email/verifyGET?token=&redirect=The link in the verification email
/magic-linkPOST{ email, redirect?, metadata? }Email a sign-in link. Always returns 200
/magic-link/verifyGET?token=&redirect=The link in the email: a page to confirm the sign-in
/magic-link/verifyPOST{ token }Use the link and start a session. Returns { user }
/email-codePOST{ email, redirect?, metadata? }Email a sign-in code. Always returns 200
/email-code/verifyPOST{ email, code }Use the code and start a session. Returns { user }
/password/forgotPOST{ email, redirect? }Send a password reset email. Always returns 200
/password/resetPOST{ token, password }Set a new password with the token from the reset link
/password/changePOST{ currentPassword?, newPassword }Change or set the signed-in user's password
/forgot-passwordGET?redirectTo=Hosted page to request a reset link
/reset-passwordGET?token=&redirectTo=Hosted page to choose a new password

redirect and redirectTo accept a path or a URL on your app's origin, like everywhere else in Ploy Auth. /email/verify/request, /magic-link, /email-code, and /password/forgot return 403 with code email_unavailable while auth emails are off, /magic-link returns magic_link_disabled while sign-in links are off, and /email-code returns email_code_disabled while sign-in codes are off. GET /config reports emailVerificationRequired, passwordResetEnabled, magicLink, and emailCode so you can adapt your own UI.

With @meetploy/auth-react, use the hook instead of calling the endpoints:

import { PloyAuthError, useAuth } from "@meetploy/auth-react";

const {
	user,
	signIn,
	resendVerification,
	sendMagicLink,
	sendEmailCode,
	verifyEmailCode,
	requestPasswordReset,
	resetPassword,
	changePassword,
} = useAuth();

try {
	await signIn(email, password);
} catch (error) {
	if (error instanceof PloyAuthError && error.code === "email_not_verified") {
		await resendVerification(email);
	}
}

await sendMagicLink(email, { redirectTo: "/dashboard" });
await sendEmailCode(email);
await verifyEmailCode(email, code); // signs in, returns the user
await requestPasswordReset(email, { redirectTo: "/account" });
await resetPassword(token, newPassword);
await changePassword({
	currentPassword: user?.hasPassword ? currentPassword : undefined,
	newPassword,
});

Lifecycle hooks

Auth hooks apply to both magic-link and email-code sign-in. For new users, approval runs when credentials are verified, not when the email is requested. afterEmailVerified runs when an account becomes verified, including accounts created already verified by social sign-in, magic link, or email code. Merely verifying an existing account by email link does not create a session.

Local development

Under ploy dev nothing is sent. Every auth email lands in a local inbox instead: open the dev dashboard and choose Auth → Emails to read it and click its links. The terminal also prints each email's link.

Sign-in links and sign-in codes are always on locally, next to passwords and passkeys, so local sign-in emails carry both the link and the code. Email verification is required by default for new local setups; turn off Require email verification in the dev dashboard's auth settings to opt out. Existing local settings are preserved.

How is this guide?

Last updated on