Created
December 30, 2015 14:31
-
-
Save karlbright/60f56790541bfd980013 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 API from 'api' | |
import { replacePath} from 'redux-simple-router' | |
export const AUTHENTICATE = 'AUTHENTICATE' | |
export const AUTHENTICATE_SUCCESS = 'AUTHENTICATE_SUCCESS' | |
export const AUTHENTICATE_FAILURE = 'AUTHENTICATE_FAILURE' | |
export const VERIFYING_TOKEN = 'VERIFYING_TOKEN' | |
export const VERIFY_TOKEN_FAILURE = 'VERIFY_TOKEN_FAILURE' | |
export const VERIFY_TOKEN_SUCCESS = 'VERIFY_TOKEN_SUCCESS' | |
export function authenticateAndRedirectTo (username, password, returnPath) { | |
return dispatch => { | |
dispatch({ type: AUTHENTICATE, payload: { username }}) | |
API.Authentication.authenticate(username, password, function(err, result) { | |
if (err) return dispatch({ type: AUTHENTICATE_FAILURE, payload: { username, error: err }}) | |
dispatch({ type: AUTHENTICATE_SUCCESS, payload: { username, result }}) | |
dispatch(replacePath(returnPath)) | |
}) | |
} | |
} | |
export function verifyTokenAndRedirectTo (returnPath) { | |
const redirectToLogin = replacePath('/login', { returnPath }) | |
const token = window.localStorage.getItem('token') | |
if (!token) return redirectToLogin | |
return dispatch => { | |
dispatch({ type: VERIFYING_TOKEN, payload: null }) | |
API.Authentication.verifyToken(token, (err, result) => { | |
if (err) { | |
dispatch({ type: VERIFY_TOKEN_FAILURE, payload: null, meta: { token }}) | |
return dispatch(redirectToLogin) | |
} | |
dispatch({ type: VERIFY_TOKEN_SUCCESS, payload: result, meta: { token }}) | |
dispatch(replacePath(returnPath)) | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment