React Session Management
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
- Open a session by doing
Session.login();
:
; Sessionstart payload: // (optional) any info you want to save on the persisten session expiration: 86400000; // (optional) defaults to 1 day;
- Close the session by doing
Session.destroy();
:
; Session;
- Retrieve the session and payload from anywhere
; const session = Session;const payload = Session; console; // will be true if is not expired or innactiveconsole; // anything you have set on the session payload is stored here
That is it!!
Some other functionalities:
- Listen to session changes
// listen to session changesconst unsubscribe = Session; //unsubscribe to session changes if needed;
- Wait for session expiration callback
// you need to enforce before calling the login method.Session; //you can destroy the session if it expires
- Change reset the session payload whenever you want
; // pass a new username that will override previous one (if any)Session;
- Check session expiration
const session = Session.get();
console.log(session.expired); // boolean
- 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 ='/profile' = /> </div></BrowserRouter>