useUser()
The useUser() hook exposes the active User object, allowing you to seamlessly integrate the user’s profile information—such as their first name, last name, profile image, and custom public metadata—directly into your frontend interface.
Import
import { useUser } from "@wacht/react-router" ;
Anatomy
Indicates whether the SDK is actively verifying the user’s identity asynchronously.
user
CurrentUser & { refetch: () => void } | null
The active CurrentUser object (including a refetch method to manually reload it), or null if the user is completely logged out.
Any error encountered during the user fetch operation.
Profile Management Methods
updateProfile
(data: ProfileUpdateData) => Promise<ApiResult<CurrentUser>>
The profile fields to update.
returns
Promise<ApiResult<CurrentUser>>
The updated user profile.
updateProfilePicture
(file: File) => Promise<ApiResult<unknown>>
Show Update profile picture
The image file to upload as the profile picture.
returns
Promise<ApiResult<unknown>>
The result of the profile picture update.
updatePassword
(currentPassword: string, newPassword: string) => Promise<ApiResult<unknown>>
The user’s current password.
returns
Promise<ApiResult<unknown>>
The result of the password update.
removePassword
(currentPassword: string) => Promise<ApiResult<unknown>>
The user’s current password.
returns
Promise<ApiResult<unknown>>
The result of the password removal.
deleteAccount
(password: string) => Promise<ApiResult<unknown>>
The user’s password to confirm account deletion.
returns
Promise<ApiResult<unknown>>
The result of the account deletion.
getEmailAddresses
() => Promise<ApiResult<UserEmailAddress[]>>
returns
Promise<ApiResult<UserEmailAddress[]>>
List of email addresses linked to the user.
getEmailAddress
(id: string) => Promise<ApiResult<UserEmailAddress>>
Show Get specific email address
The ID of the email address to fetch.
returns
Promise<ApiResult<UserEmailAddress>>
The specific email address.
createEmailAddress
(email: string) => Promise<ApiResult<UserEmailAddress>>
Show Create email address
The new email address to link.
returns
Promise<ApiResult<UserEmailAddress>>
The newly created email address.
deleteEmailAddress
(id: string) => Promise<ApiResult<unknown>>
Show Delete email address
The ID of the email address to remove.
returns
Promise<ApiResult<unknown>>
The result of the deletion.
prepareEmailVerification
(id: string) => Promise<ApiResult<unknown>>
Show Prepare email verification
The ID of the email address to verify.
returns
Promise<ApiResult<unknown>>
The result of dispatching the verification code.
attemptEmailVerification
(id: string, otp: string) => Promise<ApiResult<unknown>>
Show Attempt email verification
The ID of the email address being verified.
The verification code sent to the email address.
returns
Promise<ApiResult<unknown>>
The result of the verification attempt.
makeEmailPrimary
(id: string) => Promise<ApiResult<unknown>>
The ID of the email address to set as primary.
returns
Promise<ApiResult<unknown>>
The result of setting the primary email.
createPhoneNumber
(phone_number: string, country_code: string) => Promise<ApiResult<UserPhoneNumber>>
The new phone number to link.
The ISO country code for the phone number.
returns
Promise<ApiResult<UserPhoneNumber>>
The newly created phone number.
deletePhoneNumber
(id: string) => Promise<ApiResult<unknown>>
The ID of the phone number to remove.
returns
Promise<ApiResult<unknown>>
The result of the deletion.
preparePhoneVerification
(id: string) => Promise<ApiResult<unknown>>
Show Prepare phone verification
The ID of the phone number to verify.
returns
Promise<ApiResult<unknown>>
The result of dispatching the verification code.
attemptPhoneVerification
(id: string, otp: string) => Promise<ApiResult<unknown>>
Show Attempt phone verification
The ID of the phone number being verified.
The verification code sent to the phone number.
returns
Promise<ApiResult<unknown>>
The result of the verification attempt.
makePhonePrimary
(id: string) => Promise<ApiResult<unknown>>
The ID of the phone number to set as primary.
returns
Promise<ApiResult<unknown>>
The result of setting the primary phone number.
Authenticator (TOTP) Methods
Show Authenticator functions
setupAuthenticator
() => Promise<ApiResult<UserAuthenticator>>
returns
Promise<ApiResult<UserAuthenticator>>
The setup details (including secret and QR code URI) for a new TOTP authenticator.
verifyAuthenticator
(id: string, codes: string[]) => Promise<ApiResult<unknown>>
Show Verify authenticator
The ID of the authenticator being verified.
An array of codes generated by the authenticator app to verify setup.
returns
Promise<ApiResult<unknown>>
The result of verifying the authenticator.
deleteAuthenticator
(id: string) => Promise<ApiResult<unknown>>
Show Delete authenticator
The ID of the authenticator to remove.
returns
Promise<ApiResult<unknown>>
The result of removing the authenticator.
Show Backup Code functions
generateBackupCodes
() => Promise<ApiResult<string[]>>
Show Generate backup codes
returns
Promise<ApiResult<string[]>>
A list of newly generated MFA backup codes.
regenerateBackupCodes
() => Promise<ApiResult<string[]>>
Show Regenerate backup codes
returns
Promise<ApiResult<string[]>>
A list of newly generated MFA backup codes (invalidating previous ones).
connectSocialAccount
(params: { provider: string; redirectUri?: string }) => Promise<ApiResult<{ oauth_url: string }>>
Show Connect social account
The social provider to connect (e.g., ‘google’, ‘github’).
Optional URI to redirect back to after connecting.
returns
Promise<ApiResult<{ oauth_url: string }>>
The OAuth flow initialization URL to redirect the user to.
disconnectSocialConnection
(id: string) => Promise<ApiResult<unknown>>
Show Disconnect social connection
The ID of the social connection to disconnect.
returns
Promise<ApiResult<unknown>>
The result of the disconnection.
getPasskeys
() => Promise<ApiResult<UserPasskey[]>>
returns
Promise<ApiResult<UserPasskey[]>>
List of registered WebAuthn passkeys for the user.
registerPasskey
(name?: string) => Promise<ApiResult<unknown>>
An optional, human-readable name for the new passkey.
returns
Promise<ApiResult<unknown>>
The result of the native WebAuthn registration flow.
renamePasskey
(id: string, name: string) => Promise<ApiResult<unknown>>
The ID of the passkey to rename.
The new name for the passkey.
returns
Promise<ApiResult<unknown>>
The result of the renaming operation.
deletePasskey
(id: string) => Promise<ApiResult<unknown>>
The ID of the passkey to remove.
returns
Promise<ApiResult<unknown>>
The result of the deletion operation.
Usage: Personalized Greetings
The hallmark of a great user experience is personalization. You can easily access the user object to populate dashboard headers or welcome messages.
import { useUser } from "@wacht/react-router" ;
export function WelcomeBanner () {
const { loading , user } = useUser ();
if ( loading || ! user ) {
return null ; // Fail silently if not logged in
}
return (
< div className = "bg-blue-50 border border-blue-100 rounded-lg p-6 mb-8" >
< h2 className = "text-xl font-bold text-blue-900 tracking-tight" >
Welcome back, { user . firstName || "friend" } { /* Fallback if no first name */ }
</ h2 >
< p className = "text-blue-700 mt-1" >
Your primary email is currently mapped to { user . primaryEmailAddress } .
</ p >
</ div >
);
}
Wacht allows you to attach custom JSON metadata to individual User objects from the Dashboard or Backend API. This is incredibly useful for storing application-specific state (like a user’s theme preference, onboarding status, or stripe customer ID) directly on the identity layer.
If a piece of metadata is flagged as public, it becomes securely accessible directly from the frontend React SDK via this hook:
import { useUser } from "@wacht/react-router" ;
export function ThemeController () {
const { loading , user } = useUser ();
if ( loading || ! user ) return null ;
// Access the custom "publicMetadata" object securely attached via the backend
const userPreferredTheme = user . publicMetadata . theme || "light" ;
return (
< div className = "text-sm text-zinc-500 mt-4" >
System rendering in module: < span className = "font-bold" > { userPreferredTheme } </ span >
</ div >
);
}
Updating User Data
The user object returned by this hook is a rich instance possessing its own suite of update functions. You can programmatically update a user’s profile directly from the client (provided the updates conform to your deployment’s configuration policies).
const handleUpdateName = async () => {
try {
const updatedUser = await user . update ({ firstName: "Jonathan" });
console . log ( "Profile successfully updated!" , updatedUser );
} catch ( err ) {
console . error ( "Failed to update profile:" , err );
}
};