Skip to main content

18.10 Logging Users Out

actionTypes.js

src\store\actions\actionTypes.js
export const ADD_INGREDIENT = "ADD_INGREDIENT";
export const REMOVE_INGREDIENT = "REMOVE_INGREDIENT";
export const SET_INGREDIENTS = "SET_INGREDIENTS";
export const FETCH_INGREDIENTS_FAILED = "FETCH_INGREDIENTS_FAILED";

export const PURCHASE_BURGER_START = "PURCHASE_BURGER_START";
export const PURCHASE_BURGER_SUCCESS = "PURCHASE_BURGER_SUCCESS";
export const PURCHASE_BURGER_FAIL = "PURCHASE_BURGER_FAIL";
export const PURCHASE_INIT = "PURCHASE_INIT";

export const FETCH_ORDERS_START = "FETCH_ORDERS_START";
export const FETCH_ORDERS_SUCCESS = "FETCH_ORDERS_SUCCESS";
export const FETCH_ORDERS_FAIL = "FETCH_ORDERS_FAIL";

export const AUTH_START = "AUTH_START";
export const AUTH_SUCCESS = "AUTH_SUCCESS";
export const AUTH_FAIL = "AUTH_FAIL";
export const AUTH_LOGOUT = "AUTH_LOGOUT";

actions/auth.js

src\store\actions\auth.js
import axios from "axios";
import * as actionTypes from "./actionTypes";

export const authStart = () => {
return {
type: actionTypes.AUTH_START,
};
};

export const authSuccess = (token, userId) => {
return {
type: actionTypes.AUTH_SUCCESS,
idToken: token,
userId: userId,
};
};

export const authFail = (error) => {
return {
type: actionTypes.AUTH_FAIL,
error: error,
};
};

export const logout = () => {
return {
type: actionTypes.AUTH_LOGOUT,
};
};

export const checkAuthTimeout = (expirationTime) => {
return (dispatch) => {
setTimeout(() => {
dispatch(logout());
}, expirationTime * 1000);
};
};

export const auth = (email, password, isSignup) => {
return (dispatch) => {
dispatch(authStart());
const authData = {
email: email,
password: password,
returnSecureToken: true,
};
let url =
"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyDsmwPeH2yE7yqvoeYolCLgB9ju50rYivo";
if (!isSignup) {
url =
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyDsmwPeH2yE7yqvoeYolCLgB9ju50rYivo";
}
axios
.post(url, authData)
.then((response) => {
console.log(response);
dispatch(authSuccess(response.data.idToken, response.data.localId));
dispatch(checkAuthTimeout(response.data.expiresIn));
})
.catch((err) => {
dispatch(authFail(err.response.data.error));
});
};
};

reducers/auth.js

src\store\reducers\auth.js
import * as actionTypes from "../actions/actionTypes";
import { updateObject } from "./../utility";

const initialState = {
token: null,
userId: null,
error: null,
loading: false,
};

const authStart = (state, action) => {
return updateObject(state, { error: null, loading: true });
};

const authSuccess = (state, action) => {
return updateObject(state, {
token: action.idToken,
userId: action.userId,
error: null,
loading: false,
});
};

const authFail = (state, action) => {
return updateObject(state, {
error: action.error,
loading: false,
});
};

const authLogout = (state, action) => {
return updateObject(state, { token: null, userId: null });
};

const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.AUTH_START:
return authStart(state, action);
case actionTypes.AUTH_SUCCESS:
return authSuccess(state, action);
case actionTypes.AUTH_FAIL:
return authFail(state, action);
case actionTypes.AUTH_LOGOUT:
return authLogout(state, action);

default:
return state;
}
};
export default reducer;