Prerequisites
Install the Corbado Node.js SDK:
npm install @corbado/node-sdk
Generate an API secret in the developer panel
to allow the SDK to access the Corbado Backend API.
Example
To gain access to authentication state on the server-side we utilize the Corbdo Node.js SDK.
We might use it in a server-side action function to verify the user is authenticated and retrieve some data in this case:
"use server";
import { redirect } from "next/navigation";
import { cookies } from "next/headers";
import { Config, SDK } from '@corbado/node-sdk';
const config = new Config(
process.env.PUBLIC_CORBADO_PROJECT_ID, process.env.CORBADO_API_SECRET,
`https://${process.env.PUBLIC_CORBADO_PROJECT_ID}.frontendapi.corbado.io`,
'https://backendapi.cloud.corbado.io'
);
const sdk = new SDK(config);
export async function getUser() {
const sessionToken = cookies().get('cbo_session_token')?.value;
if (!sessionToken) {
redirect("/")
}
try {
const user = await sdk.sessions().validateToken(sessionToken);
return { user: { name: user.fullName, userID: user.userId } };
} catch (e) {
// session cookie was invalid
redirect("/")
}
}