Custom User Data

How to store custom user metadata in Stack Auth

Stack Auth allows storing additional user information through three types of metadata fields:

  1. clientMetadata: Readable and writable from a client.
  2. serverMetadata: Readable and writable only from a server.
  3. clientReadOnlyMetadata: Readable from a client, writable only from a server.

Client metadata

You can use the clientMetadata field to store non-sensitive information that both the client and server can read and write.

1await user.update({
2 clientMetadata: {
3 mailingAddress: "123 Main St",
4 },
5});
6
7// On the client:
8const user = useUser();
9console.log(user.clientMetadata);

Server-side metadata

For sensitive information, use the serverMetadata field. This ensures the data is only accessible and modifiable by the server.

1const user = await stackServerApp.getUser();
2await user.update({
3 serverMetadata: {
4 secretInfo: "This is a secret",
5 },
6});
7
8// To read:
9const user = await stackServerApp.getUser();
10console.log(user.serverMetadata);

Client read-only metadata

Use clientReadOnlyMetadata for data that clients need to read but never modify, such as subscription status.

1// On the server:
2const user = await stackServerApp.getUser();
3await user.update({
4 clientReadOnlyMetadata: {
5 subscriptionPlan: "premium",
6 },
7});
8
9// On the client:
10const user = useUser();
11console.log(user.clientReadOnlyMetadata);