Skip to content

Instantly share code, notes, and snippets.

@nesbtesh
Created February 24, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesbtesh/ce9efbe3497cd83cb0552a9f9c19626f to your computer and use it in GitHub Desktop.
Save nesbtesh/ce9efbe3497cd83cb0552a9f9c19626f to your computer and use it in GitHub Desktop.
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