|
1 | | -import { AppwriteException, type Models } from '@appwrite.io/console'; |
| 1 | +import { AppwriteException } from '@appwrite.io/console'; |
2 | 2 |
|
3 | | -const EMAIL_VERIFICATION_REQUIRED_ERROR_TYPE = 'user_email_not_verified'; |
4 | | -const CONSOLE_ACCOUNT_VERIFICATION_REQUIRED_TYPE = 'console_account_verification_required'; |
5 | | - |
6 | | -export function isEmailVerificationEnabled( |
7 | | - consoleVariables: Models.ConsoleVariables | undefined |
8 | | -): boolean { |
9 | | - if (!consoleVariables) { |
10 | | - return false; |
11 | | - } |
12 | | - |
13 | | - return String(consoleVariables._APP_CONSOLE_EMAIL_VERIFICATION) === 'true'; |
14 | | -} |
15 | | - |
16 | | -export function isEmailVerificationRequiredError(type: string | undefined): boolean { |
17 | | - return type === EMAIL_VERIFICATION_REQUIRED_ERROR_TYPE; |
18 | | -} |
19 | | - |
20 | | -function matchesVerifyEmailGate(type: string | undefined, message: string | undefined): boolean { |
21 | | - if (isEmailVerificationRequiredError(type)) return true; |
22 | | - if (type === CONSOLE_ACCOUNT_VERIFICATION_REQUIRED_TYPE) return true; |
23 | | - if (message?.includes('Console account verification is required')) return true; |
24 | | - return false; |
25 | | -} |
26 | | - |
27 | | -/** |
28 | | - * Console APIs may return `user_email_not_verified`, `console_account_verification_required`, |
29 | | - * or a message-only error when email verification is enforced for the console account. |
30 | | - */ |
| 3 | +/** True when access is blocked until the console account email is verified. */ |
31 | 4 | export function isVerifyEmailRedirectError(error: unknown): boolean { |
32 | 5 | if (error instanceof AppwriteException) { |
33 | | - return matchesVerifyEmailGate(error.type, error.message); |
| 6 | + return ( |
| 7 | + error.type === 'user_email_not_verified' || |
| 8 | + error.type === 'console_account_verification_required' || |
| 9 | + (error.message?.includes('Console account verification is required') ?? false) |
| 10 | + ); |
34 | 11 | } |
35 | | - if (typeof error === 'object' && error !== null && 'message' in error) { |
| 12 | + |
| 13 | + if (error && typeof error === 'object' && 'message' in error) { |
36 | 14 | const msg = (error as { message: unknown }).message; |
37 | 15 | if (typeof msg !== 'string') return false; |
38 | 16 | const typ = |
39 | 17 | 'type' in error && typeof (error as { type: unknown }).type === 'string' |
40 | 18 | ? (error as { type: string }).type |
41 | 19 | : undefined; |
42 | | - return matchesVerifyEmailGate(typ, msg); |
| 20 | + return ( |
| 21 | + typ === 'user_email_not_verified' || |
| 22 | + typ === 'console_account_verification_required' || |
| 23 | + msg.includes('Console account verification is required') |
| 24 | + ); |
43 | 25 | } |
| 26 | + |
44 | 27 | return false; |
45 | 28 | } |
0 commit comments