chukwuyem

action

Oct 16th, 2020
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from 'axios'
  2. import React from 'react';
  3.  
  4. import { Redirect } from "react-router-dom";
  5.  
  6.  
  7. import {AUTH_USER_REQUEST, AUTH_USER_SUCCESS, AUTH_USER_FAILURE} from './authTypes'
  8.  
  9.  
  10. export const loginUsers = (email, pass, error, history) => {
  11.     return (dispatch) => {
  12.       dispatch(authUserRequest())
  13.       axios
  14.         .post('http://127.0.0.1:8000/token', {username: email, password:pass, error:error})
  15.         .then(response => {
  16.          //  response.data is the users
  17.          dispatch(authUserSuccess())
  18.           const user = response.data
  19.           dispatch(authUserSuccess(user))
  20.           console.log(response)
  21.           alert("Hello! I am an alert box!!");
  22.  
  23.           //return <Redirect to = {{ pathname: "/dashboard" }} />;
  24.           history.push('/dashboard')
  25.         })
  26.         .catch(error => {
  27.           dispatch(authUserFailure())
  28.           console.log(error.response)
  29.           // error.message is the error message
  30.           dispatch(authUserFailure(error.response))
  31.         })
  32.     }
  33.   }
  34.  
  35. export const authUserRequest = () => {
  36.     return{
  37.         type: AUTH_USER_REQUEST
  38.        
  39.     }
  40. }
  41.  
  42. export const authUserSuccess = (email, pass) => {
  43.     return{
  44.         type: AUTH_USER_SUCCESS,
  45.         payload: {email, pass}
  46.        
  47.     }
  48. }
  49.  
  50. export const authUserFailure = error => {
  51.     return{
  52.         type: AUTH_USER_FAILURE,
  53.         payload: error
  54.        
  55.     }
  56. }
RAW Paste Data