@authaz/next
v2.3.0
Published
NextJS authaz SDK
Readme
@authaz/next
Next.js SDK for Authaz authentication.
Installation
npm install @authaz/next @authaz/sdk
# or
pnpm add @authaz/next @authaz/sdk
# or
yarn add @authaz/next @authaz/sdkUsage
API Routes Setup
// app/api/auth/[...authaz]/route.ts
import { createAuthazHandler } from '@authaz/next';
export const { GET, POST } = createAuthazHandler({
clientId: process.env.AUTHAZ_CLIENT_ID!,
clientSecret: process.env.AUTHAZ_CLIENT_SECRET!,
});Management API Calls
The createAuthazHandler() management proxy is session-protected and forwards
requests for the signed-in user. Do not use it for API-key-only management
operations such as listing roles from a server. For those calls, create a
server-side @authaz/sdk client and keep AUTHAZ_API_KEY in environment
variables:
import { createAuthazClient } from '@authaz/sdk';
const authaz = createAuthazClient({
apiKey: process.env.AUTHAZ_API_KEY!,
baseAddress: process.env.AUTHAZ_API_BASE_URL,
});
const roles = await authaz.roles.list();Middleware
// middleware.ts
import { withAuthaz } from '@authaz/next';
export default withAuthaz();
export const config = {
matcher: ['/dashboard/:path*', '/api/:path*'],
};Server Components
import { getSession } from '@authaz/next';
async function Page() {
const session = await getSession();
if (!session) {
return <div>Not authenticated</div>;
}
return <div>Hello, {session.user?.name}</div>;
}Documentation
For full documentation, visit https://authaz.io/docs
License
MIT
