Install @blitzjs/auth
plugin with:
npm i @blitzjs/auth # yarn add @blitzjs/auth # pnpm add @blitzjs/auth
Add the following to your blitz-client.ts
file:
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupBlitzClient } from "@blitzjs/next"
export const authConfig = {
cookiePrefix: "testapp",
}
const { withBlitz } = setupBlitzClient({
plugins: [AuthClientPlugin(authConfig)],
})
export { withBlitz }
Then, add the following to the blitz-server.ts
file:
import { setupBlitzServer } from "@blitzjs/next"
import {
AuthServerPlugin,
PrismaStorage,
simpleRolesIsAuthorized,
} from "@blitzjs/auth"
import { db } from "db"
import { authConfig } from "./blitz-client"
const { gSSP, gSP, api } = setupBlitzServer({
plugins: [
AuthServerPlugin({
...authConfig,
storage: PrismaStorage(db),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export { gSSP, gSP, api }
In production, you must provide the SESSION_SECRET_KEY
environment
variable with at least 32 characters. This is your private key for signing
JWT tokens.
On macOS and Linux, you can generate it by running openssl rand -hex 16
in your terminal.