From 1531e9cd9c58ca1731afdd52546d507e57ad9d56 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 8 Aug 2023 15:01:32 +0500 Subject: [PATCH] chore: addressed comments --- api/src/routes/api/spec/auth.spec.ts | 4 ++-- api/src/routes/api/spec/web.spec.ts | 2 +- api/src/utils/crypto.ts | 4 ++++ api/src/utils/index.ts | 1 + web/src/containers/Settings/profile.tsx | 26 +++++++++++++----------- web/src/context/appContext.tsx | 27 +++++++++---------------- web/src/utils/types.ts | 8 ++++---- 7 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 api/src/utils/crypto.ts diff --git a/api/src/routes/api/spec/auth.spec.ts b/api/src/routes/api/spec/auth.spec.ts index c6a2444..bc9288c 100644 --- a/api/src/routes/api/spec/auth.spec.ts +++ b/api/src/routes/api/spec/auth.spec.ts @@ -1,4 +1,3 @@ -import { randomBytes } from 'crypto' import { Express } from 'express' import mongoose, { Mongoose } from 'mongoose' import { MongoMemoryServer } from 'mongodb-memory-server' @@ -14,6 +13,7 @@ import { generateAccessToken, generateAuthCode, generateRefreshToken, + randomBytesHexString, saveTokensInDB, verifyTokenInDB } from '../../../utils' @@ -52,7 +52,7 @@ describe('auth', () => { describe('token', () => { const userInfo: InfoJWT = { clientId, - userId: randomBytes(12).toString('hex') + userId: randomBytesHexString(12) } beforeAll(async () => { await userController.createUser(user) diff --git a/api/src/routes/api/spec/web.spec.ts b/api/src/routes/api/spec/web.spec.ts index 06aa7f1..1748088 100644 --- a/api/src/routes/api/spec/web.spec.ts +++ b/api/src/routes/api/spec/web.spec.ts @@ -145,7 +145,7 @@ describe('web', () => { expect(res.body.loggedIn).toBeTruthy() expect(res.body.user).toEqual({ - id: expect.anything(), + id: expect.any(String), username: user.username, displayName: user.displayName, isAdmin: user.isAdmin, diff --git a/api/src/utils/crypto.ts b/api/src/utils/crypto.ts new file mode 100644 index 0000000..d9d4b38 --- /dev/null +++ b/api/src/utils/crypto.ts @@ -0,0 +1,4 @@ +import { randomBytes } from 'crypto' + +export const randomBytesHexString = (bytesCount: number) => + randomBytes(bytesCount).toString('hex') diff --git a/api/src/utils/index.ts b/api/src/utils/index.ts index ec211c5..53c1e60 100644 --- a/api/src/utils/index.ts +++ b/api/src/utils/index.ts @@ -2,6 +2,7 @@ export * from './appStreamConfig' export * from './connectDB' export * from './copySASjsCore' export * from './createWeboutSasFile' +export * from './crypto' export * from './desktopAutoExec' export * from './extractHeaders' export * from './extractName' diff --git a/web/src/containers/Settings/profile.tsx b/web/src/containers/Settings/profile.tsx index 5f36610..4d9bae1 100644 --- a/web/src/containers/Settings/profile.tsx +++ b/web/src/containers/Settings/profile.tsx @@ -26,18 +26,20 @@ const Profile = () => { const [isPasswordModalOpen, setIsPasswordModalOpen] = useState(false) useEffect(() => { - setIsLoading(true) - axios - .get(`/SASjsApi/user/${appContext.userId}`) - .then((res: any) => { - setUser(res.data) - }) - .catch((err) => { - console.log(err) - }) - .finally(() => { - setIsLoading(false) - }) + if (appContext.userId) { + setIsLoading(true) + axios + .get(`/SASjsApi/user/${appContext.userId}`) + .then((res: any) => { + setUser(res.data) + }) + .catch((err) => { + console.log(err) + }) + .finally(() => { + setIsLoading(false) + }) + } }, [appContext.userId]) const handleChange = (event: any) => { diff --git a/web/src/context/appContext.tsx b/web/src/context/appContext.tsx index 206197c..cf556a8 100644 --- a/web/src/context/appContext.tsx +++ b/web/src/context/appContext.tsx @@ -24,39 +24,32 @@ export enum RunTimeType { interface AppContextProps { checkingSession: boolean loggedIn: boolean - setLoggedIn: Dispatch> | null + setLoggedIn?: Dispatch> needsToUpdatePassword: boolean - setNeedsToUpdatePassword: Dispatch> | null - userId: string - setUserId: Dispatch> | null + setNeedsToUpdatePassword?: Dispatch> + userId?: string + setUserId?: Dispatch> username: string - setUsername: Dispatch> | null + setUsername?: Dispatch> displayName: string - setDisplayName: Dispatch> | null + setDisplayName?: Dispatch> isAdmin: boolean - setIsAdmin: Dispatch> | null + setIsAdmin?: Dispatch> mode: ModeType runTimes: RunTimeType[] - logout: (() => void) | null + logout?: () => void } export const AppContext = createContext({ checkingSession: false, loggedIn: false, - setLoggedIn: null, needsToUpdatePassword: false, - setNeedsToUpdatePassword: null, userId: '', - setUserId: null, username: '', - setUsername: null, displayName: '', - setDisplayName: null, isAdmin: false, - setIsAdmin: null, mode: ModeType.Server, - runTimes: [], - logout: null + runTimes: [] }) const AppContextProvider = (props: { children: ReactNode }) => { @@ -64,7 +57,7 @@ const AppContextProvider = (props: { children: ReactNode }) => { const [checkingSession, setCheckingSession] = useState(false) const [loggedIn, setLoggedIn] = useState(false) const [needsToUpdatePassword, setNeedsToUpdatePassword] = useState(false) - const [userId, setUserId] = useState('') + const [userId, setUserId] = useState() const [username, setUsername] = useState('') const [displayName, setDisplayName] = useState('') const [isAdmin, setIsAdmin] = useState(false) diff --git a/web/src/utils/types.ts b/web/src/utils/types.ts index 4f9ff11..07d1fef 100644 --- a/web/src/utils/types.ts +++ b/web/src/utils/types.ts @@ -1,12 +1,12 @@ export interface UserResponse { - uid: number + uid: string username: string displayName: string isAdmin: boolean } export interface GroupResponse { - uid: number + uid: string name: string description: string } @@ -17,7 +17,7 @@ export interface GroupDetailsResponse extends GroupResponse { } export interface PermissionResponse { - uid: number + uid: string path: string type: string setting: string @@ -30,7 +30,7 @@ export interface RegisterPermissionPayload { type: string setting: string principalType: string - principalId: number + principalId: string } export interface TreeNode {