GithubHelp home page GithubHelp logo

breatheco-de / react-session Goto Github PK

View Code? Open in Web Editor NEW
24.0 5.0 6.0 8.81 MB

Create and maintain a sessions in react, compatible with React Router (login/logout) and sync with the localstorage

Home Page: https://breatheco-de.github.io/react-session/

JavaScript 100.00%
react react-session session-management front-end-sessions react-login react-logout react-library react-component react-router-session reactjs

react-session's Introduction

React Session Management

Maintenance Tests npm npm GitHub license

Create and maintain persisten login sessions on the browser (even if the website is refreshed). Checkout the live demo.

Note: Extremely easy integration with React Router.

Installation

$ npm i --save bc-react-session

Usage

  1. Open a session by doing Session.login();:
import {Session} from 'bc-react-session';

Session.start({ 
	payload: {
	    // (optional) any info you want to save on the persisten session
	},
	expiration: 86400000; // (optional) defaults to 1 day
});
  1. Close the session by doing Session.destroy();:
import {Session} from 'bc-react-session';

Session.destroy();
  1. Retrieve the session and payload from anywhere
import {Session} from 'bc-react-session';

const session = Session.get();
const { payload } = Session.get();

console.log(session.isValid); // will be true if is not expired or innactive
console.log(payload); // anything you have set on the session payload is stored here

That is it!!

Some other functionalities:

  1. Listen to session changes
// listen to session changes
const unsubscribe = Session.onChange((session) => {
  console.log(session);
  
  if(session.expired) console.log('The session has expired')
  if(session.autenticated) console.log('No one has logged in')
  
});
 
 //unsubscribe to session changes if needed
unsubscribe();
  1. Wait for session expiration callback
// you need to enforce before calling the login method.
Session.onExpiration((session) => session.destroy()); //you can destroy the session if it expires
  1. Change reset the session payload whenever you want
import {Session} from 'bc-react-session';

// pass a new username that will override previous one (if any)
Session.setPayload({
    username: 'alesanchezr'
});
  1. Check session expiration
const session = Session.get();
console.log(session.expired); // boolean
  1. Make a Private Route using react router

The library brings a component called <PrivateRoute /> to make your routes private without any extra code.

import {PrivateRoute} from 'bc-react-session';

<BrowserRouter>
    <div>
        <PrivateRoute exact path='/profile' component={PrivateLayout} />
    </div>
</BrowserRouter>

react-session's People

Contributors

alesanchezr avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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