Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(passport): Improve connection flow toast messages #2358

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/console/app/routes/apps/$clientId/team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
)

export default () => {
useConnectResult(['SUCCESS', 'ALREADY_CONNECTED', 'CANCEL'])
useConnectResult()

const submit = useSubmit()

Expand Down
3 changes: 1 addition & 2 deletions apps/passport/app/routes/authorize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from '~/utils/authorize.server'
import { useEffect, useState } from 'react'
import { BadRequestError, InternalServerError } from '@proofzero/errors'
import { JsonError } from '@proofzero/utils/errors'
import { AuthorizationControlSelection } from '@proofzero/types/application'
import useConnectResult from '@proofzero/design-system/src/hooks/useConnectResult'

Expand Down Expand Up @@ -418,7 +417,7 @@ export default function Authorize() {
const navigate = useNavigate()
const transition = useTransition()

useConnectResult(['ALREADY_CONNECTED', 'CANCEL'])
useConnectResult()

const cancelCallback = () => {
const redirectURL = new URL(redirectUri)
Expand Down
13 changes: 11 additions & 2 deletions apps/passport/app/routes/connect/$address/sign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { CryptoAddressType, NodeType } from '@proofzero/types/address'
import { getAuthzCookieParams, getUserSession } from '../../../session.server'
import { getAuthzRedirectURL } from '../../../utils/authenticate.server'

import { signMessageTemplate } from '@proofzero/packages/utils'
import { parseJwt, signMessageTemplate } from '@proofzero/packages/utils'
import { BadRequestError } from '@proofzero/errors'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import type { AccountURN } from '@proofzero/urns/account'

export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context, params }) => {
Expand Down Expand Up @@ -49,6 +50,7 @@ export const loader: LoaderFunction = getRollupReqFunctionErrorWrapper(
export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context, params }) => {
const appData = await getAuthzCookieParams(request, context.env)
const jwt = await getUserSession(request, context.env, params.clientId)

const { address } = params
if (!address)
Expand All @@ -74,8 +76,15 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
forceAccountCreation: !appData || appData.rollup_action !== 'connect',
})

const accountURNFromAddress = await addressClient.getAccount.query()


if (appData?.rollup_action === 'connect' && existing) {
return redirect(getAuthzRedirectURL(appData, 'ALREADY_CONNECTED'))
const accountURN = parseJwt(jwt).sub! as AccountURN
if (accountURN === accountURNFromAddress) {
return redirect(getAuthzRedirectURL(appData, 'ALREADY_CONNECTED_ERROR'))
}
return redirect(getAuthzRedirectURL(appData, 'ACCOUNT_CONNECT_ERROR'))
}

// TODO: handle the error case
Expand Down
18 changes: 14 additions & 4 deletions apps/passport/app/utils/authenticate.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AddressURNSpace } from '@proofzero/urns/address'
import type { AddressURN } from '@proofzero/urns/address'
import type { AccountURN } from '@proofzero/urns/account'

import { JsonError } from '@proofzero/utils/errors'
import { GrantType, ResponseType } from '@proofzero/types/access'

import {
Expand All @@ -13,6 +12,7 @@ import {
import {
createUserSession,
getAuthzCookieParamsSession,
getUserSession,
parseJwt,
} from '~/session.server'
import { generateGradient } from './gradient.server'
Expand Down Expand Up @@ -40,15 +40,25 @@ export const authenticateAddress = async (
})
}

const jwt = await getUserSession(request, env, appData?.clientId)
if (
appData.rollup_action &&
['connect', 'reconnect'].includes(appData?.rollup_action)
) {
let result = undefined

if (existing && appData.rollup_action === 'connect') {
const loggedInAccount = parseJwt(jwt).sub
if (account !== loggedInAccount) {
result = 'ACCOUNT_CONNECT_ERROR'
} else {
result = 'ALREADY_CONNECTED_ERROR'
}
}

const redirectURL = getAuthzRedirectURL(
appData,
existing && appData.rollup_action === 'connect'
? 'ALREADY_CONNECTED'
: undefined
result
)

return redirect(redirectURL)
Expand Down
22 changes: 19 additions & 3 deletions packages/design-system/src/hooks/useConnectResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { ToastType, toast } from '../atoms/toast'
import { useEffect } from 'react'

export default (
handledMessageTypes: string[] = ['SUCCESS', 'ALREADY_CONNECTED', 'CANCEL']
handledMessageTypes: string[] = [
'SUCCESS',
'ACCOUNT_CONNECT_ERROR',
'ALREADY_CONNECTED_ERROR',
'CANCEL'
]
) => {
useEffect(() => {
const url = new URL(window.location.href)
Expand All @@ -18,10 +23,21 @@ export default (
{ duration: 2000 }
)
break
case 'ALREADY_CONNECTED':
case 'ACCOUNT_CONNECT_ERROR':
toast(
ToastType.Error,
{ message: 'Account already connected' },
{
message: 'Could not connect this account to your identity.\
It may be connected to another identity.' },
{ duration: 2000 }
)
break
case 'ALREADY_CONNECTED_ERROR':
toast(
ToastType.Error,
{
message: 'Account is already connected to your identity.'
},
{ duration: 2000 }
)
break
Expand Down