Advertisement
ahmadandika

auth.action.js

Aug 18th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from 'axios';
  2. import { Cookies } from 'react-cookie';
  3. import _ from 'lodash';
  4. import swal from 'sweetalert2';
  5. import { URL_API } from '../config/keys';
  6. import headers from '../config/headers';
  7.  
  8. const cookies = new Cookies();
  9.  
  10. export const LOGIN_REQUEST = 'LOGIN_REQUEST';
  11. export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
  12. export const LOGIN_FAILURE = 'LOGIN_FAILURE';
  13.  
  14. export const LOGOUT_REQUEST = 'LOGOUT_REQUEST';
  15. export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
  16. export const LOGOUT_FAILURE = 'LOGOUT_FAILURE';
  17.  
  18. export const REGISTER_REQUEST = 'REGISTER_REQUEST';
  19. export const REGISTER_SUCCESS = 'REGISTER_SUCCESS';
  20. export const REGISTER_FAILURE = 'REGISTER_FAILURE';
  21.  
  22. function requestLogin(creds) {
  23.   return {
  24.     type: LOGIN_REQUEST,
  25.     isFetching: true,
  26.     isAuthenticated: false,
  27.     creds
  28.   };
  29. }
  30.  
  31. function receiveLogin(data) {
  32.   return {
  33.     type: LOGIN_SUCCESS,
  34.     isFetching: false,
  35.     isAuthenticated: true,
  36.     id_token: data.token
  37.   };
  38. }
  39.  
  40. function loginError(message) {
  41.   return {
  42.     type: LOGIN_FAILURE,
  43.     isFetching: false,
  44.     isAuthenticated: false,
  45.     message
  46.   };
  47. }
  48.  
  49. function requestLogout() {
  50.   return {
  51.     type: LOGOUT_REQUEST,
  52.     isFetching: true,
  53.     isAuthenticated: true
  54.   };
  55. }
  56.  
  57. function receiveLogout() {
  58.   return {
  59.     type: LOGOUT_SUCCESS,
  60.     isFetching: false,
  61.     isAuthenticated: false
  62.   };
  63. }
  64.  
  65. function setLogin(response) {
  66.   cookies.set('id_token', `Bearer ${response.data.token}`, {
  67.     path: '/'
  68.   });
  69. }
  70.  
  71. export function registerUser(data) {
  72.   return dispatch => {
  73.     dispatch(requestLogin(data));
  74.     return axios.post(`${URL_API}/auth/register`, data, { headers }).then(
  75.       response => {
  76.         dispatch(receiveLogin(response.data));
  77.         setLogin(response);
  78.         swal
  79.           .fire('Success!', 'Akun berhasil didaftarkan!', 'success')
  80.           .then(() => {
  81.             window.location.href = '/';
  82.           });
  83.         setTimeout(() => {
  84.           window.location.href = '/';
  85.         }, 3000);
  86.       },
  87.       error => {
  88.         console.log('error', error.response);
  89.         dispatch(loginError('Email atau no hp sudah terpakai'));
  90.       }
  91.     );
  92.   };
  93. }
  94.  
  95. export function loginUser(data) {
  96.   return dispatch => {
  97.     dispatch(requestLogin(data));
  98.     return axios.post(`${URL_API}/auth/login`, data, { headers }).then(
  99.       response => {
  100.         dispatch(receiveLogin(response.data));
  101.         setLogin(response);
  102.         window.location.href = '/';
  103.       },
  104.       error => {
  105.         dispatch(loginError(error.response.data.message));
  106.       }
  107.     );
  108.   };
  109. }
  110.  
  111. // Logs the user out
  112. export function logoutUser() {
  113.   return dispatch => {
  114.     dispatch(requestLogout());
  115.     cookies.remove('id_token', { path: '/' });
  116.     dispatch(receiveLogout());
  117.     window.location.href = '/';
  118.   };
  119. }
  120.  
  121. export const getCurrent = () => async dispatch => {
  122.   const token = cookies.get('id_token') || null;
  123.   axios.defaults.headers.common.Authorization = `${token}`;
  124.   const res = await axios.get(`${URL_API}/auth/profile`, { headers });
  125.   dispatch({
  126.     type: CURRENT,
  127.     payload: res.data
  128.   });
  129. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement