Skip to content

Nicktho/redux-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redux-auth

A simple redux middleware for JWT based authorization

Works with any async store middleware.

Note:

This project is a work in progress and currently in a very early stage. Updates will be coming soon along with a general roadmap of future features.

How to use:

Add the auth middleware where you create your store

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import auth from 'redux-auth';
import reducer from '../reducers';

const applyStore = applyMiddleware(thunk, auth)(createStore);

export default function configureStore(state) {
  const store = applyStore(reducer, state);

  return store;
}

Add tokenReducer to your reducers

import { combineReducers } from 'redux';
import { tokenReducer } from 'redux-auth';

export default combineReducers({
  token: tokenReducer
  // Other reducers
});

Use the updateToken() action creator to update your client's token where you like

import { updateToken } from 'redux-auth';

export function login(email, password) {
  return dispatch => {
    loginApi.post({ email, password })
    .then(res => {
      dispatch(updateToken(res.token));
    });
  };
}

Use the withToken() helper in any action creator to get access to your token

import { withToken } from 'redux-auth';

export function addTodo(todo) {
  return withToken(token => dispatch => {
    todoApi.post({ todo }, token)
    .then(res => {
      dispatch(todoSuccess(res.todo))
    });
  });
}

About

A simple redux middleware for JWT based authorization

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published