All files / src auth0.js

100% Statements 40/40
90% Branches 18/20
100% Functions 6/6
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 584x                                                         12x   4x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 1x   1x 1x 1x         1x     1x      
import Auth0Lock from 'auth0-lock'
import Auth0Client from 'auth0-js'
import {push} from 'react-router-redux'
import {createAction} from 'redux-actions'
 
const AUTH0_CLIENT_ID = process.env.AUTH0_CLIENT_ID
const AUTH0_DOMAIN = process.env.AUTH0_DOMAIN
 
export const setAuth0User = createAction('set auth0 user')
export const authIsRequired = AUTH0_CLIENT_ID && AUTH0_DOMAIN
export const defaultLockOptions = {
  auth: {
    params: {
      scope: 'openid analyst offline_access'
    },
    redirect: false
  },
  closeable: false,
  autoclose: true
}
export const getLock = (lockOptions) => new Auth0Lock(AUTH0_CLIENT_ID, AUTH0_DOMAIN, lockOptions)
export const getClient = () => new Auth0Client({
  clientID: AUTH0_CLIENT_ID,
  domain: AUTH0_DOMAIN
})
 
/**
 * Use on application mount when authentication is required
 */
export function refreshUser (dispatch, overrideAuthIsRequired) {
  const localStorage = window.localStorage
  if (authIsRequired || overrideAuthIsRequired) {
    const client = getClient()
    const userString = localStorage.getItem('user')
    const user = userString && JSON.parse(userString)
    if (user && user.refreshToken) {
      dispatch(setAuth0User(user))
      ifE (process.env.NODE_ENV !== 'development') {
        client.refreshToken(user.refreshToken, function (err, delegationResult) {
          if (err) {
            dispatch(setAuth0User(null))
            localStorage.setItem('user', null)
            dispatch(push('/login'))
          } else {
            user.idToken = delegationResult.id_token
            dispatch(setAuth0User(user))
            localStorage.setItem('user', JSON.stringify(user))
          }
        })
      }
    } else {
      dispatch(push('/login'))
    }
  } else {
    throw new Error('Auth0 credentials not found!')
  }
}