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'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 30 | import { ChangeEvent, FormEvent, MouseEvent, ReactNode, useEffect, useState } from 'react'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 31 | import { useTranslation } from 'react-i18next'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 32 | import { Form, 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'; |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 37 | import { checkPasswordStrength, isNameRegistered, loginUser, registerUser, setAccessToken } from '../utils/auth'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 38 | import { inputWidth, jamiUsernamePattern } from '../utils/constants'; |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 39 | import { InvalidCredentials, InvalidPassword, UsernameNotFound } from '../utils/errors'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 40 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 41 | type JamiRegistrationProps = { |
| 42 | login: () => void; |
| 43 | }; |
| 44 | |
| 45 | export default function JamiRegistration(props: JamiRegistrationProps) { |
| 46 | const theme: Theme = useTheme(); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 47 | const navigate = useNavigate(); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 48 | const { t } = useTranslation(); |
| 49 | |
| 50 | const [isCreatingUser, setIsCreatingUser] = useState(false); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 51 | |
| 52 | const [username, setUsername] = useState(''); |
| 53 | const [password, setPassword] = useState(''); |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 54 | const [isJams, setIsJams] = useState(false); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 55 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 56 | const [usernameStatus, setUsernameStatus] = useState<NameStatus>('default'); |
| 57 | const [passwordStatus, setPasswordStatus] = useState<PasswordStatus>('default'); |
| 58 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 59 | const [errorAlertContent, setErrorAlertContent] = useState<ReactNode>(undefined); |
| 60 | const [successAlertContent, setSuccessAlertContent] = useState<ReactNode>(undefined); |
| 61 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 62 | const usernameError = usernameStatus !== 'success' && usernameStatus !== 'default'; |
| 63 | const usernameSuccess = usernameStatus === 'success'; |
| 64 | const passwordError = passwordStatus !== 'strong' && passwordStatus !== 'default'; |
| 65 | const passwordSuccess = passwordStatus === 'strong'; |
| 66 | |
| 67 | useEffect(() => { |
| 68 | // 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] | 69 | if (username.length > 0 && usernameStatus === 'default') { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 70 | const validateUsername = async () => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 71 | if (await isNameRegistered(username)) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 72 | setUsernameStatus('taken'); |
| 73 | } else { |
| 74 | setUsernameStatus('success'); |
| 75 | } |
| 76 | }; |
| 77 | const timeout = setTimeout(validateUsername, 1000); |
| 78 | |
| 79 | return () => clearTimeout(timeout); |
| 80 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 81 | }, [username, usernameStatus]); |
| 82 | |
| 83 | const firstUserLogin = async () => { |
| 84 | try { |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 85 | const accessToken = await loginUser(username, password, isJams); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 86 | setAccessToken(accessToken); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 87 | navigate('/conversation', { replace: true }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 88 | } catch (e) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 89 | setIsCreatingUser(false); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 90 | if (e instanceof UsernameNotFound) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 91 | setErrorAlertContent(t('login_username_not_found')); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 92 | } else if (e instanceof InvalidPassword) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 93 | setErrorAlertContent(t('login_invalid_password')); |
| 94 | } else { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 95 | throw e; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | const createAccount = async () => { |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 101 | try { |
| 102 | await registerUser(username, password, isJams); |
| 103 | setSuccessAlertContent(t('registration_success')); |
| 104 | await firstUserLogin(); |
| 105 | } catch (e) { |
| 106 | setIsCreatingUser(false); |
| 107 | if (e instanceof UsernameNotFound) { |
| 108 | setErrorAlertContent(t('login_username_not_found')); |
| 109 | } else if (e instanceof InvalidPassword) { |
| 110 | setErrorAlertContent(t('login_invalid_password')); |
| 111 | } else if (e instanceof InvalidCredentials) { |
| 112 | setErrorAlertContent(t('login_invalid_credentials')); |
| 113 | } else { |
| 114 | throw e; |
| 115 | } |
| 116 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 117 | }; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 118 | |
| 119 | const handleUsername = async (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 120 | const usernameValue: string = event.target.value; |
| 121 | setUsername(usernameValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 122 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 123 | if (usernameValue.length > 0 && !jamiUsernamePattern.test(usernameValue)) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 124 | setUsernameStatus('invalid'); |
| 125 | } else { |
| 126 | setUsernameStatus('default'); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 131 | const passwordValue: string = event.target.value; |
| 132 | setPassword(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 133 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 134 | if (passwordValue.length > 0) { |
| 135 | const checkResult = checkPasswordStrength(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 136 | setPasswordStatus(checkResult.valueCode); |
| 137 | } else { |
| 138 | setPasswordStatus('default'); |
| 139 | } |
| 140 | }; |
| 141 | |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 142 | const handleIsJams = (event: ChangeEvent<HTMLInputElement>) => { |
| 143 | setIsJams(event.target.value === 'true'); |
| 144 | }; |
| 145 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 146 | const login = (event: MouseEvent<HTMLAnchorElement>) => { |
| 147 | event.preventDefault(); |
| 148 | props.login(); |
| 149 | }; |
| 150 | |
| 151 | const handleSubmit = async (event: FormEvent) => { |
| 152 | event.preventDefault(); |
| 153 | const canCreate = usernameSuccess && passwordSuccess; |
| 154 | |
| 155 | if (canCreate) { |
| 156 | setIsCreatingUser(true); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 157 | await createAccount(); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 158 | } else { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 159 | if (usernameError || username.length === 0) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 160 | setUsernameStatus('registration_failed'); |
| 161 | } |
| 162 | if (!passwordSuccess) { |
| 163 | setPasswordStatus('registration_failed'); |
| 164 | } |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
| 169 | |
| 170 | return ( |
| 171 | <> |
| 172 | <ProcessingRequest open={isCreatingUser} /> |
| 173 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 174 | <AlertSnackbar |
| 175 | severity={'success'} |
| 176 | open={!!successAlertContent} |
| 177 | onClose={() => setSuccessAlertContent(undefined)} |
| 178 | > |
| 179 | {successAlertContent} |
| 180 | </AlertSnackbar> |
| 181 | |
| 182 | <AlertSnackbar severity={'error'} open={!!errorAlertContent} onClose={() => setErrorAlertContent(undefined)}> |
| 183 | {errorAlertContent} |
| 184 | </AlertSnackbar> |
| 185 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 186 | <Stack |
| 187 | sx={{ |
| 188 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 189 | display: 'flex', |
| 190 | alignItems: 'center', |
| 191 | justifyContent: 'center', |
| 192 | }} |
| 193 | > |
| 194 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 195 | <Typography component={'span'} variant="h2"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 196 | {t('registration_form_title')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 197 | </Typography> |
| 198 | </Box> |
| 199 | |
| 200 | <Form method="post" id="register-form"> |
| 201 | <div> |
| 202 | <UsernameInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 203 | value={username} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 204 | onChange={handleUsername} |
| 205 | error={usernameError} |
| 206 | success={usernameSuccess} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 207 | status={usernameStatus} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 208 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 209 | tooltipTitle={t('registration_form_username_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 210 | /> |
| 211 | </div> |
| 212 | <div> |
| 213 | <PasswordInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 214 | value={password} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 215 | onChange={handlePassword} |
| 216 | error={passwordError} |
| 217 | success={passwordSuccess} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 218 | status={passwordStatus} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 219 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 220 | tooltipTitle={t('registration_form_password_tooltip')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 221 | /> |
| 222 | </div> |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 223 | <div> |
| 224 | <FormControl |
| 225 | sx={{ |
| 226 | width: theme.typography.pxToRem(inputWidth), |
| 227 | alignItems: 'center', |
| 228 | justifyContent: 'space-between', |
| 229 | }} |
| 230 | > |
| 231 | <RadioGroup row onChange={handleIsJams} value={isJams}> |
| 232 | <FormControlLabel value="false" control={<Radio />} label={t('jami')} /> |
| 233 | <FormControlLabel value="true" control={<Radio />} label={t('jams')} /> |
| 234 | </RadioGroup> |
| 235 | </FormControl> |
| 236 | </div> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 237 | |
| 238 | <Button |
| 239 | variant="contained" |
| 240 | type="submit" |
| 241 | onClick={handleSubmit} |
| 242 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 243 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 244 | {t('registration_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 245 | </Button> |
| 246 | </Form> |
| 247 | |
| 248 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 249 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 250 | {t('registration_form_to_login_text')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 251 | <a href="" onClick={login}> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 252 | {t('registration_form_to_login_link')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 253 | </a> |
| 254 | </Typography> |
| 255 | </Box> |
| 256 | </Stack> |
| 257 | </> |
| 258 | ); |
| 259 | } |