JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 195
  • Score
    100M100P100Q96941F
  • 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

Documentation is still very spotty and the API is very likely to change between now and completion. I plan to fully document usage as the API is more solidified.

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 } from 'redux-simple-auth'
import { createLocalStorageStore } from 'redux-simple-auth/storage'

const localStorageStore = createLocalStorageStore()

const authMiddleware = createAuthMiddleware({
  storage: localStorageStore
})

Using a cookie store

import { createAuthMiddleware } from 'redux-simple-auth'
import { createCookieStore } from 'redux-simple-auth/storage'

const cookieStore = createCookieStore()

const authMiddleware = createAuthMiddleware({
  storage: cookieStore
})

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]
})

Define an authorizer

const authMiddleware = createAuthMiddleware({
  authorize(data, block) {
    if (data.token) {
      block('Authorization', `Bearer ${token}`)
    }
  }
})

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())

Fetch an endpoint that requires authentication. Uses authorize function passed to middleware.

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

store.dispatch(
  fetch('https://www.example.com/me')
)

License

MIT