TypeScript
Learn how to configure Aura Auth with TypeScript.
Aura Auth is built with TypeScript, providing type safety and autocompletion for a better development experience. This guide walks you through configuring Aura Auth with TypeScript, ensuring you can leverage its full potential in your projects.
Overview
Aura Auth uses TypeScript generics to let you define the shape of the data it shares and manages. The library is designed to preserve type inference across its data, functions, and methods, but you can also provide your own types to improve type safety and autocompletion in your TypeScript applications.
TypeScript Configuration
Strict Mode
Enable strict mode in your tsconfig.json file so TypeScript checks types more strictly and gives you stronger type safety and autocompletion.
{
"compilerOptions": {
"strict": true,
"alwaysStrict": true
}
}Aliases
Define path aliases in your tsconfig.json to simplify imports and improve code readability.
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}Type and Linter Checking
Enable type and lint checking to enforce clean code and catch potential issues early.
{
"compilerOptions": {
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"strictFunctionTypes": true
}
}Type Extension
For the type extension, Aura Auth provides the identity.schema configuration option. This allows you to define a custom validation schema for user identity data, ensuring that the data conforms to a specific structure and set of fields and this schema is used as User and Session types across the library. By default, Aura Auth accepts a flexible identity shape, but you can provide your own schema to enforce a specific structure and set of fields.
import { z } from "zod"
import { createAuth } from "@aura-stack/auth"
import { UserIdentity, type InferUser, type InferSession } from "@aura-stack/auth/identity"
const schema = UserIdentity.extend({
role: z.enum(["admin", "user"]),
permissions: z.array(z.string()),
})
export const auth = createAuth({
oauth: [],
identity: {
schema,
},
})
export type User = InferUser<typeof auth>
export type Session = InferSession<typeof auth>Or, if you prefer to infer the types from the schema:
import { z } from "zod"
import { createAuth } from "@aura-stack/auth"
import { UserIdentity, type UserFrom, type SessionFrom } from "@aura-stack/auth/identity"
const schema = UserIdentity.extend({
role: z.enum(["admin", "user"]),
permissions: z.array(z.string()),
})
type User = UserFrom<typeof schema>
type Session = SessionFrom<typeof schema>
export const auth = createAuth({
oauth: [],
identity: {
schema,
},
})