Skip to content

Commit

Permalink
fix(passport): logging-in is prevented when already logged-in (#2437)
Browse files Browse the repository at this point in the history
  • Loading branch information
poolsar42 authored Jun 22, 2023
1 parent d9d234c commit 3ed5128
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
22 changes: 14 additions & 8 deletions apps/passport/app/routes/authenticate/$clientId/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import Authentication, {
import { Text } from '@proofzero/design-system/src/atoms/text/Text'
import { Avatar } from '@proofzero/packages/design-system/src/atoms/profile/avatar/Avatar'
import { Button } from '@proofzero/packages/design-system/src/atoms/buttons/Button'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { GetAppPublicPropsResult } from '@proofzero/platform/starbase/src/jsonrpc/methods/getAppPublicProps'
import {
getErrorCause,
getRollupReqFunctionErrorWrapper,
} from '@proofzero/utils/errors'
import type { GetAppPublicPropsResult } from '@proofzero/platform/starbase/src/jsonrpc/methods/getAppPublicProps'

const LazyAuth = lazy(() =>
import('../../../web3/lazyAuth').then((module) => ({
Expand Down Expand Up @@ -204,13 +207,17 @@ const InnerComponent = ({
// fetch nonce and kickoff sign flow
setLoading(true)
await fetch(`/connect/${address}/sign`) // NOTE: note using fetch because it messes with wagmi state
.then((res) =>
res.json<{
.then(async (res) => {
const resJson = await res.json<{
nonce: string
state: string
address: string
}>()
)
if (!res.ok) {
throw getErrorCause(resJson)
}
return resJson
})
.then(({ nonce, state, address }) => {
setSignData({
nonce,
Expand All @@ -219,12 +226,11 @@ const InnerComponent = ({
signature: undefined,
})
})
.catch(() => {
.catch((ex) => {
toast(ToastType.Error, {
message:
'Could not fetch nonce for signing authentication message',
'Could not complete authentication. Please return to application and try again.',
})
navigate('/')
})
setLoading(false)
},
Expand Down
11 changes: 6 additions & 5 deletions apps/passport/app/routes/connect/$address/sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { getAuthzRedirectURL } from '../../../utils/authenticate.server'

import { parseJwt } from '@proofzero/packages/utils'
import { BadRequestError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import {
JsonError,
getRollupReqFunctionErrorWrapper,
} from '@proofzero/utils/errors'
import type { AccountURN } from '@proofzero/urns/account'
import {
AuthenticationScreenDefaults,
Expand Down Expand Up @@ -43,12 +46,10 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
let clientId: string = ''
try {
const res = await getAuthzCookieParams(request, context.env)
if (res === null) {
throw new Error()
}
clientId = res.clientId
} catch (ex) {
throw redirect('/')
const traceparent = context.traceSpan.getTraceParent()
return JsonError(ex, traceparent)
}
if (clientId !== 'console' && clientId !== 'passport') {
const sbClient = getStarbaseClient('', context.env, context.traceSpan)
Expand Down
17 changes: 12 additions & 5 deletions apps/passport/app/routes/connect/email/otp.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { EmailAddressType, NodeType } from '@proofzero/types/address'
import { AddressURNSpace } from '@proofzero/urns/address'
import { generateHashedIDRef } from '@proofzero/urns/idref'
import { JsonError } from '@proofzero/utils/errors'
import { json } from '@remix-run/cloudflare'
import { getAddressClient, getStarbaseClient } from '~/platform.server'

import type { ActionFunction, LoaderFunction } from '@remix-run/cloudflare'
import { getAuthzCookieParams } from '~/session.server'
import { SendOTPEmailThemeProps } from '@proofzero/platform/email/src/jsonrpc/methods/sendOTPEmail'
import { BadRequestError } from '@proofzero/errors'
import type { SendOTPEmailThemeProps } from '@proofzero/platform/email/src/jsonrpc/methods/sendOTPEmail'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'

export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
Expand All @@ -32,8 +31,16 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
context.traceSpan
)

const { clientId } = await getAuthzCookieParams(request, context.env)

let clientId: string = ''
try {
const res = await getAuthzCookieParams(request, context.env)
clientId = res.clientId
} catch (ex) {
throw new InternalServerError({
message:
'Could not complete authentication. Please return to application and try again.',
})
}
const starbaseClient = getStarbaseClient(
undefined,
context.env,
Expand Down
13 changes: 10 additions & 3 deletions apps/passport/app/utils/emailOTP.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { getErrorCause } from '@proofzero/utils/errors'

export const generateEmailOTP = async (
email: string
): Promise<{ message: string; state: string; status: number } | undefined> => {
const reqUrl = `/connect/email/otp?email=${encodeURIComponent(email)}`

const resObj = await fetch(reqUrl)
const res = await resObj.json<{
const res = await fetch(reqUrl)

const resObj = await res.json<{
message: string
state: string
}>()

return { message: res.message, state: res.state, status: resObj.status }
if (!res.ok) {
throw getErrorCause(resObj)
}

return { message: resObj.message, state: resObj.state, status: res.status }
}

0 comments on commit 3ed5128

Please sign in to comment.