Skip to content

Instantly share code, notes, and snippets.

@twilson63
Created April 10, 2017 14:28
Show Gist options
  • Save twilson63/c7165042f4ed3c5ec4c3dbf5d9385acb to your computer and use it in GitHub Desktop.
Save twilson63/c7165042f4ed3c5ec4c3dbf5d9385acb to your computer and use it in GitHub Desktop.
auth.js for redux for passwordless apps
import React from 'react'
import AuthService from './utils/auth-service'
const basicReducer = type => (state = null, action) => {
switch (action.type) {
case type:
return action.payload
default:
return state
}
}
// reducer
export const authReducer = basicReducer('SET_AUTH')
export const profileReducer = basicReducer('SET_PROFILE')
export const tokenReducer = basicReducer('SET_TOKEN')
const auth = new AuthService(
process.env.REACT_APP_AUTH0_CLIENTID,
process.env.REACT_APP_AUTH0_DOMAIN
)
// listener
export const authListener = dispatch => {
if (auth.loggedIn()) {
dispatch({ type: 'SET_PROFILE', payload: auth.getProfile() })
dispatch({ type: 'SET_TOKEN', payload: auth.getToken() })
}
dispatch({ type: 'SET_AUTH', payload: auth })
auth.on('authenicated', (profile, token) => {
dispatch({ type: 'SET_PROFILE', payload: profile })
dispatch({ type: 'SET_TOKEN', payload: token })
})
return auth
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment