Getting the current user
On both client and server, you can use thegetUser() function to get the current user (or null if not signed in). In React apps, there is also a useUser() hook which automatically updates when the value changes.
my-app.ts
Protecting a page & requiring a signed-in user
Sometimes, you want to retrieve the user only if they’re signed in, and redirect to the sign-in page otherwise. In this case, simply pass{ or: "redirect" }, and the function will never return null.
You can also use { or: "throw" } to throw an error instead — useful in API routes and server actions where a redirect doesn’t make sense.
In both cases, the return type is non-nullable, so you don’t need to handle null.
my-app.ts
User data
What’s on a user object?
Before diving into updates, here’s an overview of the most important fields available on every user: For the full list of fields and methods, see the User SDK reference.Updating a user
You can update attributes on a user object with theuser.update() function.
my-app.ts
Custom metadata
Beyond built-in fields likedisplayName and primaryEmail, you can store your own JSON-like data on a user. Hexclave provides three metadata fields, each with different read and write permissions:
| Field | Client access | Server access | Use for |
|---|---|---|---|
clientMetadata | Read and write | Read and write | Non-sensitive user preferences, such as theme, locale, onboarding form drafts, or UI state. |
serverMetadata | No access | Read and write | Sensitive or internal data that users should not be able to see or modify. |
clientReadOnlyMetadata | Read only | Read and write | Server-authoritative data that the client should display, such as subscription plans, role labels, or validated onboarding state. |
clientMetadata for information that is safe for the browser to read and change. For example, a user can store their own theme preference:
my-app.ts
serverMetadata for sensitive or internal data. It is only available through HexclaveServerApp:
my-server-file.ts
clientReadOnlyMetadata when the client needs to read a value but only your server should decide it. A common example is storing a validated onboarding or subscription state:
my-server-file.ts
Signing out
You can sign out the user by callinguser.signOut(). They will be redirected to the URL configured as afterSignOut in the Hexclave App.
my-app.ts
Example: Custom profile page
Hexclave automatically creates a user profile on sign-up. Let’s build a page that displays this information. Inapp/profile/page.tsx:
my-app.tsx
User object, check the the SDK documentation.
Anonymous users
Hexclave supports anonymous users - users who can interact with your app without signing up. This is useful for features like guest checkouts, try-before-you-sign-up flows, or collecting analytics before a user creates an account. If you have the analytics app enabled, anonymous users will automatically be created and visible in the Users table. In most ways, anonymous users are just like any other, however, there are some key differences:- Unless opted in, anonymous users cannot access any backend functions that require a signed-in user.
- By default, anonymous users are not returned by
getUser(),useUser(). - Anonymous users have a different set of JWT keys, which means they cannot access protected routes or API endpoints.
- Anonymous users are always restricted.
{ or: "anonymous" } to useUser() or getUser(). This will create an anonymous user if the user isn’t signed in.
my-app.ts
isAnonymous property set to true.