diff --git a/README.md b/README.md new file mode 100644 index 0000000..bb6a45e --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# sf-auth-nextjs-middleware + +Nextjs middleware and App Router callback handler for sf-auth. + +## Install + +```bash +npm install sf-auth-nextjs-middleware +``` + +## Usage + +### Middleware + +Create a middleware that redirects unauthenticated users to the sf-auth +authenticate endpoint. + +```ts +// middleware.ts +import { createSfAuthMiddleware } from "sf-auth-nextjs-middleware"; + +export const middleware = createSfAuthMiddleware( + "https://your-site.com/auth_callback" +); +``` + +### Callback route + +Create an App Router route to handle the sf-auth callback, validate the +one-time key, set cookies, and redirect to the desired page. + +```ts +// app/auth_callback/route.ts +import { createSfAuthCallbackRoute } from "sf-auth-nextjs-middleware"; + +export const GET = createSfAuthCallbackRoute({ + redirectTo: "/user_info" +}); +``` + +## Configuration + +Both helpers accept optional configuration to customize cookie names and +endpoints. + +```ts +import { + createSfAuthMiddleware, + createSfAuthCallbackRoute +} from "sf-auth-nextjs-middleware"; + +export const middleware = createSfAuthMiddleware( + "https://your-site.com/auth_callback", + { + cookieNames: { + userId: "sf_user_id", + username: "sf_username" + } + } +); + +export const GET = createSfAuthCallbackRoute({ + redirectTo: "/user_info", + cookieNames: { + userId: "sf_user_id", + username: "sf_username" + }, + validateEndpoint: "https://snazzyfellas.com/api/redirect/validate" +}); +```