Skip to main content

18.08 Storing The Token

SignIn Endpoint

Source: firebase.google.com: Section Sign In Email Password

https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]

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 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));
})
.catch((err) => {
console.log(err);
dispatch(authFail(err));
});
};
};

New 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 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);

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

src/index.js

src\index.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { BrowserRouter } from "react-router-dom";
import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import thunk from "redux-thunk";
import App from "./App";
import "./index.css";
import registerServiceWorker from "./registerServiceWorker";
import authReducer from "./store/reducers/auth";
import burgerBuilderReducer from "./store/reducers/burgerBuilder";
import orderReducer from "./store/reducers/order";

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const rootReducer = combineReducers({
burgerBuilder: burgerBuilderReducer,
order: orderReducer,
auth: authReducer,
});

const store = createStore(
rootReducer,
composeEnhancers(applyMiddleware(thunk))
);

const app = (
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
);
ReactDOM.render(app, document.getElementById("root"));
registerServiceWorker();