User & Accounts
Learn how to manage users and accounts, including updating user info, changing emails and passwords, deleting users with verification, token encryption, and account linking and unlinking.
Beyond authenticating users, Entro Auth also provides a set of methods to manage users. This includes, updating user information, changing passwords, and more.
The user table stores the authentication data of the user Click here to view the schema.
The user table can be extended using additional fields or by plugins to store additional data.
Update User
Update User Information
To update user information, you can use the updateUser function provided by the client. The updateUser function takes an object with the following properties:
import { authClient } from "@/lib/auth-client"
await authClient.updateUser({
image: "https://example.com/image.jpg",
name: "John Doe",
})name is one string, set at sign-up or from a provider's profile. An application that keeps a person's name in its own table, as first and last name for instance, has two copies, and the one here goes stale when the other is edited. It can be left alone: nothing in sign-in depends on it. It is read where a name is shown for you, in the member lists of the organization plugin, as the label of a new passkey, and as the name, given_name and family_name claims of the OAuth provider. If you use one of those, write the name back with updateUser when your own copy changes.
Which Addresses a Picture May Have
image is an address, and by default a signed-in person can set theirs to any. A page that renders it through an image optimizer with an allow-list (such as next/image) then breaks on the first host it does not name. user.imageValidator takes a Standard Schema that is given the address:
import * as z from "zod";
export const auth = entroAuth({
user: {
imageValidator: z
.string()
.startsWith("https://images.example.com/", "Pictures come from our own host"),
},
});- A request that sets a refused picture (
updateUser, sign-up) is answered 400VALIDATION_ERRORwith the schema's message. - A refused picture from a provider is left out and the sign-in goes on, since the person did not choose that address. To keep providers' pictures, accept their hosts in the schema.
- Removing a picture is always allowed. The schema cannot be asynchronous.
The check sits where the user row is written, so it also covers plugins and the admin API.
The library stores the address and nothing else: it does not host pictures. A provider's picture arrives with a provider sign-in; for a person without one, uploading a file and passing its address to updateUser is your application's part.
Change Email
To allow users to change their email, first enable the changeEmail feature, which is disabled by default. Set changeEmail.enabled to true:
import { entroAuth } from "@entro314labs/entro-auth";
import { sendEmail } from './email'; // your email sending function
export const auth = entroAuth({
user: {
changeEmail: {
enabled: true,
}
},
emailVerification: {
// Required to send the verification email
sendVerificationEmail: async ({ user, url, token }) => {
void sendEmail({
to: user.email,
})
}
}
})Avoid awaiting the email sending to prevent
timing attacks. On serverless platforms, use waitUntil or similar to ensure the email is sent.
By default, when a user requests to change their email, a verification email is sent to the new email address. The email is only updated after the user verifies the new email.
A change email link only works for the account that requested it and does not sign the user in. The new email is marked as verified only if the current email was verified.
Confirming with Current Email
For added security, you can require users to confirm the change via their current email before
the verification email is sent to the new address. To do this, provide the sendChangeEmailConfirmation function.
Without it, the session is the only proof that the owner is asking, so changeEmail needs a fresh session: one that signed in longer ago than session.freshAge is refused with SESSION_NOT_FRESH (403). When the current address confirms, any live session may ask.
import { entroAuth } from "@entro314labs/entro-auth";
import { sendEmail } from './email'; // your email sending function
export const auth = entroAuth({
user: {
changeEmail: {
enabled: true,
sendChangeEmailConfirmation: async ({ user, newEmail, url, token }, request) => {
void sendEmail({
to: user.email, // Sent to the CURRENT email
subject: 'Approve email change',
text: `Click the link to approve the change to ${newEmail}: ${url}`
})
}
}
},
// ...
})Updating Without Verification
If you want to allow users to update their email immediately without verification (only if their current email is NOT verified), you can enable updateEmailWithoutVerification.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
user: {
changeEmail: {
enabled: true,
updateEmailWithoutVerification: true
}
}
})If updateEmailWithoutVerification is false (default), the email will not be updated until the new email is verified, even if the current email is unverified.
Client Usage
Use the changeEmail function on the client to initiate the process.
import { authClient } from "@/lib/auth-client"
await authClient.changeEmail({
newEmail: "new-email@email.com",
callbackURL: "/dashboard", // to redirect after verification
});Change Password
A user's password isn't stored in the user table. Instead, it's stored in the account table. To change the password of a user, you can use one of the following approaches:
const { data, error } = await authClient.changePassword({ newPassword: "newpassword1234", // required, The new password to set currentPassword: "oldpassword1234", // required, The current user password revokeOtherSessions: true, // When set to true, all other active sessions for this user will be invalidated});newPasswordstringrequiredThe new password to set
currentPasswordstringrequiredThe current user password
revokeOtherSessionsbooleanWhen set to true, all other active sessions for this user will be invalidated
Set Password
If a user was registered using OAuth or other providers, they won't have a password or a credential account. In this case, you can use the setPassword action to set a password for the user. For security reasons, this function can only be called from the server. It checks only that the session is live, not that it is fresh, so check freshness yourself before calling it (see the note under Manually Linking Accounts). We recommend having users go through a 'forgot password' flow to set a password for their account.
import { auth } from "@/lib/auth"
await auth.api.setPassword({
body: {
newPassword: "new-password",
},
headers: await headers() // headers containing the user's session token
});To offer this in an account screen without a route of your own, turn emailAndPassword.allowSetPassword on. POST /set-password then does the same from the browser, for a session that is fresh:
export const auth = entroAuth({
emailAndPassword: {
enabled: true,
allowSetPassword: true,
},
});const { error } = await authClient.setInitialPassword({
newPassword: "new-password",
});- An account that already has a password answers
PASSWORD_ALREADY_SET; changing one ischangePassword, which asks for the old one. - A session older than
session.freshAgeanswers 403SESSION_NOT_FRESH: ask the person to sign in again first. WherefreshAgeis 0 no session is ever asked to be recent, so a session of any age can set the password; leave the option off there. - The password is held to the length limits, to the Have I Been Pwned check, and to the rate limit of the other password routes.
Without the option the route does not exist.
Verify Password
The verifyPassword function allows you to verify a user's current password. This is useful for confirming user identity before performing sensitive operations like updating security settings. This function can only be called from the server.
import { auth } from "@/lib/auth"
await auth.api.verifyPassword({
body: {
password: "user-password" // required
},
headers: await headers() // headers containing the user's session token
});For OAuth users who don't have passwords, consider using email verification or fresh session checks for sensitive operations instead.
Delete User
Entro Auth provides a utility to hard delete a user from your database. It's disabled by default, but you can enable it easily by passing enabled:true
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
//...other config
user: {
deleteUser: {
enabled: true
}
}
})Once enabled, you can call authClient.deleteUser to permanently delete user data from your database.
Adding Verification Before Deletion
For added security, you’ll likely want to confirm the user’s intent before deleting their account. A common approach is to send a verification email. Entro Auth provides a sendDeleteAccountVerification utility for this purpose.
This is especially needed if you have OAuth setup and want them to be able to delete their account without forcing them to login again for a fresh session.
Here’s how you can set it up:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
user: {
deleteUser: {
enabled: true,
sendDeleteAccountVerification: async (
{
user, // The user object
url, // The auto-generated URL for deletion
token // The verification token (can be used to generate custom URL)
},
request // The original request object (optional)
) => {
// Your email sending logic here
// Example: sendEmail(data.user.email, "Verify Deletion", data.url);
},
},
},
});How callback verification works:
- Callback URL: The URL provided in
sendDeleteAccountVerificationis a pre-generated link that deletes the user data when accessed.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser({
callbackURL: "/goodbye" // you can provide a callback URL to redirect after deletion
});- Authentication Check: The user must be signed in to the account they’re attempting to delete. If they aren’t signed in, the deletion process will fail.
If you have sent a custom URL, you can use the deleteUser method with the token to delete the user.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser({
token
});With the organization plugin, deleting an account can leave an organization without an owner. See When the only owner deletes their account.
Authentication Requirements
To delete a user, the user must meet one of the following requirements:
- A valid password
if the user has a password, they can delete their account by providing the password.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser({
password: "password"
});- Fresh session
The user must have a fresh session token, meaning the user must have signed in recently. This is checked if the password is not provided. A session that is not fresh is refused with SESSION_NOT_FRESH (403), the answer every endpoint that needs a fresh session gives.
By default session.freshAge is set to 60 * 60 * 24 (1 day). You can change this value by passing the session object to the auth configuration. If it is set to 0, the freshness check is disabled. It is recommended not to disable this check if you are not using email verification for deleting the account.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser();- Enabled email verification (needed for OAuth users)
As OAuth users don't have a password, we need to send a verification email to confirm the user's intent to delete their account. If you have already added the sendDeleteAccountVerification callback, you can just call the deleteUser method without providing any other information.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser();- If you have a custom delete account page and sent that url via the
sendDeleteAccountVerificationcallback. Then you need to call thedeleteUsermethod with the token to complete the deletion.
import { authClient } from "@/lib/auth-client"
await authClient.deleteUser({
token
});Callbacks
validateUserInfo: A gate that decides which identities Entro Auth admits. It fires just before a user is created (create-user) or a new provider account is linked (link-account), for every authentication method (OAuth, OIDC SSO, SAML SSO, email/password, magic link, email OTP, anonymous, SIWE, phone number, and admin-created users), including stateless setups with no persistent database, so policy lives in one place instead of per provider.
It also fires when an existing OAuth or SSO user signs in again (sign-in), and there it receives the fresh provider email and profile rather than the stored row. That lets a domain or org policy reject a user whose provider identity moved out of bounds (for example, an email that left the allowed domain). Non-provider returning sign-ins are not re-validated, because their stored row has not changed since create-user gated it; use the admin plugin's ban controls or a databaseHooks.session.create.before hook to block those.
source.action is "create-user", "link-account", or "sign-in", and source.method is the authentication method. For OAuth, source.oauth carries the provider id and raw provider profile. For OIDC and SAML SSO, source.sso carries the SSO provider id and raw provider claims or assertion attributes.
Return nothing to allow provisioning. Return an object with error to reject it: browser/redirect flows send the rejection to the configured error URL, while programmatic flows return a 403 API error. Avoid putting sensitive details in errorDescription because it is returned to the client.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
user: {
validateUserInfo: ({ user, source }) => {
if (!user.email?.endsWith("@example.com")) {
return {
error: "email_not_allowed",
errorDescription: "Use your example.com email to sign in",
};
}
if (
source.oauth?.providerId === "company-oauth" &&
source.oauth?.profile?.hd !== "example.com"
) {
return {
error: "invalid_organization",
errorDescription: "Use your company OAuth account",
};
}
},
},
});validateUserInfo is the high-level policy gate. The lower-level databaseHooks.user.create.before still runs afterward for data shaping and can also abort a write; reach for it when you need to mutate the record rather than accept or reject the identity.
beforeDelete: This callback is called before the user is deleted. You can use this callback to perform any cleanup or additional checks before deleting the user.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
user: {
deleteUser: {
enabled: true,
beforeDelete: async (user) => {
// Perform any cleanup or additional checks here
},
},
},
});you can also throw APIError to interrupt the deletion process.
import { entroAuth } from "@entro314labs/entro-auth";
import { APIError } from "@entro314labs/entro-auth/api";
export const auth = entroAuth({
user: {
deleteUser: {
enabled: true,
beforeDelete: async (user, request) => {
if (user.email.includes("admin")) {
throw new APIError("BAD_REQUEST", {
message: "Admin accounts can't be deleted",
});
}
},
},
},
});afterDelete: This callback is called after the user is deleted. You can use this callback to perform any cleanup or additional actions after the user is deleted.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
user: {
deleteUser: {
enabled: true,
afterDelete: async (user, request) => {
// Perform any cleanup or additional actions here
},
},
},
});beforeDelete and the deletion are two separate steps: your callback runs first, then the library deletes on its own connection. If the deletion fails after your callback removed your own rows, the person keeps a sign-in whose application data is gone, and no transaction of yours can span the two. Keep beforeDelete to checks, and to work that is safe to have done if the account stays. Do your side of the removal after the account is gone, from afterDelete or a databaseHooks.user.delete.after hook, and where it must not be lost record it with the outbox plugin so that a failure is tried again.
Users from Server Code
Your own server code can read the user directory and provision an account through auth.api, without the admin plugin and without reaching into the adapter. These calls have no HTTP route: a browser cannot make them, so they ask for no session, and your code decides who may call them.
const user = await auth.api.findUserById({ body: { userId } });
const sameUser = await auth.api.findUserByEmail({ body: { email } });
const where = [{ field: "plan", value: "team" }];
const { total } = await auth.api.countUsers({ body: { where } });
const page = await auth.api.findUsers({
body: { where, limit: 50, offset: 0, sortBy: { field: "email", direction: "asc" } },
});findUserById and findUserByEmail answer null for nobody. where takes the adapter's conditions (field, value, and optionally operator, connector, mode). findUsers without a limit returns at most 100 rows, the adapter's default; count with countUsers, not with the length of a list. Users are handed out as everywhere else, without fields marked returned: false.
Find or Create by Address
Code that learns of a person from outside (a payment webhook, an import) needs "the account under this address, made if there is none", and is often told twice at once:
const { user, created } = await auth.api.findOrCreateUser({
body: { email: buyer.email, name: buyer.name, data: { plan: "team" } },
});- Two calls for one new address both get the same account; the one whose insert lost reads the winner's row and answers
created: false. - The call does not prove the address. A new account is unverified, and the person proves the address the first time they sign in with a code or a link.
name,imageanddataare used for a new account only. - Whatever decides who may have an account decides here too:
validateUserInfo(which is toldmethod: "server") and auser.create.beforehook. Its refusal is thrown as it is. A hook can tell this call byctx.operationId === "findOrCreateUser". The waitlist plugin lets it through, closed or not, as it does an administrator'screateUser: provisioning from your server is not somebody signing up. - Call it outside a database transaction of your own. After a lost insert the transaction it ran in cannot be read from on PostgreSQL.
Accounts
Entro Auth supports multiple authentication methods through providers such as email and password, Google, or an enterprise identity provider. Each method linked to a user is stored as an account.
An account has a local record ID and a provider identity. id identifies the Entro Auth account record and is the value to pass as accountId to account-management APIs. The pair of providerId and accountId identifies the external account: providerId names the provider configuration, and accountId is the stable identifier that provider assigned. See the account schema for the complete set of fields.
accountId is the value to trust when your application needs the provider's own id for a person. See the provider's stable ID.
List User Accounts
Use listAccounts to retrieve every authentication method linked to the current user. Keep the returned id when you need to unlink the account or call another account-specific API.
import { authClient } from "@/lib/auth-client"
const { data: accounts, error } = await authClient.listAccounts();
if (error) {
throw new Error(error.message);
}
const googleAccount = accounts?.find((account) => account.providerId === "google");Token Encryption
Entro Auth doesn’t encrypt OAuth tokens by default. To store them encrypted, set account.encryptOAuthTokens to true. Entro Auth then encrypts the accessToken, refreshToken and idToken with your secret before it writes an account, and decrypts them when it reads them back, for example in getAccessToken.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
account: {
encryptOAuthTokens: true,
},
});Don't combine encryptOAuthTokens with your own encryption in databaseHooks. The tokens are already encrypted when the hook receives them, so the hook would encrypt them a second time. Entro Auth's own reads, such as getAccessToken, can't remove your outer layer, so they would fail or return unusable tokens.
If you need your own encryption scheme instead, leave encryptOAuthTokens off and encrypt the tokens in databaseHooks before they're saved to your database:
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
databaseHooks: {
account: {
create: {
before(account, context) {
const withEncryptedTokens = { ...account };
if (account.accessToken) {
const encryptedAccessToken = encrypt(account.accessToken)
withEncryptedTokens.accessToken = encryptedAccessToken;
}
if (account.refreshToken) {
const encryptedRefreshToken = encrypt(account.refreshToken);
withEncryptedTokens.refreshToken = encryptedRefreshToken;
}
return {
data: withEncryptedTokens
}
},
}
}
}
})Then whenever you retrieve back the account make sure to decrypt the tokens before using them. Entro Auth's own token reads, such as getAccessToken, return the stored value as it is in this setup.
Account Linking
Account linking is enabled by default and lets users associate multiple authentication methods with a single account. With Entro Auth, users can connect additional social sign-ons or OAuth providers to their existing accounts if the provider confirms the user's email as verified.
If account linking is disabled, no accounts can be linked, regardless of the provider or email verification status.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
account: {
accountLinking: {
enabled: false,
}
},
});Forced Linking
You can specify a list of "trusted providers." When a user logs in using a trusted provider, their account will be automatically linked even if the provider doesn’t confirm the email verification status. Use this with caution as it may increase the risk of account takeover.
Trusting a provider does not skip the check on the local account: accountLinking.requireLocalEmailVerified (default true) still applies, so a sign-in is only linked to an existing user whose own email is verified. Otherwise it fails with account_not_linked.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
account: {
accountLinking: {
enabled: true,
trustedProviders: ["google", "github"]
}
},
});Disable Implicit Linking
By default, when a user signs in with an OAuth provider whose email matches an existing user (and either the provider verified the email or it is in trustedProviders, and the existing user's email is verified), Entro Auth automatically links the OAuth account to that user. Set disableImplicitLinking: true to turn this off. With this option enabled:
- Same-email OAuth sign-ins for an existing user are rejected with the
account_not_linkederror instead of being silently linked, even when the provider is intrustedProvidersor the email is verified. - New users (no existing user with that email) can still sign up via OAuth.
- An already-authenticated user can still link providers explicitly via
linkSocial().
Use this when you want users to confirm linking from a settings page rather than implicitly on sign-in.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
account: {
accountLinking: {
disableImplicitLinking: true,
}
},
});Manually Linking Accounts
Users already signed in can manually link their account to additional social providers or credential-based accounts.
Linking a social provider adds a way of signing in, so linkSocial needs a fresh session, as unlinking does. A session that signed in longer ago than session.freshAge is refused with SESSION_NOT_FRESH (403); have the user sign in again and retry.
One case is let through from a session of any age: going back to a provider that is already linked, which is how more scopes are asked for. It adds nothing, because the link is bound to that account. If the provider comes back with a different account, or the account was unlinked meanwhile, the callback refuses with account_does_not_match and changes nothing. A person with two accounts of the same provider linked, and a link made with an idToken, still need a fresh session.
-
Linking Social Accounts: Use the
linkSocialmethod on the client to link a social provider to the user's account.import { authClient } from "@/lib/auth-client" await authClient.linkSocial({ provider: "google", // Provider to link callbackURL: "/callback" // Callback URL after linking completes });With a server-side session store, the session that started linking must still be active when the provider redirects back.
You can also request specific scopes when linking a social account, which can be different from the scopes used during the initial authentication:
import { authClient } from "@/lib/auth-client" await authClient.linkSocial({ provider: "google", callbackURL: "/callback", scopes: ["https://www.googleapis.com/auth/drive.readonly"] // Request additional scopes });Newly granted scopes are merged into
account.scope, so prior grants survive incremental authorization. Sign-in re-authentication and refresh-token responses do not modifyaccount.scope. Asking for more scopes goes throughlinkSocial; for a provider that is already linked it does not need a fresh session.You can also link accounts using ID tokens directly, without redirecting to the provider's OAuth flow:
import { authClient } from "@/lib/auth-client" await authClient.linkSocial({ provider: "google", idToken: { token: "id_token_from_provider", nonce: "nonce_used_for_token", // Optional accessToken: "access_token", // Optional, may be required by some providers refreshToken: "refresh_token" // Optional } });This is useful when you already have valid tokens from the provider, for example:
- After signing in with a native SDK
- When using a mobile app that handles authentication
- When implementing custom OAuth flows
The ID token must be valid and the provider must support ID token verification.
If you want your users to be able to link a social account with a different email address than the user, or if you want to use a provider that does not return email addresses, you will need to enable this in the account linking settings.
auth.ts import { entroAuth } from "@entro314labs/entro-auth"; export const auth = entroAuth({ account: { accountLinking: { allowDifferentEmails: true } }, });By default, linking an account leaves the existing user profile untouched. Enable
updateUserInfoOnLinkto copy the provider's profile onto the user each time an account is linked. The synced fields are the same ones persisted on sign-up (name,image, and any input-allowed fields yourmapProfileToUseradds). The user'semailandemailVerifiedare never changed on a link, so linking a provider can't rebind the account's identity.auth.ts import { entroAuth } from "@entro314labs/entro-auth"; export const auth = entroAuth({ account: { accountLinking: { updateUserInfoOnLink: true } }, }); -
Linking Credential-Based Accounts: To link a credential-based account (e.g., email and password), users can initiate a "forgot password" flow, or you can call the
setPasswordmethod on the server.set-password.ts import { auth } from "@/lib/auth" await auth.api.setPassword({ body: { newPassword: "new-password", // required }, headers: await headers() // headers containing the user's session token });
setPassword can't be called from the client for security reasons: a session alone would be enough to add a password to an account that has none. To offer it in an account screen, turn on emailAndPassword.allowSetPassword, which serves it over HTTP for a fresh session, or call it from a route of your own that first checks that the session signed in recently or asks for another proof.
Account Unlinking
Unlink an account by passing the Entro Auth account record's id, which you can obtain from listAccounts.
import { authClient } from "@/lib/auth-client"
const { data: accounts, error } = await authClient.listAccounts();
if (error) {
throw new Error(error.message);
}
const account = accounts?.find((account) => account.providerId === "google");
if (account) {
await authClient.unlinkAccount({
accountId: account.id,
});
}If the account does not exist or does not belong to the current user, Entro Auth returns an error. Entro Auth also prevents a user from unlinking their only account unless allowUnlinkingAll is true.
import { entroAuth } from "@entro314labs/entro-auth";
export const auth = entroAuth({
account: {
accountLinking: {
allowUnlinkingAll: true
}
},
});