-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle expired/invalid cognito code (#1275)
<!--- Please provide a general summary of your changes in the title above --> # Pull Request type <!-- Please try to limit your pull request to one type; submit multiple pull requests if needed. --> Please check the type of change your PR introduces: - [x] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue Number: N/A ## Coderabbit Summary <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added messages for verification code requests, expiration, and mismatches. - Introduced functionality to resend verification codes. - Enhanced error handling for account confirmation and verification code processes. - **Improvements** - Updated UI to handle and display messages related to code resend status. - **Bug Fixes** - Improved error handling for code mismatches and expired codes during account confirmation. - **Configuration** - Added `build_in_source` parameter to Lambda function configurations. <!-- end of auto-generated comment: release notes by coderabbit.ai --> ## Does this introduce a breaking change? - [ ] Yes - [ ] No <!-- If this does introduce a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR, such as screenshots of how the component looks before and after the change. -->
- Loading branch information
Showing
12 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 38 additions & 22 deletions
60
packages/api/router/user/mutation.confirmAccount.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
import { confirmAccount as cognitoConfirmAccount } from '@weareinreach/auth/confirmAccount' | ||
import { TRPCError } from '@trpc/server' | ||
|
||
import { | ||
CodeMismatchException, | ||
confirmAccount as cognitoConfirmAccount, | ||
ExpiredCodeException, | ||
} from '@weareinreach/auth/confirmAccount' | ||
import { prisma } from '@weareinreach/db' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TConfirmAccountSchema } from './mutation.confirmAccount.schema' | ||
|
||
const confirmAccount = async ({ input }: TRPCHandlerParams<TConfirmAccountSchema>) => { | ||
const { code, email } = input | ||
const response = await cognitoConfirmAccount(email, code) | ||
try { | ||
const { code, email } = input | ||
const response = await cognitoConfirmAccount(email, code) | ||
|
||
const { id } = await prisma.user.findFirstOrThrow({ | ||
where: { | ||
email: { | ||
equals: email.toLowerCase(), | ||
mode: 'insensitive', | ||
const { id } = await prisma.user.findFirstOrThrow({ | ||
where: { | ||
email: { | ||
equals: email.toLowerCase(), | ||
mode: 'insensitive', | ||
}, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}) | ||
}) | ||
|
||
await prisma.user.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
emailVerified: new Date(), | ||
}, | ||
}) | ||
return response | ||
await prisma.user.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
emailVerified: new Date(), | ||
}, | ||
}) | ||
return response | ||
} catch (error) { | ||
if (error instanceof CodeMismatchException) { | ||
throw new TRPCError({ code: 'BAD_REQUEST', message: 'Code mismatch', cause: error }) | ||
} | ||
if (error instanceof ExpiredCodeException) { | ||
throw new TRPCError({ code: 'BAD_REQUEST', message: 'Code expired', cause: error }) | ||
} | ||
throw error | ||
} | ||
} | ||
export default confirmAccount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { resendVerificationCode } from '@weareinreach/auth/lib/resendCode' | ||
import { handleError } from '~api/lib/errorHandler' | ||
import { type TRPCHandlerParams } from '~api/types/handler' | ||
|
||
import { type TResendCodeSchema } from './mutation.resendCode.schema' | ||
|
||
const resendCode = async ({ input }: TRPCHandlerParams<TResendCodeSchema>) => { | ||
try { | ||
const result = await resendVerificationCode(input.email) | ||
return result | ||
} catch (error) { | ||
return handleError(error) | ||
} | ||
} | ||
export default resendCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { z } from 'zod' | ||
|
||
import { decodeUrl } from '~api/lib/encodeUrl' | ||
|
||
export const ZResendCodeSchema = z.union([ | ||
z.object({ email: z.string().email().toLowerCase(), data: z.never().optional() }), | ||
z | ||
.object({ data: z.string(), email: z.never().optional() }) | ||
.transform(({ data }) => ({ email: decodeUrl(data).email })), | ||
]) | ||
|
||
// .object({ email: z.string().email().toLowerCase() }) | ||
// .or(z.object({ data: z.string() })) | ||
export type TResendCodeSchema = z.infer<typeof ZResendCodeSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters