Code Monkey home page Code Monkey logo

sk-redis-sessionmanager's Introduction

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

SvelteKit Redis Session Manager

Redis integration in SvelteKit for Session Management
@ethercorps/sveltekit-redis-session
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. License
  6. Contact
  7. Acknowledgments

About The Project

"sveltekit-redis-session" makes it easy for developers to use Redis as a session manager in SvelteKit applications. It offers a variety of convenient features that make managing user sessions a breeze. This includes simple functions for storing and retrieving data, encryption of session data for added security, and automatic handling of session expiration. Additionally, the package is highly customizable, which allows developers to adjust it to their specific requirements and optimize performance (COPY MY CODE BUT LEARN). Overall, "sveltekit-redis-session" is choice for managing user sessions in any SvelteKit application thanks to SvelteKit package for robust functionality and flexibility.

(back to top)

Built With

  • SvelteKit
  • Redis
  • Svelte

(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.

  • Install the sveltekit-redis-session
    pnpm i @ethercorps/SvelteKit-redis-session

Setup

Guide to how to use @ethercorps/SvelteKit-redis-session

  1. First we need to make instance of it to use everywhere in project.

    import { RedisSessionStore } from '@ethercorps/SvelteKit-redis-session';
    import Redis from 'ioredis';
    export const sessionManager = new RedisSessionStore({
    	redisClient: new Redis(), // Required A pre-initiated redis client
    	secret: 'your-secret-key', // Required A secret key for encryption and other things,
    	cookieName: 'session', // CookieName to be saved in cookies for browser Default session
    	prefix: 'sk-session', // Prefix for Redis Keys Default sk-session
    	signed: true, // Do you want to sign your cookies Default true
    	encrypted: false, // Do you want to encrypt your cookies using 'aes-256-cbc' algorithm Default false
    	useTTL: true, // Do you wanna use redis's Expire key functionality Default false
    	renewSessionBeforeExpire: false, // Do you wanna update session expire time in built function Default false
    	renewBeforeSeconds: 30 * 60, // If renewSessionBeforeExpire is true define your renew before time in seconds Default 30 minutes
    	serializer: JSON, // You can define your own serializer functions to stringify and parse sessionData for redis Default JSON
    	cookiesOptions: {
    		path: '/',
    		httpOnly: true,
    		sameSite: 'strict',
    		secure: !dev, // From SvelteKit "$app/environment"
    		maxAge: 60 * 60 * 24 // You have to define time in seconds and it's also used for redis key expiry time
    	} // You have more options these are default used in package for more check sveltekit CookieSerializeOptions type.
    });

    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.

  2. To create new session and add cookies for user after authentication

    // Example it's a +page.server.ts
    import sessionManager from 'sessionManagerFile';
    
    export const actions: Actions = {
    	login: async ({ req, cookies, locals }) => {
    		const formdata = await request.formData();
    		// Form validation && user validation
    		const { data, error, message } = sessionManager.createNewSession(cookies, userData);
    		// data is the value we added to cookies, check for error which is a boolean and message.
    		/* add data to locals too for accessing data from client */
    		throw redirect(307, '/dashboard');
    	}
    };
  3. To get session data for the cookie

    /* hooks.server.ts */
    import sessionManager from 'sessionManagerFile';
    
    export const handle: Handle = async ({ event, resolve }) => {
    	/* your validation logic */
    	const { data, error, message } = sessionManager.getSession(event.cookies);
    	// data is the User session data we saved to redis while login, check for error which is a boolean and message.
    	/* do error check and then set data to locals as per your logic */
    };
  4. To update session expiry in redis and cookies

    // in any server side file or endpoint where you can access browser cookies
    import sessionManager from 'sessionManagerFile';
    const { data, error, message } = await sessionManager.updateSessionExpiry(cookies);
    // data is going to be null or key which is updated, error is a boolean value and message a string
  5. To delete session from redis and cookie from browser

    // Example it's a +page.server.ts
    
    import sessionManager from 'sessionManagerFile';
    export const actions: Actions = {
    	logout: async ({ cookies, locals }) => {
    		const { data, error, message } = await sessionManager.delSession(cookies);
    		// data is the value we deleted key, check for error which is a boolean and message.
    		/* clear your locals too */
    		throw redirect(307, '/login');
    	}
    };

(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 - [email protected]

Project Link: https://github.com/etherCorps/SK-Redis-SessionManager

(back to top)

Acknowledgments

(back to top)

sk-redis-sessionmanager's People

Contributors

theethergit avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.