npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dlt-react-session

v3.6.0

Published

[![NPM version](https://img.shields.io/npm/v/redux-react-session.svg?style=flat)](https://npmjs.org/package/redux-react-session) [![Build status: Linux](https://travis-ci.org/bernabe9/redux-react-session.svg?branch=master)](https://travis-ci.org/bernabe9/

Downloads

1

Readme

Redux React Session

NPM version Build status: Linux Dependency Status Coverage Status

Keep your session sync with your local storage and Redux :key:

Redux React Session provides an API that allows to manage sessions through the app, with authorization function for react-router and a persisted session.

Installation

yarn:

yarn add redux-react-session

npm:

npm install redux-react-session --save

Usage

  • Add the session reducer:
import { combineReducers } from 'redux';
import { sessionReducer } from 'redux-react-session';

const reducers = {
  // ... your other reducers here ...
  session: sessionReducer
};
const reducer = combineReducers(reducers);
  • Initiate the session service:
import { createStore } from 'redux';
import { sessionService } from 'redux-react-session';

const store = createStore(reducer)

sessionService.initSessionService(store);

Examples

The examples simulates a simple login/logout that sends requests to a server.

Run the example for react router v3

  1. get into the folder:cd examples/example
  2. install dependencies: npm install
  3. run the example: npm start

Run the example for react router v4

  1. get into the folder:cd examples/react-router-v4-example
  2. install dependencies: npm install
  3. run the example: npm start

API

initSessionService(store, options) : Promise

Initialize an instance of the session service.

The promise will be resolved if the session is valid, and will be rejected if there is no data in the storage.

Once the promise is resolved or rejected the flag checked in the redux store will change from false to true. This allows to check into any component if the session was already checked and it's valid.

Options:

  • refreshOnCheckAuth(default: false): Refresh Redux store in the checkAuth function
  • redirectPath(default: "login"): Path used when a session is rejected or doesn't exist
  • driver: Force to use a particular driver, could be: 'INDEXEDDB', 'WEBSQL', 'LOCALSTORAGE' or 'COOKIES'
  • validateSession: Function to validate the saved session. It can either be a function to return an immediate boolean value or a function that returns a promise. In the case it returns an immadiate value and false is returned the session will be destroyed. In the case of a promise, if either false is returned or an exception is thrown, the session will be destroyed. Example:
const validateSession = (session) => {
  // check if your session is still valid
  return true;
}
const options = { refreshOnCheckAuth: true, redirectPath: '/home', driver: 'COOKIES', validateSession };

sessionService.initSessionService(store, options)
  .then(() => console.log('Redux React Session is ready and a session was refreshed from your storage'))
  .catch(() => console.log('Redux React Session is ready and there is no session in your storage'));
const validateSession = (session) => {
  // check if your session is still valid with a server check, through axios for instance
  return api.invokeRemoteSessionValidationThroughAxios(session).then(response => response.isSessionValid);
}
const options = { refreshOnCheckAuth: true, redirectPath: '/home', driver: 'COOKIES', validateSession };

sessionService.initSessionService(store, options)
  .then(() => console.log('Redux React Session is ready and a session was refreshed from your storage'))
  .catch(() => console.log('Redux React Session is ready and there is no session in your storage'));

refreshFromLocalStorage

Force to refresh the Redux Store from the local storage.

The promise will be resolved if the session is valid, and will be rejected if there is no data in the storage.

Note: this function is called once the session service is initialized

checkAuth

Authorization function for react-router to restrict routes, it checks if exist a session and redirects to the redirectPath

Example:

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import { sessionService } from 'redux-react-session';
import App from './components/App';
import HomePage from './containers/HomePage';
import LoginPage from './containers/LoginPage';

export default (
  <Route path="/" component={App}>
    <IndexRoute onEnter={sessionService.checkAuth} component={HomePage} />
    <Route path="login" component={LoginPage} />
  </Route>
);

Note: If you're using react-router v4 this function it's not necessary. Check out the react-router-v4-example

Note: This function could be used in the client side as well as the server side.

saveSession(session:object) : Promise

Saves the session object in the storage/cookies and changes the authenticated flag to true in Redux Store

loadSession : Promise(currentSession:Object)

Returns the current session if exists

Example:

loadSession
.then(currentSession => console.log(currentSession))
.catch(err => console.log(err))

deleteSession : Promise

Deletes the current session from the storage/cookies

saveUser(user:object) : Promise

Saves the user object in the storage/cookies and in the Redux Store

loadUser : Promise

Returns the current user if exists

deleteUser : Promise

Deletes the current user from the storage/cookies

Immutable JS

Usage of redux-react-session with an immutable store is really simple. Instead of the sessionReducer import the sessionImmutableReducer, as the following example:

  • Add the session reducer:
import { combineReducers } from 'redux';
import { sessionImmutableReducer as session } from 'redux-react-session';

const reducers = {
  // ... your other reducers here ...
  session
};
const reducer = combineReducers(reducers);

Server Rendering

redux-react-session also provides methods to keep the session with server rendering using cookies. So the session will work on the server side as well as the client side.

Here is an example using server rendering

initServerSession(store, req)

Initialize an instance of the server session service.

This function is used in the server.js to initialize a session service instance in each request.

// server.js
import { sessionService, sessionReducer } from 'redux-react-session';
import { combineReducers, createStore } from 'redux';

// ...
app.use((req, res) => {
  const reducer = combineReducers({
    session: sessionReducer
  });
  // Create a new Redux store instance
  const store = createStore(reducer);

  sessionService.initServerSession(store, req);
  // ...
}
// ...

initSessionService(store, { driver: 'COOKIES' })

Initialize an instance of the client session service, IMPORTANT to set the option 'COOKIES'(this is the way that the client send the session data to the server).

This function is used in the client.js of the server rendering to initialize a session service instance.

// client.js
import { createStore } from 'redux';
import { sessionService } from 'redux-react-session';

const store = createStore(reducer)

initSessionService(store, { driver: 'COOKIES' });