wasp/auth
Auth APIs you can use both on the client and on the server: the AuthUser type of the logged-in user, and helpers for reading auth data from a user entity object.
Only available if your app has auth enabled.
Type Aliases
AuthUser
AuthUser =
UserEntity&object
The logged-in user, as you receive it on the client (from
useAuth or
the user prop of pages with authRequired) and on the server (as
context.user in Operations and APIs, and socket.data.user in
WebSockets).
Its shape depends on your app: it has all the fields you defined on your
user entity at the top level, and the auth-related data of each auth method
your app enables in the identities field. Wasp strips the hashed passwords
out of it.
Type Declaration
getFirstProviderUserId
getFirstProviderUserId: () =>
string|null
Returns the first user ID that it finds for the user, or null if they
don't have any. For example, if the user signed up with email, it returns
their email. If they signed up with Google, it returns their Google ID.
This is useful if you support multiple auth methods and you need any ID that identifies the user in your app.
Returns
string | null
identities
identities:
AuthUserIdentities
Examples
For a user that signed up with email, in an app with email and Google auth, it looks like this:
const user = {
// User data
id: "cluqsex9500017cn7i2hwsg17",
address: "Some address",
// Auth methods specific data
identities: {
email: {
id: "user@app.com",
isEmailVerified: true,
emailVerificationSentAt: "2024-04-08T10:06:02.204Z",
passwordResetSentAt: null,
},
google: null,
},
};
Typing the user prop of a page with authRequired:
import type { AuthUser } from "wasp/auth";
export const AccountPage = ({ user }: { user: AuthUser }) => {
const userId = user.getFirstProviderUserId();
// ...
};
See
Functions
getEmail()
getEmail(
user):string|null
Returns the user's email if they signed up with
Email auth, or null
otherwise.
Parameters
user
Returns
string | null
Example
import { getEmail } from "wasp/auth";
const email = getEmail(task.user);
getFirstProviderUserId()
getFirstProviderUserId(
user?):string|null
Returns the user's ID for one of their auth methods, or null if you don't
pass a user or they don't have any.
Works like the getFirstProviderUserId method of AuthUser, which
you can call instead if you have one.
Parameters
user?
Returns
string | null
Example
import { getFirstProviderUserId } from "wasp/auth";
const userId = getFirstProviderUserId(task.user);
getUsername()
getUsername(
user):string|null
Returns the user's username if they signed up with
Username & password
auth, or null otherwise.
Parameters
user
Returns
string | null
Example
import { getUsername } from "wasp/auth";
const username = getUsername(task.user);
Referenced Types
AuthUserIdentities
The user's auth data. It only has properties for the auth methods your app
enables. A property's value is null if the user doesn't have an identity
for that auth method, so check it before using it.
Example
if (user.identities.email !== null) {
const email = user.identities.email.id;
// ...
} else if (user.identities.google !== null) {
const googleId = user.identities.google.id;
// ...
}
Properties
discord?
optionaldiscord?: {id:string; } |null
The user's Discord auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's Discord ID.
null
email?
optionalemail?: {emailVerificationSentAt:string|null;id:string;isEmailVerified:boolean;passwordResetSentAt:string|null; } |null
The user's Email auth data.
Union Members
Type Literal
{ emailVerificationSentAt: string | null; id: string; isEmailVerified: boolean; passwordResetSentAt: string | null; }
emailVerificationSentAt
emailVerificationSentAt:
string|null
Datetime when the email verification email was sent.
id
id:
string
The user's email address.
isEmailVerified
isEmailVerified:
boolean
true if the user has verified their email address.
passwordResetSentAt
passwordResetSentAt:
string|null
Datetime when the last password reset email was sent.
null
github?
optionalgithub?: {id:string; } |null
The user's GitHub auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's GitHub ID.
null
google?
optionalgoogle?: {id:string; } |null
The user's Google auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's Google ID.
null
keycloak?
optionalkeycloak?: {id:string; } |null
The user's Keycloak auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's Keycloak ID.
null
microsoft?
optionalmicrosoft?: {id:string; } |null
The user's Microsoft auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's Microsoft ID.
null
slack?
optionalslack?: {id:string; } |null
The user's Slack auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's Slack ID.
null
username?
optionalusername?: {id:string; } |null
The user's Username & password auth data.
Union Members
Type Literal
{ id: string; }
id
id:
string
The user's username.
null
UserEntity
Your app's user entity: the Prisma model you set as auth.userEntity in
your app spec (usually User).
Wasp generates the real type from your Prisma schema, so it has the fields you defined on that model.
UserEntityAuthIdentity
One of the auth identities in a UserEntityWithAuth.
Properties
providerName
providerName:
string
The auth method's name, e.g. "email" or "google".
providerUserId
providerUserId:
string
The user's ID for that auth method, e.g. their email or Google ID.
UserEntityWithAuth
UserEntityWithAuth =
UserEntity&object
A user entity object fetched with its auth relation, and the auth's
identities relation, included. An AuthUser doesn't have the auth
relation, so read its identities field instead.
For example, the user field of a Task you fetched with:
const tasks = await context.entities.Task.findMany({
include: {
user: {
include: {
auth: {
include: {
identities: {
select: { providerName: true, providerUserId: true },
},
},
},
},
},
},
});
Type Declaration
auth
auth: {
identities:UserEntityAuthIdentity[]; } |null