Created
April 10, 2017 14:28
-
-
Save twilson63/c7165042f4ed3c5ec4c3dbf5d9385acb to your computer and use it in GitHub Desktop.
auth.js for redux for passwordless apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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