CurrentUser

CurrentUser and CurrentServerUser

CurrentUser and CurrentServerUser provide functionalities like getting user profile, updating user information, send email verification, etc. CurrentServerUser is a subclass of CurrentUser and provides some additional functionalities like deleting users and server only metadata.

CurrentUser Object

You can call useUser() or stackServerApp.getUser() to get the CurrentUser object.

Attributes

  • id (string): The user ID. This is the unique identifier of the user.
  • displayName (string | null): The display name of the user. Can be changed by the user.
  • primaryEmail (string | null): The primary email of the user. Note: this is not unique.
  • primaryEmailVerified (boolean): Whether the primary email is verified.
  • profileImageUrl (string | null): The profile image URL of the user.
  • signedUpAt (Date): The Date when the user signed up.
  • emailAuthEnabled (boolean): Whether the user has an email authentication method (magic link or password).
  • hasPassword (boolean): Whether the user has a password set.
  • oauthProviders (string[]): The list of OAuth provider ID s the user has connected.
  • clientMetadata (object): The JSON metadata that is visible on the client side. Note that this should not contain information that should be kept private on the server side.

Methods

update

This method can update the user’s profile information. Here is an example of how to update the user’s display name and set the client metadata.

1await user.update({
2 displayName: "New Display Name",
3 clientMetadata: {
4 address: "123 Main St",
5 },
6});

signOut

This will sign out the user and clear the session.

Example:

1await user.signOut();

sendEmailVerification

This will send an email verification link to the user’s primary email. It will return an error object (not throw an error) if the email is already verified. If successful, it will return undefined.

Example:

1const error = await user.sendVerificationEmail();
2if (error) {
3 console.error("Error sending email verification", error);
4} else {
5 console.log("Email verification sent");
6}

updatePassword

This will update the user’s password. It will return an error object (not throw an error) if the passwords mismatch or if the new password does not meet the requirements. If successful, it will return undefined.

Example:

1const error = await user.updatePassword({
2 currentPassword: "oldPassword",
3 newPassword: "newPassword",
4});
5if (error) {
6 console.error("Error updating password", error);
7} else {
8 console.log("Password updated");
9}

CurrentServerUser

If you call getUser() on a StackServerApp, you will get the CurrentServerUser object. It contains all the fields and methods of CurrentUser, but also has some additional data.

Attributes

  • serverMetadata: The JSON metadata that is visible on the server side. This contains information that should be kept private on the server side. Do not share this with the client.

Methods

delete

This will delete the user. Use it with caution as it is irreversible.

Example:

1await serverUser.delete();