JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 224
  • Score
    100M100P100Q98517F
  • License MIT

Redux middleware for implementing authentication with a redux application

Package Exports

  • redux-simple-auth

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-simple-auth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status

Redux Simple Auth

Redux middleware inspired by the wonderful Ember Simple Auth library

DISCLAIMER: This package is still in active development

Installation

npm:

npm install --save redux-simple-auth

yarn:

yarn add redux-simple-auth

Usage

configureStore.js

import { createStore, applyMiddleware } from 'redux'
import { createAuthMiddleware } from 'redux-simple-auth'
import rootReducer from './reducers'

const authMiddleware = createAuthMiddleware()

const store = createStore(
  rootReducer,
  applyMiddleware(authMiddleware)
)

reducers/index.js

import { combineReducers } from 'redux'
import { reducer as session } from 'redux-simple-auth'

export default combineReducers({
  session
})

Configuring the middleware

Changing the session storage

import {
  createAuthMiddleware,
  createLocalStorageStore
} from 'redux-simple-auth'

const localStorageStore = createLocalStorageStore()

const authMiddleware = createAuthMiddleware({
  storage: localStorageStore
})

Defining authenticators

import { createAuthMiddleware, createAuthenticator } from 'redux-simple-auth'

const credentialsAuthenticator = createAuthenticator({
  name: 'credentials',
  authenticate(credentials) {
    return fetch('/api/login', {
      method: 'POST',
      body: JSON.stringify(credentials)
    }).then(({ token }) => ({ token }))
  },
  restore(data) {
    if (data.token) {
      return Promise.resolve(data)
    }

    return Promise.reject()
  }
})

const authMiddleware = createAuthMiddleware({
  authenticators: [credentialsAuthenticator]
})

Actions

Authenticate with a named authenticator:

import { authenticate } from 'redux-simple-auth'

store.dispatch(
  authenticate('credentials', {
    email: 'user@example.com',
    password: 'password'
  })
)

Invalidate the session:

import { invalidateSession } from 'redux-simple-auth'

store.dispatch(invalidateSession())

License

MIT