Skip to content
On this page

Function: withOcAuth()

TypeScript
function withOcAuth<Req, Res>(handler: (req: WithOcSession<Req>, res: Res) => unknown, options?: WithOcAuthOptions): (req: Req, res: Res) => Promise<unknown>;

Defined in: me-client/src/server.ts:824 (opens in a new tab)

Wrap a Next.js Pages-Router API route handler. Adds req.ocSession — the verified SessionPayload, or null when unauthenticated.

Code
// pages/api/me/profile.ts
import { withOcAuth } from '@orangecheck/me-client/server';

export default withOcAuth(async (req, res) => {
  if (!req.ocSession) return res.status(401).json({ ok: false });
  res.status(200).json({ account: { address: req.ocSession.did_oc } });
}, { required: true, audience: 'https://your-site.example' });

audience is your own origin. The sign-in popup issues each site a token bound to it; a site on *.ochk.io that reads the family cookie omits it.

Cache-Control: no-store, private and Vary: Cookie, Authorization are set on every authenticated response — without them a cached 401 leaks anonymous to authenticated callers.

Type ParametersLink to this section

Type ParameterDefault type
Req extends NextApiRequestLikeNextApiRequestLike
Res extends NextApiResponseLikeNextApiResponseLike

ParametersLink to this section

ParameterType
handler(req: WithOcSession<Req>, res: Res) => unknown
optionsWithOcAuthOptions

ReturnsLink to this section

(req: Req, res: Res) => Promise<unknown>