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 BackendAPI.

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 event handler function to verify the user is authenticated and retrieve some data in this case:

server/api/user-info.ts
import { SDK, Config } from "@corbado/node-sdk";
import { defineEventHandler, H3Event, parseCookies } from "h3";

const projcetId = "pro-XXX";

const config = new Config(
    projcetId,
    "corbado1_XXX",
    `https://${projcetId}.frontendapi.corbado.io`,
    "https://backendapi.cloud.corbado.io",
);
const sdk = new SDK(config);

export default defineEventHandler(async (event: H3Event) => {
    const cookies = parseCookies(event);
    const sessionToken = cookies.cbo_session_token;

    if (!sessionToken) {
        return {
            user: undefined,
        };
    }

    try {
        const user = await sdk.sessions().validateToken(sessionToken);
        return {
            id: user.userId,
        };
    } catch (e) {
        console.log(e)
        return {
            id: undefined,
        };
    }
});

Find more info on the Corbdo Node.js SDK in the backend integration section.