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 | */ |
| 18 | import { Box, Button, Stack, Typography, useMediaQuery } from '@mui/material'; |
| 19 | import { Theme, useTheme } from '@mui/material/styles'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 20 | import { ChangeEvent, FormEvent, MouseEvent, ReactNode, useState } from 'react'; |
| 21 | import { useTranslation } from 'react-i18next'; |
| 22 | import { Form, useNavigate } from 'react-router-dom'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 23 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 24 | import { AlertSnackbar } from '../components/AlertSnackbar'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 25 | import { PasswordInput, UsernameInput } from '../components/Input'; |
| 26 | import ProcessingRequest from '../components/ProcessingRequest'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 27 | import { loginUser, setAccessToken } from '../utils/auth'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 28 | import { inputWidth } from '../utils/constants'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 29 | import { InvalidPassword, UsernameNotFound } from '../utils/errors'; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 30 | |
| 31 | type JamiLoginProps = { |
| 32 | register: () => void; |
| 33 | }; |
| 34 | |
| 35 | export default function JamiLogin(props: JamiLoginProps) { |
| 36 | const theme: Theme = useTheme(); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 37 | const navigate = useNavigate(); |
| 38 | const { t } = useTranslation(); |
| 39 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 40 | const [username, setUsername] = useState<string>(''); |
| 41 | const [password, setPassword] = useState<string>(''); |
| 42 | const [isLoggingInUser, setIsLoggingInUser] = useState<boolean>(false); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 43 | const [errorAlertContent, setErrorAlertContent] = useState<ReactNode>(undefined); |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 44 | |
| 45 | const handleUsername = (event: ChangeEvent<HTMLInputElement>) => { |
| 46 | setUsername(event.target.value); |
| 47 | }; |
| 48 | |
| 49 | const handlePassword = (event: ChangeEvent<HTMLInputElement>) => { |
| 50 | setPassword(event.target.value); |
| 51 | }; |
| 52 | |
| 53 | const register = (event: MouseEvent<HTMLAnchorElement>) => { |
| 54 | event.preventDefault(); |
| 55 | props.register(); |
| 56 | }; |
| 57 | |
| 58 | const authenticateUser = async (event: FormEvent) => { |
| 59 | event.preventDefault(); |
| 60 | if (username.length > 0 && password.length > 0) { |
| 61 | setIsLoggingInUser(true); |
| 62 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 63 | try { |
| 64 | const accessToken = await loginUser(username, password); |
| 65 | setAccessToken(accessToken); |
| 66 | navigate('/settings', { replace: true }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 67 | } catch (e) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 68 | setIsLoggingInUser(false); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 69 | if (e instanceof UsernameNotFound) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 70 | setErrorAlertContent(t('login_username_not_found')); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 71 | } else if (e instanceof InvalidPassword) { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 72 | setErrorAlertContent(t('login_invalid_password')); |
| 73 | } else { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 74 | throw e; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 77 | } |
| 78 | }; |
| 79 | |
| 80 | const isMobile: boolean = useMediaQuery(theme.breakpoints.only('xs')); |
| 81 | |
| 82 | return ( |
| 83 | <> |
| 84 | <ProcessingRequest open={isLoggingInUser} /> |
| 85 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 86 | <AlertSnackbar severity={'error'} open={!!errorAlertContent} onClose={() => setErrorAlertContent(undefined)}> |
| 87 | {errorAlertContent} |
| 88 | </AlertSnackbar> |
| 89 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 90 | <Stack |
| 91 | sx={{ |
| 92 | minHeight: `${isMobile ? 'auto' : '100%'}`, |
| 93 | display: 'flex', |
| 94 | alignItems: 'center', |
| 95 | justifyContent: 'center', |
| 96 | }} |
| 97 | > |
| 98 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(20) }}> |
| 99 | <Typography component={'span'} variant="h2"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 100 | {t('login_form_title')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 101 | </Typography> |
| 102 | </Box> |
| 103 | |
| 104 | <Form method="post" id="login-form"> |
| 105 | <div> |
| 106 | <UsernameInput |
| 107 | onChange={handleUsername} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 108 | tooltipTitle={t('login_form_username_tooltip')} |
Michelle Sepkap Sime | 45e7e8c | 2022-11-12 22:52:54 -0500 | [diff] [blame] | 109 | status={'default'} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 110 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 111 | /> |
| 112 | </div> |
| 113 | <div> |
| 114 | <PasswordInput |
| 115 | onChange={handlePassword} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 116 | tooltipTitle={t('login_form_password_tooltip')} |
Michelle Sepkap Sime | 45e7e8c | 2022-11-12 22:52:54 -0500 | [diff] [blame] | 117 | status={'default'} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 118 | sx={{ width: theme.typography.pxToRem(inputWidth) }} |
| 119 | /> |
| 120 | </div> |
| 121 | |
| 122 | <Button |
| 123 | variant="contained" |
| 124 | type="submit" |
| 125 | onClick={authenticateUser} |
| 126 | sx={{ width: theme.typography.pxToRem(inputWidth), mt: theme.typography.pxToRem(20) }} |
| 127 | > |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 128 | {t('login_form_submit_button')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 129 | </Button> |
| 130 | </Form> |
| 131 | |
| 132 | <Box sx={{ mt: theme.typography.pxToRem(50), mb: theme.typography.pxToRem(50) }}> |
| 133 | <Typography variant="body1"> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 134 | {t('login_form_to_registration_text')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 135 | <a href="" onClick={register}> |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 136 | {t('login_form_to_registration_link')} |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 137 | </a> |
| 138 | </Typography> |
| 139 | </Box> |
| 140 | </Stack> |
| 141 | </> |
| 142 | ); |
| 143 | } |