Added fingerprinting + store in firebase for user auth. BOD-132
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { all, call, put, takeLatest } from "redux-saga/effects";
|
||||
import { all, call, put, takeLatest, delay } from "redux-saga/effects";
|
||||
import LogRocket from "logrocket";
|
||||
import { firestore } from "../../firebase/firebase.utils";
|
||||
import Fingerprint2 from "fingerprintjs2";
|
||||
|
||||
import {
|
||||
auth,
|
||||
getCurrentUser,
|
||||
@@ -12,12 +15,18 @@ import {
|
||||
signOutSuccess,
|
||||
unauthorizedUser,
|
||||
updateUserDetailsSuccess,
|
||||
setInstanceId,
|
||||
checkInstanceId,
|
||||
setInstanceConflict,
|
||||
} from "./user.actions";
|
||||
import UserActionTypes from "./user.types";
|
||||
|
||||
export function* signInWithEmail({ payload: { email, password } }) {
|
||||
try {
|
||||
const { user } = yield auth.signInWithEmailAndPassword(email, password);
|
||||
LogRocket.identify(user.email);
|
||||
|
||||
yield put(setInstanceId("123"));
|
||||
yield put(
|
||||
signInSuccess({
|
||||
uid: user.uid,
|
||||
@@ -35,7 +44,6 @@ export function* signInWithEmail({ payload: { email, password } }) {
|
||||
export function* onEmailSignInStart() {
|
||||
yield takeLatest(UserActionTypes.EMAIL_SIGN_IN_START, signInWithEmail);
|
||||
}
|
||||
|
||||
export function* isUserAuthenticated() {
|
||||
try {
|
||||
const user = yield getCurrentUser();
|
||||
@@ -43,6 +51,7 @@ export function* isUserAuthenticated() {
|
||||
yield put(unauthorizedUser());
|
||||
return;
|
||||
}
|
||||
yield put(setInstanceId(user.uid));
|
||||
LogRocket.identify(user.email);
|
||||
yield put(
|
||||
signInSuccess({
|
||||
@@ -57,11 +66,9 @@ export function* isUserAuthenticated() {
|
||||
yield put(signInFailure(error));
|
||||
}
|
||||
}
|
||||
|
||||
export function* onCheckUserSession() {
|
||||
yield takeLatest(UserActionTypes.CHECK_USER_SESSION, isUserAuthenticated);
|
||||
}
|
||||
|
||||
export function* signOutStart() {
|
||||
try {
|
||||
yield auth.signOut();
|
||||
@@ -71,11 +78,9 @@ export function* signOutStart() {
|
||||
yield put(signOutFailure(error.message));
|
||||
}
|
||||
}
|
||||
|
||||
export function* onSignOutStart() {
|
||||
yield takeLatest(UserActionTypes.SIGN_OUT_START, signOutStart);
|
||||
}
|
||||
|
||||
export function* onUpdateUserDetails() {
|
||||
yield takeLatest(UserActionTypes.UPDATE_USER_DETAILS, updateUserDetails);
|
||||
}
|
||||
@@ -88,6 +93,70 @@ 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({
|
||||
excludes: { fonts: true, audio: true, webgl: true },
|
||||
})).join(""),
|
||||
31
|
||||
);
|
||||
|
||||
yield userInstanceRef.set({
|
||||
timestamp: new Date(),
|
||||
fingerprint,
|
||||
});
|
||||
|
||||
yield delay(5000);
|
||||
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 fingerprint = Fingerprint2.x64hash128(
|
||||
(yield Fingerprint2.getPromise({
|
||||
excludes: { fonts: true, audio: true, webgl: true },
|
||||
})).join(""),
|
||||
31
|
||||
);
|
||||
|
||||
const snapshot = yield userInstanceRef.get();
|
||||
console.log("function*checkInstanceIdSaga -> snapshot", snapshot.data());
|
||||
console.log("fingerprint", fingerprint);
|
||||
|
||||
if (snapshot.data().fingerprint === fingerprint) {
|
||||
console.log("Waiting and checking.");
|
||||
yield delay(5000);
|
||||
yield put(checkInstanceId(uid));
|
||||
} else {
|
||||
console.log("Didnt match");
|
||||
yield put(setInstanceConflict());
|
||||
}
|
||||
// yield userInstanceRef.set({
|
||||
// timestamp: new Date(),
|
||||
// fingerprint,
|
||||
// });
|
||||
} catch (error) {
|
||||
console.log("error", error);
|
||||
//yield put(signOutFailure(error.message));
|
||||
//TODO error handling
|
||||
}
|
||||
}
|
||||
|
||||
export function* userSagas() {
|
||||
yield all([
|
||||
@@ -95,5 +164,7 @@ export function* userSagas() {
|
||||
call(onCheckUserSession),
|
||||
call(onSignOutStart),
|
||||
call(onUpdateUserDetails),
|
||||
call(onSetInstanceId),
|
||||
call(onCheckInstanceId),
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user