All Projects → bernabe9 → Redux React Session

bernabe9 / Redux React Session

🔑 Simple Session API storage for Redux and React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redux React Session

WaWebSessionHandler
(DISCONTINUED) Save WhatsApp Web Sessions as files and open them everywhere!
Stars: ✭ 27 (-80.71%)
Mutual labels:  localstorage, session, indexeddb
V2 Universal Js Hmr Ssr React Redux
⚡ (V2) Universal JS - Server Side Rendering, Code Splitting and Hot Module Reloading ⚡
Stars: ✭ 147 (+5%)
Mutual labels:  react-router, server-side-rendering, react-router-v4
Project Webcube
Continuously updated JS infrastructure for modern web dev
Stars: ✭ 141 (+0.71%)
Mutual labels:  react-router, server-side-rendering, universal-app
Universal React Redux
🧐 A sensible universal starter kit for React + Redux
Stars: ✭ 112 (-20%)
Mutual labels:  react-router, server-side-rendering, react-router-v4
Simple Universal React Redux
The simplest possible Async Universal React & Redux Boilerplate app, that works on both Mac and Windows
Stars: ✭ 58 (-58.57%)
Mutual labels:  react-router, react-router-v4
React Cool Starter
😎 🐣 A starter boilerplate for a universal web app with the best development experience and a focus on performance and best practices.
Stars: ✭ 1,083 (+673.57%)
Mutual labels:  react-router, server-side-rendering
Starter Lapis
Cutting edge starter kit
Stars: ✭ 72 (-48.57%)
Mutual labels:  react-router, server-side-rendering
React Redux Saucepan
A minimal and universal react redux starter project. With hot reloading, linting and server-side rendering
Stars: ✭ 86 (-38.57%)
Mutual labels:  react-router, server-side-rendering
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-68.57%)
Mutual labels:  server-side-rendering, universal-app
Godb.js
IndexedDB with Intuitive API,轻松搞定浏览器数据库🎉
Stars: ✭ 78 (-44.29%)
Mutual labels:  indexeddb, localstorage
React Dashboard
🔥React Dashboard - isomorphic admin dashboard template (React.js, Bootstrap, Node.js, GraphQL, React Router, Babel, Webpack, Browsersync) 🔥
Stars: ✭ 1,268 (+805.71%)
Mutual labels:  react-router, react-router-v4
Universal React Demo
ES6 demo of a simple but scalable React app with react-router, code splitting, server side rendering, and tree shaking.
Stars: ✭ 50 (-64.29%)
Mutual labels:  server-side-rendering, react-router-v4
Hapi React Hot Loader Example
Simple React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-68.57%)
Mutual labels:  server-side-rendering, universal-app
Ssr React
How to server-side render React, hydrate it on the client and combine client and server routes
Stars: ✭ 67 (-52.14%)
Mutual labels:  react-router, server-side-rendering
React Redux Antdesign Webpack Starter
react + redux + ant design + react-router 4 + webpack 4 starter
Stars: ✭ 44 (-68.57%)
Mutual labels:  react-router, react-router-v4
The Ultimate Boilerplate
webpack 2, react hotloader 3, react router v4, code splitting and more
Stars: ✭ 85 (-39.29%)
Mutual labels:  server-side-rendering, react-router-v4
React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-26.43%)
Mutual labels:  react-router, react-router-v4
Vlf
A Vue plugin from localForage.vue-localForage or vlf
Stars: ✭ 99 (-29.29%)
Mutual labels:  indexeddb, localstorage
Road Beyond React App
🌈 The Road beyond React - Thing you can use after learning plain React.js
Stars: ✭ 108 (-22.86%)
Mutual labels:  react-router, react-router-v4
Broadcast Channel
📡 BroadcastChannel to send data between different browser-tabs or nodejs-processes 📡
Stars: ✭ 843 (+502.14%)
Mutual labels:  indexeddb, localstorage

Redux React Session

NPM version Build status: Linux Dependency Status Coverage Status

Keep your session sync with your local storage and Redux 🔑

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 sessionReducer from redux-react-session/immutable, as the following example:

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

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' });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].