Skip to content

Session

The ctx.session utility is accessible in any handler or middleware and supports asynchronous operations with a clean interface:

(ctx) => {
await ctx.session.set("userId", 123);
const userId = await ctx.session.get("userId");
};

you need to install the following packages

Terminal window
npm install @gaman/session

Stores a value in the session.

  • key (string) – The name of the session key.
  • value (string | object | Buffer) – The session value. Objects will be automatically serialized.
(ctx) => {
await ctx.session.set("user", { id: 1, name: "Angga" });
await ctx.session.set("token", "abc123");
await ctx.session.set("preferences", { theme: "dark", lang: "en" });
};

Retrieves a value from the session.

Returns the stored value or null if the key doesn’t exist.

(ctx) => {
const user = await ctx.session.get("user");
const token = await ctx.session.get("token");
if (user) {
// user data exists
console.log(user.name);
}
};

Checks if a session key exists.

Returns true if the key exists, false otherwise.

(ctx) => {
if (await ctx.session.has("user")) {
// user session exists
}
};

Deletes a session key and its value.

(ctx) => {
await ctx.session.delete("token");
await ctx.session.delete("user");
};

To use sessions in your GamanJS application, you need to import and configure the session integration:

index.ts
import { session } from "@gaman/session";
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(session());
});

GamanJS supports multiple session storage drivers. Each driver has its own configuration options and use cases.

Stores session data in encrypted cookies. Perfect for stateless applications.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: { type: "cookies" },
secret: "your-secret-key",
maxAge: 86400, // 1 day
secure: true,
})
);
});

Use cases: Small session data, stateless deployments, simple applications.


Stores session data in server memory. Fast but not persistent across restarts.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: { type: "memory" },
secret: "your-secret-key",
maxAge: 3600, // 1 hour
})
);
});

Use cases: Development, testing, single-server deployments with temporary sessions.


Stores session data in local files. Persistent across restarts but not suitable for multi-server setups.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: {
type: "file",
dir: "./sessions", // Optional: custom directory
},
secret: "your-secret-key",
maxAge: 86400,
})
);
});

Use cases: Single-server deployments, development with persistent sessions.


Stores session data in Redis. Excellent for production and multi-server deployments.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: {
type: "redis",
url: "redis://localhost:6379", // Optional: custom Redis URL
},
secret: "your-secret-key",
maxAge: 86400,
})
);
});

Use cases: Production applications, multi-server setups, high-performance requirements.


Stores session data in SQLite database. Persistent and suitable for single-server deployments.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: {
type: "sql",
file: "sessions.db", // Optional: custom database file
},
secret: "your-secret-key",
maxAge: 86400,
})
);
});

Use cases: Single-server deployments with persistent sessions, applications requiring SQL queries.


Stores session data in MongoDB. Great for applications already using MongoDB.

index.ts
defineBootstrap(mainBlock, (app) => {
app.registerIntegration(
session({
driver: {
type: "mongodb",
dbName: "myapp",
uri: "mongodb://localhost:27017", // Optional: custom MongoDB URI
collection: "sessions", // Optional: custom collection name
},
secret: "your-secret-key",
maxAge: 86400,
})
);
});

Use cases: Applications using MongoDB, document-based session storage, multi-server deployments.


OptionTypeDescriptionDefault
secretstringSecret key for session encryptionprocess.env.GAMAN_KEY
driverobjectStorage driver configuration{ type: 'cookies' }
maxAgenumberSession lifetime in seconds86400 (1 day)
securebooleanRequire HTTPS for session cookiestrue

example.routes.ts
export default defineRoutes(() => ({
"/login": async (ctx) => {
// Simulate login
const user = { id: 1, name: "Angga", email: "angga@example.com" };
await ctx.session.set("user", user);
await ctx.session.set("isAuthenticated", true);
return Response.json({ status: "logged in", user });
},
"/profile": async (ctx) => {
if (await ctx.session.has("isAuthenticated")) {
const user = await ctx.session.get("user");
return Response.json({ user });
}
return Response.json({ error: "Not authenticated" }, { status: 401 });
},
"/logout": async (ctx) => {
await ctx.session.delete("user");
await ctx.session.delete("isAuthenticated");
return Response.json({ status: "logged out" });
},
"/check": async (ctx) => {
const hasUser = await ctx.session.has("user");
const isAuth = await ctx.session.get("isAuthenticated");
return Response.json({
hasUser,
isAuthenticated: !!isAuth,
});
},
}));

  • Always use a strong secret key in production environments.
  • Choose the appropriate driver based on your deployment architecture.
  • Use secure: true in production (HTTPS required).
  • Set reasonable maxAge values to balance security and user experience.
  • Handle session operations with try-catch blocks for error handling.
  • Clean up unused session keys to prevent memory/storage bloat.

  • Cookies: Small data, stateless apps, simple deployments
  • Memory: Development, testing, single-server with temporary sessions
  • File: Single-server with persistent sessions, development
  • Redis: Production, multi-server, high performance
  • SQL: Single-server with SQL needs, persistent sessions
  • MongoDB: Document-based storage, existing MongoDB infrastructure

For advanced usage, sessions integrate seamlessly with authentication middleware and can be extended with custom serialization logic.