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:

This example assumes you are using the setShortSessionCookie option in the Corbado.load() function, so that a cookie with the users shortSession JWT will be set on login. Find more info here.

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 shortSession = cookies.cbo_short_session;

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

    try {
        const user = await sdk.sessions().validateToken(shortSession);
        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.