Skip to content

React SDK — Authentication

The useAuth hook provides the full authentication API. Import it from @aerostack/react.

import { useAuth } from '@aerostack/react'
const {
user, // User | null
tokens, // { accessToken, refreshToken?, expiresAt? } | null
loading, // boolean — any auth operation in progress
error, // string | null — last error message
isAuthenticated, // boolean — shorthand for !!tokens?.accessToken
} = useAuth()
interface User {
id: string
email: string
name?: string
avatar_url?: string
emailVerified: boolean
createdAt?: string
customFields?: Record<string, any>
}

Sign in with email and password. Sets user and tokens on success.

await signIn('user@example.com', 'password')
// or with Turnstile bot protection:
await signIn('user@example.com', 'password', turnstileToken)
ParameterTypeRequiredDescription
emailstringYesUser’s email address
passwordstringYesUser’s password
turnstileTokenstringNoCloudflare Turnstile token

Returns: Promise<AuthResponse>{ user, accessToken, refreshToken }


Register a new user. If email verification is enabled, requiresVerification: true is returned.

const result = await signUp('user@example.com', 'password', {
name: 'Jane Doe',
customFields: { plan: 'free' },
turnstileToken: token,
})
if (result.requiresVerification) {
// Show "check your email" message
}
ParameterTypeRequiredDescription
emailstringYesNew user’s email
passwordstringYesNew user’s password
opts.namestringNoDisplay name
opts.customFieldsRecord<string, any>NoCustom profile fields
opts.turnstileTokenstringNoBot protection token

Sign out the current user. Invalidates the refresh token server-side and clears local state.

await signOut()
// user and tokens are now null

Send a one-time code to an email address or phone number.

await sendOTP('user@example.com', 'email')
await sendOTP('+1234567890', 'phone')
ParameterTypeDefaultDescription
identifierstringEmail or phone number
type'email' | 'phone''email'Delivery method

Verify the OTP code and sign in. Sets user and tokens on success.

await verifyOTP('user@example.com', '123456', 'email')
ParameterTypeDefault
identifierstring
codestring
type'email' | 'phone''email'

Verify the email address using the token from the verification email link.

// In your /verify-email page
const token = new URLSearchParams(window.location.search).get('token')
await verifyEmail(token)

Resend the email verification link.

await resendVerificationEmail('user@example.com')

requestPasswordReset(email, turnstileToken?)

Section titled “requestPasswordReset(email, turnstileToken?)”

Send a password reset email.

await requestPasswordReset('user@example.com')

Set a new password using the token from the reset email.

const token = new URLSearchParams(window.location.search).get('token')
await resetPassword(token, 'newSecurePassword123')

Refresh the access token. Called automatically by the SDK on 401 responses, but can also be called manually.

const newTokens = await refreshAccessToken(tokens.refreshToken)

Re-fetch the current user’s profile and update local state.

await refreshUser()
// user state is updated from the server

Update the current user’s profile. Calls refreshUser() automatically.

await updateProfile({
name: 'New Name',
avatar_url: 'https://cdn.example.com/avatar.jpg',
customFields: { plan: 'pro' },
})
ParameterTypeDescription
namestringDisplay name
avatar_urlstringAvatar URL
avatar_image_idstringStorage image ID (auto-resolves to URL)
customFieldsRecord<string, any>Merge into existing custom fields

Remove the current user’s avatar.

await deleteAvatar()

All methods throw on failure. The error message is also set in error state:

const { signIn, error, loading } = useAuth()
const handleSubmit = async () => {
try {
await signIn(email, password)
} catch (err) {
// err.message is the same as error state
console.error(err.message)
}
}
// Or use the error state directly:
{error && <p style={{ color: 'red' }}>{error}</p>}