Email and Password
Users often expect to sign in to your site with a password. Openfort Auth helps you implement password-based authentication safely, using secure configuration options and best practices for storing and verifying passwords.
Sign up a user
Call signUpWithEmailPassword() with the user's email address, password, and optional display name.
The method returns either an AuthResponse with the user's token and profile, or an AuthActionRequiredResponse if email verification is required before the user can log in.
import openfort from "./openfortConfig"
async function signUpNewUser(email: string, password: string, name: string) {
const result = await openfort.auth.signUpWithEmailPassword({
email: email,
password: password,
name: name,
callbackURL: 'https://your-app.com/verify-email',
});
if ('action' in result) {
console.log('Please verify your email before logging in');
return;
}
console.log('User signed up:', result.user.id);
}To require email verification, send a verification email after sign up:
await openfort.auth.requestEmailVerification({
email: email,
redirectUrl: 'http://example.com/account/register',
});Log in a user
When your user signs in, call logInWithEmailPassword() with their email address and password.
The method returns an AuthResponse with the user's token and profile.
import openfort from "./openfortConfig"
async function logInUser(email: string, password: string) {
const result = await openfort.auth.logInWithEmailPassword({
email: email,
password: password
});
console.log('User logged in:', result.user.id);
}Log in with OTP
Use one-time passcode (OTP) authentication for passwordless login via email.
Request OTP
Call requestEmailOtp() to send a one-time passcode to the user's email address:
await openfort.auth.requestEmailOtp({ email: '[email protected]' });Verify OTP and log in
Call logInWithEmailOtp() with the email and the OTP code the user received:
import openfort from "./openfortConfig"
async function logInWithOtp(email: string, otp: string) {
const result = await openfort.auth.logInWithEmailOtp({
email: email,
otp: otp
});
console.log('User logged in:', result.user.id);
}Reset a password
Request password reset
Create a reset password page. Collect the user's email address and request a password reset email with a redirect URL pointing to your change password page:
await openfort.auth.requestResetPassword({
email: '[email protected]',
redirectUrl: 'https://example.com/account/update-password',
});Reset password with token
Create a change password page at the URL you specified. Collect the user's new password and pass the reset token from the URL query parameters:
await openfort.auth.resetPassword({
password: 'new-password',
token: 'reset-token-from-url',
});