import { AuthActionTypes , REQUEST_LOGIN , REQUEST_LOGIN_SUCCESS , LOGOUT , REQUEST_SIGN_UP , REQUEST_LOGIN_ERROR , REQUEST_SIGN_UP_ERROR } from './types' ; import { Dispatch } from 'redux' ; import { login , signup } from '../../shared/services' ; export function requestLogin ( email , password ) { return { type : REQUEST_LOGIN , email , password } ; } export function requestLoginSuccess ( token ) { return { type : REQUEST_LOGIN_SUCCESS , token } ; } export function requestLoginError ( error ) { return { type : REQUEST_LOGIN_ERROR , error } ; } export function logout ( ) { return { type : LOGOUT } ; } export function requestSignUp ( email , password ) { return { type : REQUEST_SIGN_UP , email , password } ; } export function requestSignUpError ( error ) { return { type : REQUEST_SIGN_UP_ERROR , error } ; } export function loginUser ( email , password ) { return function ( dispatch ) { dispatch ( requestLogin ( email , password ) ) ; return login ( email , password ) . then ( jwt => { dispatch ( requestLoginSuccess ( jwt ) ) ; } ) . catch ( error => { dispatch ( requestLoginError ( error ) ) ; } ) ; } ; } export function signUpUser ( email , password ) { return function ( dispatch ) { dispatch ( requestSignUp ( email , password ) ) ; return signup ( email , password ) . then ( ( ) => { loginUser ( email , password ) ( dispatch ) ; } ) . catch ( error => { dispatch ( requestSignUpError ( error ) ) ; } ) ; } ; }