1.0.4 • Published 7 years ago
auth-jwt-reducer v1.0.4
Auth-jwt-reducer
Some mashed together auth modules i use in my react projects. This is meant to work along side redux, react-redux-router
Installation
npm install auth-jwt-reducer
##Conditions
The reducer expects you to have a reducer with react-router-redux, like this { auth: YourAuthReducer, routing: routerReducer }
And these are the AuthWrapper Proptypes
AuthWrapper.propTypes = {
pathSelector: React.PropTypes.func,
authSelector: React.PropTypes.func,
validatingPage: React.PropTypes.element,
setTokenAction: React.PropTypes.func,
tokenName: React.PropTypes.string,
loginUrl: React.PropTypes.string
}
AuthWrapper.defaultProps = {
tokenName: 'jwttoken',
loginUrl: '/login'
}
The AuthWrapper goes as a high level route in the route that renders its children depending on if the user is logged in. When logged in, if trying to access from another tab, the AuthWrapper checks for a previously set token with the specified key so it can automatically login.
Default State
{
//Logged User Data
user: null
//Logged user JWT token
token: null
//Attempting login
attempting: false
error: null
validatingExistingToken: false
}
Actions handled by reducer
AUTH_LOGIN | Expects nothing.
AUTH_LOGIN_SUCCESS | Expects action.payload.user and action.payload.token.
AUTH_LOGIN_ERROR | Expects action.payload to be error.
AUTH_SET_TOKEN_SUCCESS | Expects action.payload.user and action.payload.token.
AUTH_SET_TOKEN_ERROR | Expects action.payload to be error.
AUTH_LOGOUT | Expects nothing.
Usage
import { reducer } from 'auth-jwt-reducer'
Then just use the reducer in Redux.