BREAKING CHANGES: Converted to use Redux stores. Login now working using Redux.

This commit is contained in:
Patrick Fic
2020-01-31 13:20:15 -08:00
parent f1ac052a1b
commit 3060c16dd3
26 changed files with 628 additions and 360 deletions

View File

@@ -0,0 +1,24 @@
import { combineReducers } from "redux";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import userReducer from "./user/user.reducer";
// import cartReducer from './cart/cart.reducer';
// import directoryReducer from './directory/directory.reducer';
// import shopReducer from './shop/shop.reducer';
const persistConfig = {
key: "root",
storage,
//whitelist: ["cart"]
blacklist: ["user"]
};
const rootReducer = combineReducers({
user: userReducer
// cart: cartReducer,
// directory: directoryReducer,
// shop: shopReducer
});
export default persistReducer(persistConfig, rootReducer);

View File

@@ -0,0 +1,11 @@
import { all, call } from "redux-saga/effects";
//List of all Sagas
// import { shopSagas } from "./shop/shop.sagas";
import { userSagas } from "./user/user.sagas";
//import { cartSagas } from "./cart/cart.sagas";
export default function* rootSaga() {
//All starts all the Sagas concurrently.
yield all([call(userSagas)]);
}

19
client/src/redux/store.js Normal file
View File

@@ -0,0 +1,19 @@
import { createStore, applyMiddleware } from "redux";
import { persistStore } from "redux-persist";
import logger from "redux-logger";
import createSagaMiddleware from "redux-saga";
import rootReducer from "./root.reducer";
import rootSaga from "./root.saga";
const sagaMiddleWare = createSagaMiddleware();
const middlewares = [sagaMiddleWare];
if (process.env.NODE_ENV === "development") {
middlewares.push(logger);
}
export const store = createStore(rootReducer, applyMiddleware(...middlewares));
sagaMiddleWare.run(rootSaga);
export const persistor = persistStore(store);
export default { store, persistStore };

View File

@@ -0,0 +1,35 @@
import UserActionTypes from "./user.types";
export const signInSuccess = user => ({
type: UserActionTypes.SIGN_IN_SUCCESS,
payload: user
});
export const signInFailure = errorMsg => ({
type: UserActionTypes.SIGN_IN_FAILURE,
payload: errorMsg
});
export const emailSignInStart = emailAndPassword => ({
type: UserActionTypes.EMAIL_SIGN_IN_START,
payload: emailAndPassword
});
export const checkUserSession = () => ({
type: UserActionTypes.CHECK_USER_SESSION
});
export const signOutStart = () => ({
type: UserActionTypes.SIGN_OUT_START
});
export const signOutSuccess = () => ({
type: UserActionTypes.SIGN_OUT_SUCCESS
});
export const signOutFailure = error => ({
type: UserActionTypes.SIGN_OUT_FAILURE,
payload: error
});
export const unauthorizedUser = () => ({
type: UserActionTypes.UNAUTHORIZED_USER
});

View File

@@ -0,0 +1,42 @@
import UserActionTypes from "./user.types";
const INITIAL_STATE = {
currentUser: {
authorized: null
},
error: null
};
const userReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case UserActionTypes.SIGN_IN_SUCCESS:
return {
...state,
currentUser: action.payload,
error: null
};
case UserActionTypes.SIGN_OUT_SUCCESS:
return {
...state,
currentUser: { authorized: false },
error: null
};
case UserActionTypes.UNAUTHORIZED_USER:
return {
...state,
error: null,
currentUser: { authorized: false }
};
case UserActionTypes.SIGN_IN_FAILURE:
case UserActionTypes.SIGN_OUT_FAILURE:
case UserActionTypes.EMAIL_SIGN_UP_FAILURE:
return {
...state,
error: action.payload
};
default:
return state;
}
};
export default userReducer;

View File

@@ -0,0 +1,87 @@
import { all, call, put, takeLatest } from "redux-saga/effects";
import { auth, getCurrentUser } from "../../firebase/firebase.utils";
import {
signInFailure,
signInSuccess,
signOutFailure,
signOutSuccess,
unauthorizedUser
} from "./user.actions";
import UserActionTypes from "./user.types";
// export function* getSnapshotFromUserAuth(userAuth) {
// try {
// const userRef = yield call(createUserProfileDocument, userAuth);
// //const userSnapshot = yield userRef.get();
// } catch (error) {
// yield put(signInFailure(error));
// }
// }
export function* signInWithEmail({ payload: { email, password } }) {
try {
const { user } = yield auth.signInWithEmailAndPassword(email, password);
yield put(
signInSuccess({
id: user.uid,
email: user.email,
displayName: user.displayName,
authorized: true
})
);
} catch (error) {
yield put(signInFailure(error));
}
}
//This is the listener fo rthe call, and when it finds it, it triggers somethign else.
export function* onEmailSignInStart() {
yield takeLatest(UserActionTypes.EMAIL_SIGN_IN_START, signInWithEmail);
}
export function* isUserAuthenticated() {
try {
const user = yield getCurrentUser();
if (!user) {
yield put(unauthorizedUser());
return;
}
yield put(
signInSuccess({
id: user.uid,
email: user.email,
displayName: user.displayName,
authorized: true
})
);
} catch (error) {
yield put(signInFailure(error));
}
}
export function* onCheckUserSession() {
yield takeLatest(UserActionTypes.CHECK_USER_SESSION, isUserAuthenticated);
}
export function* signOutStart() {
try {
yield auth.signOut();
yield put(signOutSuccess());
} catch (error) {
yield put(signOutFailure(error.message));
}
}
export function* onSignOutStart() {
yield takeLatest(UserActionTypes.SIGN_OUT_START, signOutStart);
}
export function* userSagas() {
yield all([
// call(onGoogleSignInStart),
call(onEmailSignInStart),
call(onCheckUserSession),
call(onSignOutStart)
// call(onEmailSignUpStart),
// call(onEmailSignUpSuccess)
]);
}

View File

@@ -0,0 +1,13 @@
import { createSelector } from "reselect";
const selectUser = state => state.user;
export const selectCurrentUser = createSelector(
[selectUser],
user => user.currentUser
);
export const selectSignInError = createSelector(
[selectUser],
user => user.error
);

View File

@@ -0,0 +1,17 @@
const UserActionTypes = {
SET_CURRENT_USER: "SET_CURRENT_USER",
GOOGLE_SIGN_IN_START: "GOOGLE_SIGN_IN_START",
SIGN_IN_SUCCESS: "SIGN_IN_SUCCESS",
SIGN_IN_FAILURE: "SIGN_IN_FAILURE",
EMAIL_SIGN_IN_START: "EMAIL_SIGN_IN_START",
CHECK_USER_SESSION: "CHECK_USER_SESSION",
SIGN_OUT_START: "SIGN_OUT_START",
SIGN_OUT_SUCCESS: "SIGN_OUT_SUCCESS",
SIGN_OUT_FAILURE: "SIGN_OUT_FAILURE",
EMAIL_SIGN_UP_START: "EMAIL_SIGN_UP_START",
EMAIL_SIGN_UP_SUCCESS: "EMAIL_SIGN_UP_SUCCESS",
EMAIL_SIGN_UP_FAILURE: "EMAIL_SIGN_UP_FAILURE",
UNAUTHORIZED_USER: "UNAUTHORIZED_USER"
};
export default UserActionTypes;