Client provider
Wrap wrapRootElement in gatsby-browser.js and gatsby-ssr.js once, and session state is available across every page and component.
- Browser and SSR
- Every page and component
Gatsby
Add auth with a React client provider and server helpers for Gatsby Functions. Sessions use the same cookie and OIDC flow as every other Authdog SDK, so one environment works across your stack.
Client provider
// gatsby-browser.js
import { AuthdogProvider } from "@authdog/gatsby/client"
export const wrapRootElement = ({ element }) => (
<AuthdogProvider>{element}</AuthdogProvider>
)
Protect a Gatsby Function
// src/api/me.ts
import { createAuthdog } from "@authdog/gatsby/server"
const authdog = createAuthdog({ publicKey: process.env.PK_AUTHDOG! })
export default authdog.requireAuth((req, res) => {
res.json(req.authdog.user)
})
Client provider meets Functions
A provider for the browser and typed helpers for Gatsby Functions: session state in the UI, real enforcement on the server.
Wrap wrapRootElement in gatsby-browser.js and gatsby-ssr.js once, and session state is available across every page and component.
createAuthdog() gives you getSession, getUser, requireAuth, and logout as first-class helpers inside src/api routes.
requireAuth is the enforcement point. It validates the session in your Functions before the handler ever runs.
Uses the same authdog-session cookie and OIDC userinfo flow as every other @authdog/* package, so one environment spans your stack.
The provider strips the login redirect's ?token= and reloads once so the server can persist the HttpOnly session cookie.
req.authdog.user is typed end to end inside Functions, and the client hooks are typed for your components.
Add @authdog/gatsby and your environment keys.
Add the provider to gatsby-browser.js and gatsby-ssr.js.
Wrap src/api handlers in requireAuth.
Install the SDK, wrap your root element, and protect your Functions today. Free to start, with secure defaults built in.