Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios';
- import { Cookies } from 'react-cookie';
- import _ from 'lodash';
- import swal from 'sweetalert2';
- import { URL_API } from '../config/keys';
- import headers from '../config/headers';
- const cookies = new Cookies();
- export const LOGIN_REQUEST = 'LOGIN_REQUEST';
- export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
- export const LOGIN_FAILURE = 'LOGIN_FAILURE';
- export const LOGOUT_REQUEST = 'LOGOUT_REQUEST';
- export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';
- export const LOGOUT_FAILURE = 'LOGOUT_FAILURE';
- export const REGISTER_REQUEST = 'REGISTER_REQUEST';
- export const REGISTER_SUCCESS = 'REGISTER_SUCCESS';
- export const REGISTER_FAILURE = 'REGISTER_FAILURE';
- function requestLogin(creds) {
- return {
- type: LOGIN_REQUEST,
- isFetching: true,
- isAuthenticated: false,
- creds
- };
- }
- function receiveLogin(data) {
- return {
- type: LOGIN_SUCCESS,
- isFetching: false,
- isAuthenticated: true,
- id_token: data.token
- };
- }
- function loginError(message) {
- return {
- type: LOGIN_FAILURE,
- isFetching: false,
- isAuthenticated: false,
- message
- };
- }
- function requestLogout() {
- return {
- type: LOGOUT_REQUEST,
- isFetching: true,
- isAuthenticated: true
- };
- }
- function receiveLogout() {
- return {
- type: LOGOUT_SUCCESS,
- isFetching: false,
- isAuthenticated: false
- };
- }
- function setLogin(response) {
- cookies.set('id_token', `Bearer ${response.data.token}`, {
- path: '/'
- });
- }
- export function registerUser(data) {
- return dispatch => {
- dispatch(requestLogin(data));
- return axios.post(`${URL_API}/auth/register`, data, { headers }).then(
- response => {
- dispatch(receiveLogin(response.data));
- setLogin(response);
- swal
- .fire('Success!', 'Akun berhasil didaftarkan!', 'success')
- .then(() => {
- window.location.href = '/';
- });
- setTimeout(() => {
- window.location.href = '/';
- }, 3000);
- },
- error => {
- console.log('error', error.response);
- dispatch(loginError('Email atau no hp sudah terpakai'));
- }
- );
- };
- }
- export function loginUser(data) {
- return dispatch => {
- dispatch(requestLogin(data));
- return axios.post(`${URL_API}/auth/login`, data, { headers }).then(
- response => {
- dispatch(receiveLogin(response.data));
- setLogin(response);
- window.location.href = '/';
- },
- error => {
- dispatch(loginError(error.response.data.message));
- }
- );
- };
- }
- // Logs the user out
- export function logoutUser() {
- return dispatch => {
- dispatch(requestLogout());
- cookies.remove('id_token', { path: '/' });
- dispatch(receiveLogout());
- window.location.href = '/';
- };
- }
- export const getCurrent = () => async dispatch => {
- const token = cookies.get('id_token') || null;
- axios.defaults.headers.common.Authorization = `${token}`;
- const res = await axios.get(`${URL_API}/auth/profile`, { headers });
- dispatch({
- type: CURRENT,
- payload: res.data
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement