From cc13a6cd5af723ab86e6778f924440760e6ff325 Mon Sep 17 00:00:00 2001 From: zx <67887489+tan-zx@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:20:18 +0800 Subject: [PATCH] hotfix: move setHasLoginStateFlag (#202) * hotfix: move setHasLoginStateFlag * use async await --- .../sign-in/components/SgidCallback.tsx | 8 +++++-- .../sign-in/components/SgidLoginButton.tsx | 5 ----- src/server/modules/auth/sgid/sgid.router.ts | 21 +++++++++++-------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/features/sign-in/components/SgidCallback.tsx b/src/features/sign-in/components/SgidCallback.tsx index aa501b98..c0219579 100644 --- a/src/features/sign-in/components/SgidCallback.tsx +++ b/src/features/sign-in/components/SgidCallback.tsx @@ -1,12 +1,15 @@ import { useRouter } from 'next/router' import { FullscreenSpinner } from '~/components/FullscreenSpinner' +import { useLoginState } from '~/features/auth' import { trpc } from '~/utils/trpc' /** * This component is responsible for handling the callback from the SGID login. */ export const SgidCallback = () => { + const utils = trpc.useContext() const router = useRouter() + const { setHasLoginStateFlag } = useLoginState() const { query: { code, state }, } = router @@ -18,12 +21,13 @@ export const SgidCallback = () => { }, { onSuccess: async ({ redirectUrl }) => { + setHasLoginStateFlag() + await utils.me.get.invalidate() await router.replace(redirectUrl) }, onError: async (error) => { - // Server should return redirectUrl even on error, this function is a fallback. console.error(error) - await router.replace('/sign-in') + await router.replace(`/sign-in?error=${error.message}`) }, } ) diff --git a/src/features/sign-in/components/SgidLoginButton.tsx b/src/features/sign-in/components/SgidLoginButton.tsx index 94cad423..b017d92e 100644 --- a/src/features/sign-in/components/SgidLoginButton.tsx +++ b/src/features/sign-in/components/SgidLoginButton.tsx @@ -3,17 +3,12 @@ import { useRouter } from 'next/router' import { useFeatures } from '~/components/AppProviders' import { trpc } from '~/utils/trpc' import { CALLBACK_URL_KEY } from '~/constants/params' -import { useLoginState } from '~/features/auth' export const SgidLoginButton = (): JSX.Element | null => { - const { setHasLoginStateFlag } = useLoginState() - const utils = trpc.useContext() const router = useRouter() const { sgid } = useFeatures() const sgidLoginMutation = trpc.auth.sgid.login.useMutation({ onSuccess: async ({ redirectUrl }) => { - await utils.me.get.invalidate() - setHasLoginStateFlag() await router.push(redirectUrl) }, }) diff --git a/src/server/modules/auth/sgid/sgid.router.ts b/src/server/modules/auth/sgid/sgid.router.ts index 415605ba..700e2fcb 100644 --- a/src/server/modules/auth/sgid/sgid.router.ts +++ b/src/server/modules/auth/sgid/sgid.router.ts @@ -79,20 +79,23 @@ export const sgidRouter = router({ ) .query(async ({ ctx, input: { state, code } }) => { if (!env.NEXT_PUBLIC_ENABLE_SGID) { - return { - redirectUrl: `/sign-in?error=${'SGID is not enabled'}`, - } + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'SGID is not enabled', + }) } if (!ctx.session?.sgidSessionState) { - return { - redirectUrl: `/sign-in?error=${'Invalid login flow'}`, - } + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Invalid login flow', + }) } const parsedState = sgidCallbackStateSchema.safeParse(state) if (!parsedState.success) { - return { - redirectUrl: `/sign-in?error=${'Invalid SGID state'}`, - } + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Invalid SGID callback state', + }) } const { codeVerifier, nonce } = ctx.session.sgidSessionState