Created
February 24, 2017 20:04
-
-
Save nesbtesh/ce9efbe3497cd83cb0552a9f9c19626f to your computer and use it in GitHub Desktop.
from https://github.com/joshgeller/react-redux-jwt-auth-example/blob/master/src/actions/index.js
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 { checkHttpStatus, parseJSON } from '../utils'; | |
import {LOGIN_USER_REQUEST, LOGIN_USER_FAILURE, LOGIN_USER_SUCCESS, LOGOUT_USER, FETCH_PROTECTED_DATA_REQUEST, RECEIVE_PROTECTED_DATA} from '../constants'; | |
import { pushState } from 'redux-router'; | |
import jwtDecode from 'jwt-decode'; | |
export function loginUserSuccess(token) { | |
localStorage.setItem('token', token); | |
return { | |
type: LOGIN_USER_SUCCESS, | |
payload: { | |
token: token | |
} | |
} | |
} | |
export function loginUserFailure(error) { | |
localStorage.removeItem('token'); | |
return { | |
type: LOGIN_USER_FAILURE, | |
payload: { | |
status: error.response.status, | |
statusText: error.response.statusText | |
} | |
} | |
} | |
export function loginUserRequest() { | |
return { | |
type: LOGIN_USER_REQUEST | |
} | |
} | |
export function logout() { | |
localStorage.removeItem('token'); | |
return { | |
type: LOGOUT_USER | |
} | |
} | |
export function logoutAndRedirect() { | |
return (dispatch, state) => { | |
dispatch(logout()); | |
dispatch(pushState(null, '/login')); | |
} | |
} | |
export function loginUser(email, password, redirect="/") { | |
return function(dispatch) { | |
dispatch(loginUserRequest()); | |
return fetch('http://localhost:3000/auth/getToken/', { | |
method: 'post', | |
credentials: 'include', | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({email: email, password: password}) | |
}) | |
.then(checkHttpStatus) | |
.then(parseJSON) | |
.then(response => { | |
try { | |
let decoded = jwtDecode(response.token); | |
dispatch(loginUserSuccess(response.token)); | |
dispatch(pushState(null, redirect)); | |
} catch (e) { | |
dispatch(loginUserFailure({ | |
response: { | |
status: 403, | |
statusText: 'Invalid token' | |
} | |
})); | |
} | |
}) | |
.catch(error => { | |
dispatch(loginUserFailure(error)); | |
}) | |
} | |
} | |
export function receiveProtectedData(data) { | |
return { | |
type: RECEIVE_PROTECTED_DATA, | |
payload: { | |
data: data | |
} | |
} | |
} | |
export function fetchProtectedDataRequest() { | |
return { | |
type: FETCH_PROTECTED_DATA_REQUEST | |
} | |
} | |
export function fetchProtectedData(token) { | |
return (dispatch, state) => { | |
dispatch(fetchProtectedDataRequest()); | |
return fetch('http://localhost:3000/getData/', { | |
credentials: 'include', | |
headers: { | |
'Authorization': `Bearer ${token}` | |
} | |
}) | |
.then(checkHttpStatus) | |
.then(parseJSON) | |
.then(response => { | |
dispatch(receiveProtectedData(response.data)); | |
}) | |
.catch(error => { | |
if(error.response.status === 401) { | |
dispatch(loginUserFailure(error)); | |
dispatch(pushState(null, '/login')); | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment