Skip to content

Commit

Permalink
Storing the Response Token from Sing-In/Sign-Up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch-sriram committed Sep 4, 2020
1 parent 04a27f1 commit 674882c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ Dependency Installation: **`npm i --save redux-thunk`**
2. Adding **`ACTION Creators`** for the Auth Form: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/0a75199f9f50f0ba10fa05489739b6541152f27c)
3. Getting a Token from the Backend for Sign-Up: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/57771e243efe0719b86938a041656073b30a69d4)
4. Adding Sign-In for Authorized Users: [Commit Details](https://github.com/Ch-sriram/burger-builder/commit/2e00bc34c30fe005f6aa303191a4ad7050b613c5)
5. Storing the Response Token from Sing-In/Sign-Up: [Commit Details]()
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import App from './App';
import * as serviceWorker from './serviceWorker';
import burgerBuilderReducer from './store/reducers/burgerBuilder';
import orderReducer from './store/reducers/order';
import authReducer from './store/reducers/auth';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

// REDUCER SETUP - Combining Reducers
const rootReducer = combineReducers({
burgerBuilder: burgerBuilderReducer,
order: orderReducer,
auth: authReducer,
});

const store = createStore(rootReducer, composeEnhancers(applyMiddleware(thunk)));
Expand Down
8 changes: 5 additions & 3 deletions src/store/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import * as actionTypes from './actionTypes';

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

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

Expand Down Expand Up @@ -40,7 +41,8 @@ export const auth = (email, password, isSignup) => dispatch => {
.post(url, authData)
.then((res) => {
console.log(res);
dispatch(authSuccess(res.data));
const { idToken, localId } = res.data;
dispatch(authSuccess(idToken, localId));
return res;
})
.catch((err) => {
Expand Down
24 changes: 24 additions & 0 deletions src/store/reducers/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as actionTypes from '../actions/actionTypes';
import { updateObject } from '../utility';

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

const authStart = (state, action) => updateObject(state, { error: null, loading: true });
const authSuccess = (state, action) => updateObject(state, { token: action.idToken, userId: action.userId, error: null, loading: false, });
const authFail = (state, action) => 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;

0 comments on commit 674882c

Please sign in to comment.