Added a testing sign in method for Redux.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { all, call } from "redux-saga/effects";
|
||||
|
||||
// import { userSagas } from "./user/user.sagas";
|
||||
import { userSagas } from "./user/user.sagas";
|
||||
// import { messagingSagas } from "./messaging/messaging.sagas";
|
||||
// import { emailSagas } from "./email/email.sagas";
|
||||
// import { modalsSagas } from "./modals/modals.sagas";
|
||||
@@ -9,7 +9,7 @@ import { all, call } from "redux-saga/effects";
|
||||
|
||||
export default function* rootSaga() {
|
||||
yield all([
|
||||
// call(userSagas),
|
||||
call(userSagas),
|
||||
// call(messagingSagas),
|
||||
// call(emailSagas),
|
||||
// call(modalsSagas),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// import UserActionTypes from "./user.types";
|
||||
import UserActionTypes from "./user.types";
|
||||
|
||||
const INITIAL_STATE = {
|
||||
currentUser: {
|
||||
@@ -17,12 +17,6 @@ const INITIAL_STATE = {
|
||||
|
||||
const userReducer = (state = INITIAL_STATE, action) => {
|
||||
switch (action.type) {
|
||||
// case UserActionTypes.SET_LOCAL_FINGERPRINT:
|
||||
// return { ...state, fingerprint: action.payload };
|
||||
// case UserActionTypes.SET_INSTANCE_ID:
|
||||
// return { ...state, conflict: false };
|
||||
// case UserActionTypes.SET_INSTANCE_CONFLICT:
|
||||
// return { ...state, conflict: true };
|
||||
// case UserActionTypes.VALIDATE_PASSWORD_RESET_START:
|
||||
// case UserActionTypes.SEND_PASSWORD_RESET_EMAIL_START:
|
||||
// return {
|
||||
@@ -42,47 +36,47 @@ const userReducer = (state = INITIAL_STATE, action) => {
|
||||
// ...state,
|
||||
// passwordreset: { ...state.passwordreset, success: true },
|
||||
// };
|
||||
// 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_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.SET_USER_LANGUAGE:
|
||||
// return {
|
||||
// ...state,
|
||||
// language: action.payload,
|
||||
// };
|
||||
// case UserActionTypes.UPDATE_USER_DETAILS_SUCCESS:
|
||||
// return {
|
||||
// ...state,
|
||||
// currentUser: {
|
||||
// ...state.currentUser,
|
||||
// ...action.payload, //Spread current user details in.
|
||||
// },
|
||||
// };
|
||||
case UserActionTypes.UPDATE_USER_DETAILS_SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
currentUser: {
|
||||
...state.currentUser,
|
||||
...action.payload, //Spread current user details in.
|
||||
},
|
||||
};
|
||||
|
||||
// case UserActionTypes.SET_SHOP_DETAILS:
|
||||
// return { ...state, bodyshop: action.payload };
|
||||
// case UserActionTypes.SIGN_IN_FAILURE:
|
||||
// case UserActionTypes.SIGN_OUT_FAILURE:
|
||||
// case UserActionTypes.EMAIL_SIGN_UP_FAILURE:
|
||||
// return {
|
||||
// ...state,
|
||||
// error: action.payload,
|
||||
// };
|
||||
case UserActionTypes.SET_SHOP_DETAILS:
|
||||
return { ...state, bodyshop: action.payload };
|
||||
case UserActionTypes.SIGN_IN_FAILURE:
|
||||
case UserActionTypes.SIGN_OUT_FAILURE:
|
||||
case UserActionTypes.EMAIL_SIGN_UP_FAILURE:
|
||||
return {
|
||||
...state,
|
||||
error: action.payload,
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -1,28 +1,20 @@
|
||||
import Fingerprint2 from "fingerprintjs2";
|
||||
import LogRocket from "logrocket";
|
||||
import { all, call, delay, put, select, takeLatest } from "redux-saga/effects";
|
||||
import { all, call, put, takeLatest } from "redux-saga/effects";
|
||||
import {
|
||||
auth,
|
||||
firestore,
|
||||
getCurrentUser,
|
||||
logImEXEvent,
|
||||
updateCurrentUser,
|
||||
} from "../../firebase/firebase.utils";
|
||||
import {
|
||||
checkInstanceId,
|
||||
setInstanceConflict,
|
||||
setInstanceId,
|
||||
setLocalFingerprint,
|
||||
sendPasswordResetFailure,
|
||||
sendPasswordResetSuccess,
|
||||
signInFailure,
|
||||
signInSuccess,
|
||||
signOutFailure,
|
||||
signOutSuccess,
|
||||
unauthorizedUser,
|
||||
updateUserDetailsSuccess,
|
||||
sendPasswordResetFailure,
|
||||
sendPasswordResetSuccess,
|
||||
validatePasswordResetSuccess,
|
||||
validatePasswordResetFailure,
|
||||
validatePasswordResetSuccess,
|
||||
} from "./user.actions";
|
||||
import UserActionTypes from "./user.types";
|
||||
|
||||
@@ -31,10 +23,8 @@ export function* onEmailSignInStart() {
|
||||
}
|
||||
export function* signInWithEmail({ payload: { email, password } }) {
|
||||
try {
|
||||
logImEXEvent("redux_sign_in_attempt", { user: email });
|
||||
|
||||
//logImEXEvent("redux_sign_in_attempt", { user: email });
|
||||
const { user } = yield auth.signInWithEmailAndPassword(email, password);
|
||||
|
||||
yield put(
|
||||
signInSuccess({
|
||||
uid: user.uid,
|
||||
@@ -46,7 +36,7 @@ export function* signInWithEmail({ payload: { email, password } }) {
|
||||
);
|
||||
} catch (error) {
|
||||
yield put(signInFailure(error));
|
||||
logImEXEvent("redux_sign_in_failure", { user: email, error });
|
||||
//logImEXEvent("redux_sign_in_failure", { user: email, error });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +45,7 @@ export function* onCheckUserSession() {
|
||||
}
|
||||
export function* isUserAuthenticated() {
|
||||
try {
|
||||
logImEXEvent("redux_auth_check");
|
||||
//logImEXEvent("redux_auth_check");
|
||||
|
||||
const user = yield getCurrentUser();
|
||||
if (!user) {
|
||||
@@ -63,7 +53,6 @@ export function* isUserAuthenticated() {
|
||||
return;
|
||||
}
|
||||
|
||||
LogRocket.identify(user.email);
|
||||
yield put(
|
||||
signInSuccess({
|
||||
uid: user.uid,
|
||||
@@ -82,7 +71,7 @@ export function* onSignOutStart() {
|
||||
}
|
||||
export function* signOutStart() {
|
||||
try {
|
||||
logImEXEvent("redux_sign_out");
|
||||
//logImEXEvent("redux_sign_out");
|
||||
|
||||
yield auth.signOut();
|
||||
yield put(signOutSuccess());
|
||||
@@ -104,65 +93,13 @@ export function* updateUserDetails(userDetails) {
|
||||
//TODO error handling
|
||||
}
|
||||
}
|
||||
export function* onSetInstanceId() {
|
||||
yield takeLatest(UserActionTypes.SET_INSTANCE_ID, setInstanceIdSaga);
|
||||
}
|
||||
export function* setInstanceIdSaga({ payload: uid }) {
|
||||
try {
|
||||
const userInstanceRef = firestore.doc(`userInstance/${uid}`);
|
||||
|
||||
const fingerprint = Fingerprint2.x64hash128(
|
||||
(yield Fingerprint2.getPromise({})).map((c) => c.value).join(""),
|
||||
31
|
||||
);
|
||||
|
||||
yield userInstanceRef.set({
|
||||
timestamp: new Date(),
|
||||
fingerprint,
|
||||
});
|
||||
|
||||
yield put(setLocalFingerprint(fingerprint));
|
||||
yield delay(5 * 60 * 1000);
|
||||
yield put(checkInstanceId(uid));
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
//yield put(signOutFailure(error.message));
|
||||
//TODO error handling
|
||||
}
|
||||
}
|
||||
|
||||
export function* onCheckInstanceId() {
|
||||
yield takeLatest(UserActionTypes.CHECK_INSTANCE_ID, checkInstanceIdSaga);
|
||||
}
|
||||
export function* checkInstanceIdSaga({ payload: uid }) {
|
||||
try {
|
||||
const userInstanceRef = firestore.doc(`userInstance/${uid}`);
|
||||
|
||||
const snapshot = yield userInstanceRef.get();
|
||||
let fingerprint = yield select((state) => state.user.fingerprint);
|
||||
|
||||
if (snapshot.data().fingerprint === fingerprint) {
|
||||
yield delay(5 * 60 * 1000);
|
||||
yield put(checkInstanceId(uid));
|
||||
} else {
|
||||
console.log("ERROR: Fingerprints do not match. Conflict detected.");
|
||||
logImEXEvent("instance_confict");
|
||||
yield put(setInstanceConflict());
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
//TODO error handling
|
||||
}
|
||||
}
|
||||
|
||||
export function* onSignInSuccess() {
|
||||
yield takeLatest(UserActionTypes.SIGN_IN_SUCCESS, signInSuccessSaga);
|
||||
}
|
||||
|
||||
export function* signInSuccessSaga({ payload }) {
|
||||
LogRocket.identify(payload.email);
|
||||
yield put(setInstanceId(payload.uid));
|
||||
yield logImEXEvent("redux_sign_in_success");
|
||||
//yield logImEXEvent("redux_sign_in_success");
|
||||
}
|
||||
|
||||
export function* onSendPasswordResetStart() {
|
||||
@@ -176,7 +113,6 @@ export function* sendPasswordResetEmail({ payload }) {
|
||||
yield auth.sendPasswordResetEmail(payload, {
|
||||
url: "https://imex.online/passwordreset",
|
||||
});
|
||||
console.log("Good should send.");
|
||||
yield put(sendPasswordResetSuccess());
|
||||
} catch (error) {
|
||||
yield put(sendPasswordResetFailure(error.message));
|
||||
@@ -194,7 +130,6 @@ export function* validatePasswordResetStart({ payload: { password, code } }) {
|
||||
yield auth.confirmPasswordReset(code, password);
|
||||
yield put(validatePasswordResetSuccess());
|
||||
} catch (error) {
|
||||
console.log("function*validatePasswordResetStart -> error", error);
|
||||
yield put(validatePasswordResetFailure(error.message));
|
||||
}
|
||||
}
|
||||
@@ -205,8 +140,7 @@ export function* userSagas() {
|
||||
call(onCheckUserSession),
|
||||
call(onSignOutStart),
|
||||
call(onUpdateUserDetails),
|
||||
call(onSetInstanceId),
|
||||
call(onCheckInstanceId),
|
||||
|
||||
call(onSignInSuccess),
|
||||
call(onSendPasswordResetStart),
|
||||
call(onValidatePasswordResetStart),
|
||||
|
||||
Reference in New Issue
Block a user