Skip to content

Commit

Permalink
hotfix: move setHasLoginStateFlag (#202)
Browse files Browse the repository at this point in the history
* hotfix: move setHasLoginStateFlag

* use async await
  • Loading branch information
zxt-tzx authored Sep 22, 2023
1 parent 4cf1d64 commit cc13a6c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
8 changes: 6 additions & 2 deletions src/features/sign-in/components/SgidCallback.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}`)
},
}
)
Expand Down
5 changes: 0 additions & 5 deletions src/features/sign-in/components/SgidLoginButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
})
Expand Down
21 changes: 12 additions & 9 deletions src/server/modules/auth/sgid/sgid.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

1 comment on commit cc13a6c

@vercel
Copy link

@vercel vercel bot commented on cc13a6c Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.