Skip to content

Instantly share code, notes, and snippets.

@OleksivO
Created January 16, 2019 20:00
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 OleksivO/ab0c362ce439811c1befcd6347063a73 to your computer and use it in GitHub Desktop.
Save OleksivO/ab0c362ce439811c1befcd6347063a73 to your computer and use it in GitHub Desktop.
the description for this gist
import axios from 'axios';
export const authStart = () => {
return {
type: AUTH_START
}
};
export const authSuccess = (token, userId) => {
return {
type: AUTH_SUCCESS,
token: token,
userId: userId
}
};
export const authFail = (error) => {
return {
type: AUTH_FAIL,
error: error
}
};
export const logout = () => {
localStorage.removeItem('token');
localStorage.removeItem('expirationDate');
localStorage.removeItem('userId');
return {
type: AUTH_LOGOUT
}
};
export const auth = (email, password, signin) => {
return dispatch => {
dispatch(authStart());
const url = action.signin ? 'SIGNIN_URL' : 'SIGNUP_URL';
axios.post(url, {
email: email,
password: password
})
.then(response => {
const expirationDate = new Date(new Date().getTime() + response.data.expiresIn * 1000);
localStorage.setItem('token', response.data.authToken);
localStorage.setItem('expirationDate', expirationDate);
localStorage.setItem('userId', response.data.userId);
dispatch(authSuccess(response.data.authToken, response.data.userId));
dispatch(checkAuthTimeout(response.data.expiresIn));
})
.catch(error => {
dispatch(authFail(error))
})
}
};
export const checkAuthTimeout = (expirationTime) => {
return dispatch => {
setTimeout(() => {
dispatch(logout())
}, expirationTime * 1000)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment