Back to Blitz

README

packages/blitz-auth/README.md

3.0.24.1 KB
Original Source

<!-- prettier-ignore-start --> <p align="center"> <a aria-label="Join our Discord Community" href="https://discord.blitzjs.com"> </a> <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> <a aria-label="All Contributors" href="#contributors-"></a> <!-- ALL-CONTRIBUTORS-BADGE:END --> <a aria-label="License" href="https://github.com/blitz-js/blitz/blob/main/LICENSE"> </a> <a aria-label="NPM version" href="https://www.npmjs.com/package/@blitzjs/auth"> </a> </p> <!-- prettier-ignore-end --> <h1 align="center"> Blitz Auth - Framework Agnostic Authentication </h1>

Quick Start

Install Blitz Auth

bash
`npm i @blitzjs/auth`
# yarn add @blitzjs/auth
# pnpm add @blitzjs/auth

You can alternatively use npx

Framework Support

Currently Blitz Auth usage is only documented with Next.js. We are working on adding additional support for other frameworks.

Setup Blitz Auth in Next.js

Client setup

Add the following to your blitz-client.ts file:

ts
import {AuthClientPlugin} from "@blitzjs/auth"
import {setupBlitzClient} from "@blitzjs/next"

export const authConfig = {
  cookiePrefix: "testapp",
}

const {withBlitz} = setupBlitzClient({
  plugins: [AuthClientPlugin(authConfig)],
})

export {withBlitz}
Server setup

Then, add the following to the blitz-server.ts file:

ts
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}