fixed all redirect logic

This commit is contained in:
Abhinav-grd 2021-08-16 11:12:48 +05:30
parent 6f57650f6e
commit f381a3cb63
6 changed files with 57 additions and 45 deletions

View file

@ -1,7 +1,7 @@
import React, { useState, useEffect, useContext } from 'react';
import constants from 'utils/strings/constants';
import { logoutUser, putAttributes } from 'services/userService';
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import { logoutUser, putAttributes, User } from 'services/userService';
import { getData, LS_KEYS } from 'utils/storage/localStorage';
import { useRouter } from 'next/router';
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
import {
@ -33,29 +33,23 @@ export default function Generate() {
useEffect(() => {
const main = async () => {
setLoading(true);
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
const key: string = getKey(SESSION_KEYS.ENCRYPTION_KEY);
const keyAttributes: KeyAttributes = getData(
LS_KEYS.ORIGINAL_KEY_ATTRIBUTES
);
router.prefetch('/gallery');
const user = getData(LS_KEYS.USER);
router.prefetch('/credentials');
const user: User = getData(LS_KEYS.USER);
if (!user?.token) {
router.push('/');
return;
}
setToken(user.token);
if (keyAttributes?.encryptedKey) {
try {
await putAttributes(user.token, keyAttributes);
} catch (e) {
// ignore
}
setData(LS_KEYS.ORIGINAL_KEY_ATTRIBUTES, null);
setRecoveryModalView(true);
} else if (keyAttributes?.encryptedKey) {
router.push('/credentials');
} else if (key) {
router.push('/gallery');
} else {
setToken(user.token);
setLoading(false);
}
setLoading(false);
};
main();
appContext.showNavBar(true);

View file

@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react';
import constants from 'utils/strings/constants';
import { getData, LS_KEYS } from 'utils/storage/localStorage';
import { clearData, getData, LS_KEYS } from 'utils/storage/localStorage';
import { useRouter } from 'next/router';
import { KeyAttributes } from 'types';
import CryptoWorker, { SaveKeyInSessionStore } from 'utils/crypto';
@ -11,7 +11,8 @@ import { Card, Button } from 'react-bootstrap';
import { AppContext } from 'pages/_app';
import LogoImg from 'components/LogoImg';
import { logError } from 'utils/sentry';
import { SESSION_KEYS } from 'utils/storage/sessionStorage';
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
import { User } from 'services/userService';
export default function Recover() {
const router = useRouter();
@ -21,12 +22,19 @@ export default function Recover() {
useEffect(() => {
router.prefetch('/gallery');
const user = getData(LS_KEYS.USER);
const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
if (!user?.token) {
const user: User = getData(LS_KEYS.USER);
const keyAttributes: KeyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
if (
(!user?.token && !user?.encryptedToken) ||
!keyAttributes?.memLimit
) {
clearData();
router.push('/');
} else if (!keyAttributes) {
router.push('/generate');
} else if (key) {
router.push('/gallery');
} else {
setKeyAttributes(keyAttributes);
}

View file

@ -20,10 +20,13 @@ export default function Recover() {
useEffect(() => {
router.prefetch('/gallery');
const user = getData(LS_KEYS.USER);
if (!user?.email) {
if (!user.isTwoFactorEnabled && (user.encryptedToken || user.token)) {
router.push('/credential');
} else if (!user.email || !user.twoFactorSessionID) {
router.push('/');
} else {
setSessionID(user.twoFactorSessionID);
}
setSessionID(user.twoFactorSessionID);
const main = async () => {
const resp = await recoverTwoFactor(user.twoFactorSessionID);
setEncryptedTwoFactorSecret({

View file

@ -4,22 +4,25 @@ import VerifyTwoFactor from 'components/VerifyTwoFactor';
import router from 'next/router';
import React, { useEffect, useState } from 'react';
import { Button, Card } from 'react-bootstrap';
import { logoutUser, verifyTwoFactor } from 'services/userService';
import { logoutUser, User, verifyTwoFactor } from 'services/userService';
import { setData, LS_KEYS, getData } from 'utils/storage/localStorage';
import constants from 'utils/strings/constants';
export default function Home() {
const [email, setEmail] = useState('');
const [sessionID, setSessionID] = useState('');
useEffect(() => {
const main = async () => {
router.prefetch('/credentials');
const user = getData(LS_KEYS.USER);
if (!user?.email) {
const user: User = getData(LS_KEYS.USER);
if (
!user.isTwoFactorEnabled &&
(user.encryptedToken || user.token)
) {
router.push('/credential');
} else if (!user?.email || !user.twoFactorSessionID) {
router.push('/');
} else {
setEmail(user.email);
setSessionID(user.twoFactorSessionID);
}
};
@ -32,7 +35,6 @@ export default function Home() {
const { keyAttributes, encryptedToken, token, id } = resp;
setData(LS_KEYS.USER, {
...getData(LS_KEYS.USER),
email,
token,
encryptedToken,
id,

View file

@ -68,23 +68,24 @@ export default function Verify() {
isTwoFactorEnabled: true,
});
router.push('/two-factor/verify');
return;
}
setData(LS_KEYS.USER, {
...getData(LS_KEYS.USER),
email,
token,
encryptedToken,
id,
});
keyAttributes && setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes);
clearFiles();
setIsFirstLogin(true);
if (keyAttributes?.encryptedKey) {
clearKeys();
router.push('/credentials');
} else {
router.push('/generate');
setData(LS_KEYS.USER, {
...getData(LS_KEYS.USER),
email,
token,
encryptedToken,
id,
isTwoFactorEnabled: false,
});
keyAttributes && setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes);
clearFiles();
setIsFirstLogin(true);
if (keyAttributes?.encryptedKey) {
clearKeys();
router.push('/credentials');
} else {
router.push('/generate');
}
}
} catch (e) {
if (e?.status === 401) {

View file

@ -30,6 +30,10 @@ export interface User {
id: number;
name: string;
email: string;
token: string;
encryptedToken: string;
isTwoFactorEnabled: boolean;
twoFactorSessionID: string;
}
export interface EmailVerificationResponse {
id: number;