Interface: Auth
Authentication configuration and lifecycle hooks.
See the Auth overview for the
supported auth methods and how the User entity is connected to auth.
If hooks are async, Wasp awaits them. All hooks receive prisma and req
in their input. Hook return values are ignored except for
AuthHooks.onBeforeOAuthRedirect, which can change the redirect URL.
In TypeScript, you can type each hook implementation with its matching
type from wasp/server/auth (e.g. OnBeforeSignupHook). See
Auth Hooks for the full hook
inputs and examples.
Example
import { app } from "@wasp.sh/spec"
export default app({
// ...
auth: {
userEntity: "User",
methods: {
usernameAndPassword: {}, // use this or email, not both
google: {},
gitHub: {},
},
onAuthFailedRedirectTo: "/login",
},
})
Properties
onAuthFailedRedirectTo
onAuthFailedRedirectTo:
string
Route that Wasp redirects unauthenticated users to when they try to
access a page that has authRequired: true.
See Adding Auth to the Project for an example.
provider
provider:
AuthProviderConfig
The authentication provider that establishes and verifies user identity.
Either Wasp's own auth, configured with waspAuth:
auth: {
userEntity: "User",
onAuthFailedRedirectTo: "/login",
provider: waspAuth({ methods: { email: { ... } } }),
}
or an external provider, through an adapter package's spec helper (or customAuthProvider for a hand-written adapter):
import { clerk } from "@wasp.sh/auth-clerk/spec";
auth: {
userEntity: "User",
onAuthFailedRedirectTo: "/login",
provider: clerk(),
}
The choice is a discriminated union, so provider-specific configuration
(auth methods, wasp auth hooks) is only expressible under the provider it
belongs to. Wasp still owns everything downstream of authentication:
context.user is always a row in your own user entity, whichever provider
vouched for the request.
userEntity
userEntity:
string
Name of the Prisma model that represents the application user connected to your business logic.
The user entity needs to have an ID field that uniquely identifies each
user. It can be of any name and type, but it needs to be marked with @id:
model User {
id Int @id @default(autoincrement())
}
You can add any other fields you want to the user entity. Make sure to also
define them in the userSignupFields field if they need to be set during
the sign-up process.
See Accessing User Data for how the user entity connects to the rest of the auth system.
userEntity is provider-independent: whichever provider authenticates a
request, context.user is always a row of this entity.