From c653f807f454520a8d6888710579008bf1634cc1 Mon Sep 17 00:00:00 2001 From: akhilmhdh Date: Tue, 27 Dec 2022 23:22:03 +0530 Subject: [PATCH] fix(api-frontend): resolved failure in inviting existing infisical users to organization --- .../controllers/v1/membershipOrgController.ts | 18 ++++++++++++------ frontend/components/RouteGuard.js | 1 + .../components/utilities/SecurityClient.ts | 11 +++++++++-- frontend/pages/api/auth/CheckAuth.ts | 8 +------- frontend/pages/signupinvite.js | 13 +++++++++++-- 5 files changed, 34 insertions(+), 17 deletions(-) diff --git a/backend/src/controllers/v1/membershipOrgController.ts b/backend/src/controllers/v1/membershipOrgController.ts index 400147a169..5628cda1a9 100644 --- a/backend/src/controllers/v1/membershipOrgController.ts +++ b/backend/src/controllers/v1/membershipOrgController.ts @@ -218,12 +218,6 @@ export const verifyUserToOrganization = async (req: Request, res: Response) => { const { email, code } = req.body; user = await User.findOne({ email }).select('+publicKey'); - if (user && user?.publicKey) { - // case: user has already completed account - return res.status(403).send({ - error: 'Failed email magic link verification for complete account' - }); - } const membershipOrg = await MembershipOrg.findOne({ inviteEmail: email, @@ -238,6 +232,18 @@ export const verifyUserToOrganization = async (req: Request, res: Response) => { code }); + if (user && user?.publicKey) { + // case: user has already completed account + // membership can be approved and redirected to login/dashboard + membershipOrg.status = ACCEPTED; + await membershipOrg.save(); + + return res.status(200).send({ + message: 'Successfully verified email', + user, + }); + } + if (!user) { // initialize user account user = await new User({ diff --git a/frontend/components/RouteGuard.js b/frontend/components/RouteGuard.js index d08972b997..c5f6feb35e 100644 --- a/frontend/components/RouteGuard.js +++ b/frontend/components/RouteGuard.js @@ -48,6 +48,7 @@ export default function RouteGuard({ children }) { // Check if the user is authenticated const response = await checkAuth(); // #TODO: figure our why sometimes it doesn't output a response + // ANS(akhilmhdh): Because inside the security client the await token() doesn't have try/catch if (!publicPaths.includes(path)) { try { if (response.status !== 200) { diff --git a/frontend/components/utilities/SecurityClient.ts b/frontend/components/utilities/SecurityClient.ts index ea2664c70b..3adfa1dc15 100644 --- a/frontend/components/utilities/SecurityClient.ts +++ b/frontend/components/utilities/SecurityClient.ts @@ -16,12 +16,19 @@ export default class SecurityClient { const req = new Request(resource, options); if (this.#token == '') { - this.setToken(await token()); + try { + // TODO: This should be moved to a context to do it only once when app loads + // this try catch saves route guard from a stuck state + this.setToken(await token()); + } catch (error) { + console.error("Unauthorized access"); + } } if (this.#token) { req.headers.set('Authorization', 'Bearer ' + this.#token); - return fetch(req); } + + return fetch(req); } } diff --git a/frontend/pages/api/auth/CheckAuth.ts b/frontend/pages/api/auth/CheckAuth.ts index 2578d7ce9a..1decb5bf35 100644 --- a/frontend/pages/api/auth/CheckAuth.ts +++ b/frontend/pages/api/auth/CheckAuth.ts @@ -10,13 +10,7 @@ const checkAuth = async () => { headers: { 'Content-Type': 'application/json' } - }).then((res) => { - if (res && res.status == 200) { - return res; - } else { - console.log('Not authorized'); - } - }); + }).then((res) => res); }; export default checkAuth; diff --git a/frontend/pages/signupinvite.js b/frontend/pages/signupinvite.js index 7686161a2a..169c5c014c 100644 --- a/frontend/pages/signupinvite.js +++ b/frontend/pages/signupinvite.js @@ -159,8 +159,17 @@ export default function SignupInvite() { code: token }); if (response.status == 200) { - setVerificationToken((await response.json()).token); - setStep(2); + const res = await response.json(); + // user will have temp token if doesn't have an account + // then continue with account setup workflow + if(res?.token){ + setVerificationToken(res.token); + setStep(2); + } else { + // user will be redirected to dashboard + // if not logged in gets kicked out to login + router.push("/dashboard") + } } else { console.log('ERROR', response); router.push('/requestnewinvite');