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 | */ |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 18 | import { Box, Button, Stack, Typography, useMediaQuery } from '@mui/material'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 19 | import { Theme, useTheme } from '@mui/material/styles'; |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 20 | import { HttpStatusCode } from 'jami-web-common'; |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 21 | import { ChangeEvent, FormEvent, useEffect, useState } from 'react'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 22 | import { useTranslation } from 'react-i18next'; |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 23 | import { Form, Link } from 'react-router-dom'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 24 | |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 25 | import { NameStatus, PasswordInput, PasswordStatus, UsernameInput } from '../components/Input'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 26 | import ProcessingRequest from '../components/ProcessingRequest'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 27 | import withAuthUI from '../components/WithAuthUI'; |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 28 | import { |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 29 | checkPasswordStrength, |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 30 | useCheckIfUsernameIsRegisteredQuery, |
| 31 | useLoginMutation, |
| 32 | useRegisterMutation, |
| 33 | } from '../services/authQueries'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 34 | import { inputWidth, jamiUsernamePattern } from '../utils/constants'; |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 35 | function RegistrationForm() { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 36 | const theme: Theme = useTheme(); |
| 37 | const { t } = useTranslation(); |
| 38 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 39 | const [username, setUsername] = useState<string>(''); |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 40 | const [debouncedUsername, setDebouncedUsername] = useState<string>(''); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 41 | const [password, setPassword] = useState<string>(''); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 42 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 43 | const [usernameStatus, setUsernameStatus] = useState<NameStatus>('default'); |
| 44 | const [passwordStatus, setPasswordStatus] = useState<PasswordStatus>('default'); |
| 45 | |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 46 | const registerMutation = useRegisterMutation(); |
| 47 | const loginMutation = useLoginMutation(); |
| 48 | |
| 49 | const { isLoading: isRegisterLoading } = registerMutation; |
| 50 | const { isLoading: isLoginLoading } = loginMutation; |
| 51 | |
| 52 | const isLoading = isRegisterLoading || isLoginLoading; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 53 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 54 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 55 | |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 56 | const { data: response, isError } = useCheckIfUsernameIsRegisteredQuery(debouncedUsername); |
| 57 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 58 | useEffect(() => { |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 59 | if (response !== undefined) { |
| 60 | const { responseMessage } = response; |
| 61 | setUsernameStatus(responseMessage); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 62 | } |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 63 | if (isError) { |
| 64 | setUsernameStatus('invalid'); |
| 65 | } |
| 66 | }, [response, isError]); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 67 | |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 68 | useEffect(() => { |
| 69 | const timeoutId = setTimeout(() => { |
| 70 | setDebouncedUsername(username); |
| 71 | }, 500); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 72 | return () => { |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 73 | clearTimeout(timeoutId); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 74 | }; |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 75 | }, [username]); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 76 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 77 | const createAccount = () => { |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 78 | registerMutation.mutate( |
| 79 | { username, password }, |
| 80 | { |
| 81 | onSuccess: (response) => { |
| 82 | if (response.status === HttpStatusCode.Created) { |
| 83 | loginMutation.mutate({ username, password, isJams: false }); |
| 84 | } |
| 85 | }, |
| 86 | } |
| 87 | ); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 88 | }; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 89 | |
| 90 | const handleUsername = async (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 91 | const usernameValue: string = event.target.value; |
| 92 | setUsername(usernameValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 93 | |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 94 | if (usernameValue === '') { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 95 | setUsernameStatus('default'); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 96 | return; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 97 | } |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 98 | |
| 99 | if (!jamiUsernamePattern.test(usernameValue)) { |
| 100 | setUsernameStatus('invalid'); |
| 101 | return; |
| 102 | } |
| 103 | //The valid state is when the username passes the Regex test and is ready for availability check. |
| 104 | setUsernameStatus('valid'); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 108 | const passwordValue: string = event.target.value; |
| 109 | setPassword(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 110 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 111 | if (passwordValue.length > 0) { |
| 112 | const checkResult = checkPasswordStrength(passwordValue); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 113 | setPasswordStatus(checkResult.valueCode); |
| 114 | } else { |
| 115 | setPasswordStatus('default'); |
| 116 | } |
| 117 | }; |
| 118 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 119 | const handleSubmit = async (event: FormEvent) => { |
| 120 | event.preventDefault(); |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 121 | const usernameOk = usernameStatus === 'success'; |
| 122 | const passwordOk = passwordStatus === 'strong'; |
| 123 | |
| 124 | const canCreate = usernameOk && passwordOk; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 125 | |
| 126 | if (canCreate) { |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 127 | createAccount(); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 128 | } else { |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 129 | if (!usernameOk) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 130 | setUsernameStatus('registration_failed'); |
| 131 | } |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 132 | if (!passwordOk) { |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 133 | setPasswordStatus('registration_failed'); |
| 134 | } |
| 135 | } |
| 136 | }; |
| 137 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 138 | return ( |
| 139 | <> |
Ziwei Wang | 45baf53 | 2023-02-16 15:41:49 -0500 | [diff] [blame^] | 140 | <ProcessingRequest open={isLoading} /> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 141 | |
| 142 | <Stack |
| 143 | sx={{ |
| 144 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 145 | display: 'flex', |
| 146 | alignItems: 'center', |
| 147 | justifyContent: 'center', |
| 148 | }} |
| 149 | > |
| 150 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 151 | <Typography component={'span'} variant="h2"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 152 | {t('registration_form_title')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 153 | </Typography> |
| 154 | </Box> |
| 155 | |
| 156 | <Form method="post" id="register-form"> |
| 157 | <div> |
| 158 | <UsernameInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 159 | value={username} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 160 | onChange={handleUsername} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 161 | status={usernameStatus} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 162 | tooltipTitle={t('registration_form_username_tooltip')} |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 163 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 164 | /> |
| 165 | </div> |
| 166 | <div> |
| 167 | <PasswordInput |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 168 | value={password} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 169 | onChange={handlePassword} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 170 | status={passwordStatus} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 171 | tooltipTitle={t('registration_form_password_tooltip')} |
Ziwei Wang | 9b4e2c1 | 2023-02-06 14:45:37 -0500 | [diff] [blame] | 172 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 173 | /> |
| 174 | </div> |
| 175 | |
| 176 | <Button |
| 177 | variant="contained" |
| 178 | type="submit" |
| 179 | onClick={handleSubmit} |
| 180 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 181 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 182 | {t('registration_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 183 | </Button> |
| 184 | </Form> |
| 185 | |
| 186 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 187 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 188 | {t('registration_form_to_login_text')} |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 189 | <Link to={'/login'}>{t('registration_form_to_login_link')}</Link> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 190 | </Typography> |
| 191 | </Box> |
| 192 | </Stack> |
| 193 | </> |
| 194 | ); |
| 195 | } |
Ziwei Wang | 3ce1ac0 | 2023-02-03 11:59:03 -0500 | [diff] [blame] | 196 | |
| 197 | export default withAuthUI(RegistrationForm); |