|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import * as React from 'react'; |
| 4 | +import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutline'; |
| 5 | +import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline'; |
| 6 | +import { |
| 7 | + Alert, |
| 8 | + CircularProgress, |
| 9 | + Stack, |
| 10 | + Typography, |
| 11 | + useTheme, |
| 12 | +} from '@mui/material'; |
| 13 | +import { useTranslations } from 'next-intl'; |
| 14 | +import { app } from '../../../firebase'; |
| 15 | +import { ContentBox } from '../../components/ContentBox'; |
| 16 | + |
| 17 | +type VerificationStatus = 'loading' | 'success' | 'error'; |
| 18 | + |
| 19 | +interface EmailVerificationContentProps { |
| 20 | + mode?: string; |
| 21 | + oobCode?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export default function EmailVerificationContent({ |
| 25 | + mode, |
| 26 | + oobCode, |
| 27 | +}: EmailVerificationContentProps): React.ReactElement { |
| 28 | + const t = useTranslations('emailVerification'); |
| 29 | + const theme = useTheme(); |
| 30 | + const [status, setStatus] = React.useState<VerificationStatus>('loading'); |
| 31 | + const [errorMessage, setErrorMessage] = React.useState<string | null>(null); |
| 32 | + |
| 33 | + React.useEffect(() => { |
| 34 | + const verifyEmail = async (): Promise<void> => { |
| 35 | + if (mode !== 'verifyEmail' || oobCode == null || oobCode.length === 0) { |
| 36 | + setStatus('error'); |
| 37 | + setErrorMessage(t('invalidLink')); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + await app.auth().applyActionCode(oobCode); |
| 43 | + |
| 44 | + setStatus('success'); |
| 45 | + setErrorMessage(null); |
| 46 | + } catch { |
| 47 | + setStatus('error'); |
| 48 | + setErrorMessage(t('verificationFailed')); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + void verifyEmail(); |
| 53 | + }, [mode, oobCode]); |
| 54 | + |
| 55 | + return ( |
| 56 | + <ContentBox |
| 57 | + title='' |
| 58 | + sx={{ |
| 59 | + display: 'flex', |
| 60 | + justifyContent: 'center', |
| 61 | + backgroundColor: theme.palette.background.paper, |
| 62 | + maxWidth: theme.breakpoints.values.sm, |
| 63 | + mx: 'auto', |
| 64 | + mt: 6, |
| 65 | + }} |
| 66 | + > |
| 67 | + <Stack spacing={3} alignItems='center' textAlign='center'> |
| 68 | + {status === 'loading' ? ( |
| 69 | + <CircularProgress aria-label={t('loadingTitle')} /> |
| 70 | + ) : status === 'success' ? ( |
| 71 | + <CheckCircleOutlineIcon color='success' sx={{ fontSize: 56 }} /> |
| 72 | + ) : ( |
| 73 | + <ErrorOutlineIcon color='error' sx={{ fontSize: 56 }} /> |
| 74 | + )} |
| 75 | + |
| 76 | + <Stack spacing={1.5}> |
| 77 | + <Typography variant='h4' component='h1' sx={{ fontWeight: 700 }}> |
| 78 | + {status === 'loading' |
| 79 | + ? t('loadingTitle') |
| 80 | + : status === 'success' |
| 81 | + ? t('successTitle') |
| 82 | + : t('errorTitle')} |
| 83 | + </Typography> |
| 84 | + <Typography variant='body1' color='text.secondary'> |
| 85 | + {status === 'loading' |
| 86 | + ? t('loadingDescription') |
| 87 | + : status === 'success' |
| 88 | + ? t('successDescription') |
| 89 | + : t('errorDescription')} |
| 90 | + </Typography> |
| 91 | + </Stack> |
| 92 | + |
| 93 | + {errorMessage != null && ( |
| 94 | + <Alert severity='error' variant='outlined' sx={{ width: '100%' }}> |
| 95 | + {errorMessage} |
| 96 | + </Alert> |
| 97 | + )} |
| 98 | + </Stack> |
| 99 | + </ContentBox> |
| 100 | + ); |
| 101 | +} |
0 commit comments