Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 18 | import { |
| 19 | Box, |
| 20 | Button, |
| 21 | FormControl, |
| 22 | FormControlLabel, |
| 23 | Radio, |
| 24 | RadioGroup, |
| 25 | Stack, |
| 26 | Typography, |
| 27 | useMediaQuery, |
| 28 | } from '@mui/material'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 29 | import { Theme, useTheme } from '@mui/material/styles'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 30 | import { ChangeEvent, FormEvent, ReactNode, useEffect, useState } from 'react'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 31 | import { useTranslation } from 'react-i18next'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 32 | import { Form, Link, useNavigate } from 'react-router-dom'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 33 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 34 | import { AlertSnackbar } from '../components/AlertSnackbar'; |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 35 | import { NameStatus, PasswordInput, PasswordStatus, UsernameInput } from '../components/Input'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 36 | import ProcessingRequest from '../components/ProcessingRequest'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 37 | import withAuthUI from '../components/WithAuthUI'; |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 38 | import { checkPasswordStrength, isNameRegistered, loginUser, registerUser, setAccessToken } from '../utils/auth'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 39 | import { inputWidth, jamiUsernamePattern } from '../utils/constants'; |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 40 | import { InvalidCredentials, InvalidPassword, UsernameNotFound } from '../utils/errors'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 41 | |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 42 | function RegistrationForm() { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 43 | const theme: Theme = useTheme(); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 44 | const navigate = useNavigate(); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 45 | const { t } = useTranslation(); |
| 46 | |
| 47 | const [isCreatingUser, setIsCreatingUser] = useState(false); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 48 | |
| 49 | const [username, setUsername] = useState(''); |
| 50 | const [password, setPassword] = useState(''); |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 51 | const [isJams, setIsJams] = useState(false); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 52 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 53 | const [usernameStatus, setUsernameStatus] = useState<NameStatus>('default'); |
| 54 | const [passwordStatus, setPasswordStatus] = useState<PasswordStatus>('default'); |
| 55 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 56 | const [errorAlertContent, setErrorAlertContent] = useState<ReactNode>(undefined); |
| 57 | const [successAlertContent, setSuccessAlertContent] = useState<ReactNode>(undefined); |
| 58 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 59 | const usernameError = usernameStatus !== 'success' && usernameStatus !== 'default'; |
| 60 | const usernameSuccess = usernameStatus === 'success'; |
| 61 | const passwordError = passwordStatus !== 'strong' && passwordStatus !== 'default'; |
| 62 | const passwordSuccess = passwordStatus === 'strong'; |
| 63 | |
| 64 | useEffect(() => { |
| 65 | // To prevent lookup if field is empty, in error state or lookup already done |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 66 | if (username.length > 0 && usernameStatus === 'default') { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 67 | const validateUsername = async () => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 68 | if (await isNameRegistered(username)) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 69 | setUsernameStatus('taken'); |
| 70 | } else { |
| 71 | setUsernameStatus('success'); |
| 72 | } |
| 73 | }; |
| 74 | const timeout = setTimeout(validateUsername, 1000); |
| 75 | |
| 76 | return () => clearTimeout(timeout); |
| 77 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 78 | }, [username, usernameStatus]); |
| 79 | |
| 80 | const firstUserLogin = async () => { |
| 81 | try { |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 82 | const accessToken = await loginUser(username, password, isJams); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 83 | setAccessToken(accessToken); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 84 | navigate('/conversation', { replace: true }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 85 | } catch (e) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 86 | setIsCreatingUser(false); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 87 | if (e instanceof UsernameNotFound) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 88 | setErrorAlertContent(t('login_username_not_found')); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 89 | } else if (e instanceof InvalidPassword) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 90 | setErrorAlertContent(t('login_invalid_password')); |
| 91 | } else { |
Ziwei Wang | ce81edd | 2023-02-03 13:43:22 -0500 | [diff] [blame^] | 92 | setErrorAlertContent(t('unknown_error_alert')); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | const createAccount = async () => { |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 98 | try { |
| 99 | await registerUser(username, password, isJams); |
| 100 | setSuccessAlertContent(t('registration_success')); |
| 101 | await firstUserLogin(); |
| 102 | } catch (e) { |
| 103 | setIsCreatingUser(false); |
| 104 | if (e instanceof UsernameNotFound) { |
| 105 | setErrorAlertContent(t('login_username_not_found')); |
| 106 | } else if (e instanceof InvalidPassword) { |
| 107 | setErrorAlertContent(t('login_invalid_password')); |
| 108 | } else if (e instanceof InvalidCredentials) { |
| 109 | setErrorAlertContent(t('login_invalid_credentials')); |
| 110 | } else { |
Ziwei Wang | ce81edd | 2023-02-03 13:43:22 -0500 | [diff] [blame^] | 111 | setErrorAlertContent(t('unknown_error_alert')); |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 112 | } |
| 113 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 114 | }; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 115 | |
| 116 | const handleUsername = async (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 117 | const usernameValue: string = event.target.value; |
| 118 | setUsername(usernameValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 119 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 120 | if (usernameValue.length > 0 && !jamiUsernamePattern.test(usernameValue)) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 121 | setUsernameStatus('invalid'); |
| 122 | } else { |
| 123 | setUsernameStatus('default'); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 128 | const passwordValue: string = event.target.value; |
| 129 | setPassword(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 130 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 131 | if (passwordValue.length > 0) { |
| 132 | const checkResult = checkPasswordStrength(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 133 | setPasswordStatus(checkResult.valueCode); |
| 134 | } else { |
| 135 | setPasswordStatus('default'); |
| 136 | } |
| 137 | }; |
| 138 | |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 139 | const handleIsJams = (event: ChangeEvent<HTMLInputElement>) => { |
| 140 | setIsJams(event.target.value === 'true'); |
| 141 | }; |
| 142 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 143 | const handleSubmit = async (event: FormEvent) => { |
| 144 | event.preventDefault(); |
| 145 | const canCreate = usernameSuccess && passwordSuccess; |
| 146 | |
| 147 | if (canCreate) { |
| 148 | setIsCreatingUser(true); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 149 | await createAccount(); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 150 | } else { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 151 | if (usernameError || username.length === 0) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 152 | setUsernameStatus('registration_failed'); |
| 153 | } |
| 154 | if (!passwordSuccess) { |
| 155 | setPasswordStatus('registration_failed'); |
| 156 | } |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
| 161 | |
| 162 | return ( |
| 163 | <> |
| 164 | <ProcessingRequest open={isCreatingUser} /> |
| 165 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 166 | <AlertSnackbar |
| 167 | severity={'success'} |
| 168 | open={!!successAlertContent} |
| 169 | onClose={() => setSuccessAlertContent(undefined)} |
| 170 | > |
| 171 | {successAlertContent} |
| 172 | </AlertSnackbar> |
| 173 | |
| 174 | <AlertSnackbar severity={'error'} open={!!errorAlertContent} onClose={() => setErrorAlertContent(undefined)}> |
| 175 | {errorAlertContent} |
| 176 | </AlertSnackbar> |
| 177 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 178 | <Stack |
| 179 | sx={{ |
| 180 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 181 | display: 'flex', |
| 182 | alignItems: 'center', |
| 183 | justifyContent: 'center', |
| 184 | }} |
| 185 | > |
| 186 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 187 | <Typography component={'span'} variant="h2"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 188 | {t('registration_form_title')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 189 | </Typography> |
| 190 | </Box> |
| 191 | |
| 192 | <Form method="post" id="register-form"> |
| 193 | <div> |
| 194 | <UsernameInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 195 | value={username} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 196 | onChange={handleUsername} |
| 197 | error={usernameError} |
| 198 | success={usernameSuccess} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 199 | status={usernameStatus} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 200 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 201 | tooltipTitle={t('registration_form_username_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 202 | /> |
| 203 | </div> |
| 204 | <div> |
| 205 | <PasswordInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 206 | value={password} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 207 | onChange={handlePassword} |
| 208 | error={passwordError} |
| 209 | success={passwordSuccess} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 210 | status={passwordStatus} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 211 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 212 | tooltipTitle={t('registration_form_password_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 213 | /> |
| 214 | </div> |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 215 | <div> |
| 216 | <FormControl |
| 217 | sx={{ |
| 218 | width: theme.typography.pxToRem(inputWidth), |
| 219 | alignItems: 'center', |
| 220 | justifyContent: 'space-between', |
| 221 | }} |
| 222 | > |
| 223 | <RadioGroup row onChange={handleIsJams} value={isJams}> |
| 224 | <FormControlLabel value="false" control={<Radio />} label={t('jami')} /> |
| 225 | <FormControlLabel value="true" control={<Radio />} label={t('jams')} /> |
| 226 | </RadioGroup> |
| 227 | </FormControl> |
| 228 | </div> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 229 | |
| 230 | <Button |
| 231 | variant="contained" |
| 232 | type="submit" |
| 233 | onClick={handleSubmit} |
| 234 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 235 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 236 | {t('registration_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 237 | </Button> |
| 238 | </Form> |
| 239 | |
| 240 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 241 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 242 | {t('registration_form_to_login_text')} |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 243 | <Link to={'/login'}>{t('registration_form_to_login_link')}</Link> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 244 | </Typography> |
| 245 | </Box> |
| 246 | </Stack> |
| 247 | </> |
| 248 | ); |
| 249 | } |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 250 | |
| 251 | export default withAuthUI(RegistrationForm); |