BREAKING CHANGES: Converted to use Redux stores. Login now working using Redux.
This commit is contained in:
24
client/src/redux/root.reducer.js
Normal file
24
client/src/redux/root.reducer.js
Normal 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);
|
||||
11
client/src/redux/root.saga.js
Normal file
11
client/src/redux/root.saga.js
Normal 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
19
client/src/redux/store.js
Normal 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 };
|
||||
35
client/src/redux/user/user.actions.js
Normal file
35
client/src/redux/user/user.actions.js
Normal 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
|
||||
});
|
||||
42
client/src/redux/user/user.reducer.js
Normal file
42
client/src/redux/user/user.reducer.js
Normal 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;
|
||||
87
client/src/redux/user/user.sagas.js
Normal file
87
client/src/redux/user/user.sagas.js
Normal 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)
|
||||
]);
|
||||
}
|
||||
13
client/src/redux/user/user.selectors.js
Normal file
13
client/src/redux/user/user.selectors.js
Normal 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
|
||||
);
|
||||
17
client/src/redux/user/user.types.js
Normal file
17
client/src/redux/user/user.types.js
Normal 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;
|
||||
|
||||
Reference in New Issue
Block a user