
Table of Contents
About The Project
"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.
(back to top)
Key Features
Simplicity at its Core
: With intuitive functions, developers can effortlessly store and retrieve data without wading through complexities.
Enhanced Security
: Understanding the need for robust data protection, the package offers signature & encryption for session data.
Intelligent Session Management
: The package intelligently handles session expiration, reducing the manual oversight typically needed.
Bespoke Customization
: Recognizing that one size doesn't fit all, "SvelteKit-Redis-Session" offers high customizability, allowing developers to tailor its functionalities to their distinct needs, and in turn, enhancing application performance.
For all runtimes
: As of now we have so many runtimes cloudflare workers, vercel, netlify.
Support for multiple redis libraries
: We support redis
& ioredis
out of the box.
Support for @upstash/redis
: It's mandatory if you are working with workers, edge and serverless. Cloudflare workers doesn't support TCP.
Built With
(back to top)
Getting Started
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.
Prerequisites
This is an example of how to list things you need to use the software and how to install them.
Setup
-
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)
Usage
Examples are going to be added soon.
For more examples, please refer to the Examples
(back to top)
Roadmap
See the open issues for a full list of proposed
features (and known issues).
(back to top)
License
Distributed under the MIT License. See LICENSE.txt
for more information.
(back to top)
Contact
Your Name - @theether0 - meenashivam9650@gmail.com
Project
Link: https://github.com/etherCorps/SK-Redis-SessionManager
(back to top)
Acknowledgments
(back to top)