This guide is only applicable to SvelteKit applications, as they can contain code that runs on the server-side.

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

routes/profile/+page.server.ts
import { redirect, type RequestEvent } from '@sveltejs/kit';
import { PUBLIC_CORBADO_PROJECT_ID } from '$env/static/public';
import { CORBADO_API_SECRET } from '$env/static/private';
import { Config, SDK } from '@corbado/node-sdk';

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

export async function load({ cookies }: RequestEvent) {
    const sessionToken = cookies.get('cbo_session_token');
    if (!sessionToken) {
        return redirect(302, "/")
    }

    try {
        const user = await sdk.sessions().validateToken(sessionToken);
        return { user: { name: user.fullName, userID: user.userId } };
    } catch (e) {
        // session cookie was invalid
        return redirect(302, "/")
    }
}

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