fastify-custom-session
A Fastify (v3.x+) plugin that let you use session and decide only where to load/save from/to through the adapter pattern.
Built-in adapters
- FirebaseSessionAdapter (firebase-admin) [fully working]
- MockSessionAdapter (in-memory, for writing tests) [fully working]
- PrismaSessionAdapter (@prisma/client compat layer) [fully working]
- PostgresSessionAdapter (pg, pg-pool) [wip]
Installation
$ yarn add @ethicdevs/fastify-custom-session
# or
$ npm i @ethicdevs/fastify-custom-session
Usage
import fastifyCookies from "@fastify/cookie";
import fastifyCustomSession, {
FirebaseSessionAdapter, // Firebase Firestore adapter
MockSessionAdapter, // In Memory adapter (for testing)
PostgresSessionAdapter, // PostgreSQL adapter (pg/pg-pool)
PrismaSessionAdapter, // Prisma Client adapter
} from "@ethicdevs/fastify-custom-session";
let server = null;
function main() {
server = fastify(); // provide your own
// some cookies options
const cookieSecret = "super-secret-session-secret"; // or better: Env.SESSION_SECRET
const cookiesOptions = {
domain: `.my-app.com`, // or better: `.${Env.DEPLOYMENT_DOMAIN}`,
httpOnly: true,
expires: new Date(Date.now() + 10 * (8 * 3600) * 1000), // 10 days in secs
path: "/",
secure: false,
sameSite: "lax",
signed: true,
};
// register the cookies plugin FIRST.
server.register(fastifyCookies, {
parseOptions: cookiesOptions,
secret: cookieSecret,
});
// then register the customSession plugin.
server.register(fastifyCustomSession, {
password: cookieSecret,
cookieName: "my_app_session_id", // or better: Env.COOKIE_NAME,
cookieOptions,
storeAdapter: new MockSessionAdapter({
/* ... AdapterOptions ... */
}) as any,
ttl: 30 * 60, // expire session after 30 minutes (in seconds)
initialSession: { // initial data in session (so you can avoid null's)
whateverYouWant: '<unset>',
aNullableProp: null,
mySuperObject: {
foo: '<unset>',
bar: 0,
baz: '<unset>',
};
},
});
}
main();
then if you are a TypeScript user you will need to define the shape of the
session.data
object, you can do so easily by adding the following lines to your
types/global/index.d.ts
file:
// use declaration merging to provide custom session interface
declare module "@ethicdevs/fastify-custom-session" {
declare interface CustomSession {
// request.session.data shape
whateverYouWant: string;
aNullableProp: null | string;
mySuperObject: {
foo: string;
bar: number;
baz: string;
};
}
}
later on during request before you send the headers/response, typically in your controller/handler:
const myRequestHandler = async (request, reply) => {
request.session.data.whateverYouWant = "foo";
request.session.data.aNullableProp = "not null anymore";
request.session.data.mySuperObject = {
foo: "bar",
bar: 42,
baz: "quxx",
};
return reply.send("Hello with session!");
};
const mySecondRequestHandler = async (request, reply) => {
// if you're a typescript user, enjoy ;)
/* request.session.data.
| [p] whateverYouWant
| [p] mySuperObject */
request.session.data.whateverYouWant; // "foo"
request.session.data.aNullableProp; // "not null anymore"
request.session.data.object.foo; // "bar"
request.session.data.object.bar; // 42
return reply.send(
"Cool value from session:" + request.session.data.object.baz,
);
// "Cool value from session: quxx"
};
then enjoy the session being available both in your controllers/handlers and in your data store table/collection (linked to the Adapter you chosen in first step).
Debugging
This library make use of the debug
package,
thus it's possible to make the output of the program more verbose.
The plugin itself, and the adapters have their own "scopes" of logs so its possible to troubleshoot only with logs from the plugin, or with logs from the adapter(s) or both.
# Syntax and examples:
# DEBUG=$scope:$subscope $(...command)
# Show all logs kinds from the customSession plugin
$ DEBUG=customSession:* yarn dev
# Show only trace logs from the customSession plugin
$ DEBUG=customSession:trace yarn dev
# Show only error logs from the customSession plugin
$ DEBUG=customSession:error yarn dev
Note: in this example, $scope
could be:
-
customSession
; -
firebaseSessionAdapter
; -
mockSessionAdapter
; -
prismaSessionAdapter
; -
postgresSessionAdapter
; -
yourOwnSessionAdapter
.
License
The MIT license.