"SvelteKit-Redis-Session" stands out as an indispensable tool for developers aiming to seamlessly integrate Redis as a session manager within their SvelteKit applications. Designed with a keen attention to detail, this package ensures that managing user sessions is not only efficient but also intuitive.
This is an example of how you may give instructions on setting up your project locally.
To get a local copy up and running follow these simple example steps.
This is an example of how to list things you need to use the software and how to install them.
-
First, we need to make instance of it to use everywhere in the project.
import { IoRedisSessionStore } from '@ethercorps/SvelteKit-redis-session';
import Redis from 'ioredis';
export const sessionManager = new IoRedisSessionStore({
redisClient: new Redis(),
secret: 'your-secret-key',
cookieName: 'session',
prefix: 'sk-session',
signed: true,
encrypted: false,
useTTL: true,
renewSessionBeforeExpire: false,
renewBeforeSeconds: 30 * 60,
serializer: JSON,
cookiesOptions: {
path: '/',
httpOnly: true,
sameSite: 'strict',
secure: !dev,
maxAge: 60 * 60 * 24
}
});
These are the default config example you can use as per your need and make it better for your use.
I have written an article to explain more about this package link for article.
-
To create a new session and add cookies for user after authentication
import sessionManager from 'sessionManagerFile';
export const actions: Actions = {
login: async ({ req, cookies, locals }) => {
const formdata = await request.formData();
const { data, error, message } = sessionManager.createSession(cookies, userData, userId);
throw redirect(307, '/dashboard');
}
};
-
To get session data for the cookie
import sessionManager from 'sessionManagerFile';
export const handle: Handle = async ({ event, resolve }) => {
const { data, error, message } = sessionManager.getSession(event.cookies);
};
-
To update session expiry in redis and cookies
import sessionManager from 'sessionManagerFile';
const { data, error, message } = await sessionManager.updateSessionExpiry(cookies);
-
To update session data in redis and cookies
import sessionManager from 'sessionManagerFile';
const { data, error, message } = await sessionManager.updateSession(cookies, newSessionData);
-
To delete session from redis and cookie from browser
import sessionManager from 'sessionManagerFile';
export const actions: Actions = {
logout: async ({ cookies, locals }) => {
const { data, error, message } = await sessionManager.deleteSession(cookies);
throw redirect(307, '/login');
}
};
-
To get all sessions of a user from redis and cookie from browser
import sessionManager from 'sessionManagerFile';
export const GET: RequestHandler = async ({ cookies, locals }) => {
const { data, error, message } = await sessionManager.getSessionsByUserId(userId);
return json({data})
}
};
(back to top)
Examples are going to be added soon.
Distributed under the MIT License. See LICENSE.txt
for more information.