Skip to content
Back

How to get current user account session from server side

  • 0
  • Auth
amaturcodrgui
4 Jan, 2026, 19:06

i am using sveltekit and i'm a bit unclear on how to get the current user's session from the server side.

thisi s what i have so far // src/lib/server/appwrite.js import { Client, Account } from 'node-appwrite'; import { APPWRITE_KEY } from '$env/static/private'; import { PUBLIC_APPWRITE_ENDPOINT, PUBLIC_APPWRITE_PROJECT } from '$env/static/public';

export const SESSION_COOKIE = 'my-custom-session';

export function createAdminClient() { const client = new Client() .setEndpoint(PUBLIC_APPWRITE_ENDPOINT) .setProject(PUBLIC_APPWRITE_PROJECT) .setKey(APPWRITE_KEY); // Set the Appwrite API key!

TypeScript
// Return the services we want to use.
return {
    get account() {
        return new Account(client);
    }
};

}

export function createSessionClient(event) { const client = new Client() .setEndpoint(PUBLIC_APPWRITE_ENDPOINT) .setProject(PUBLIC_APPWRITE_PROJECT);

TypeScript
// Extract our custom domain's session cookie from the request
const session = event.cookies.get(SESSION_COOKIE);
if (!session) {
    throw new Error("No user session");
}

client.setSession(session);

// Return the services we want to use.
return {
    get account() {
        return new Account(client);
    }
};

}

// src/routes/+page.server.js import { redirect } from '@sveltejs/kit'; export async function load({ locals }) { // Access our user from locals. if (!locals.user) { // If no user is logged in, redirect to the sign up page. redirect(301, '/login'); } // If the user is logged in, redirect to the account page. redirect(301, '/'); }

TL;DR
To get the current user's session from the server side, developers can create an admin client using node-appwrite, set the Appwrite API key, and create a session client. In the load function of the sveltekit file, check if the user is logged in; if not, redirect to the sign-up page, otherwise redirect to the account page.
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more