From 12184a5ef570593ab492c1499f2d7a23e3be2569 Mon Sep 17 00:00:00 2001 From: zirops Date: Tue, 26 Jan 2021 10:57:25 +0100 Subject: user self registration --- modern/src/LoginPage.js | 29 +++++++++++- modern/src/RegisterDialog.js | 109 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 modern/src/RegisterDialog.js (limited to 'modern') diff --git a/modern/src/LoginPage.js b/modern/src/LoginPage.js index 2636c80..cbe50fe 100644 --- a/modern/src/LoginPage.js +++ b/modern/src/LoginPage.js @@ -7,6 +7,9 @@ import FormControl from '@material-ui/core/FormControl'; import Paper from '@material-ui/core/Paper'; import { makeStyles } from '@material-ui/core'; import TextField from '@material-ui/core/TextField'; +import RegisterDialog from './RegisterDialog'; +import Snackbar from '@material-ui/core/Snackbar'; +import { useSelector } from 'react-redux'; import t from './common/localization'; @@ -47,10 +50,19 @@ const LoginPage = () => { const [failed, setFailed] = useState(false); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); + const [registerDialogShown, setRegisterDialogShown] = useState(false); + const [snackbarOpen, setSnackbarOpen] = useState(false); const classes = useStyles(); const history = useHistory(); + const server = useSelector(state => state.session.server); + + let registrationPref = false; + if (server) { + registrationPref = server['registration'] + } + const handleEmailChange = (event) => { setEmail(event.target.value); } @@ -60,7 +72,14 @@ const LoginPage = () => { } const handleRegister = () => { - // TODO: Implement registration + setRegisterDialogShown(true); + } + + const handleRegisterResult = (resultOk) => { + setRegisterDialogShown(false); + if (resultOk) { + setSnackbarOpen(true); + } } const handleLogin = async (event) => { @@ -109,7 +128,7 @@ const LoginPage = () => {
-
+ {registerDialogShown && } + {setSnackbarOpen(false);}} message={t('loginCreated')} + /> ); diff --git a/modern/src/RegisterDialog.js b/modern/src/RegisterDialog.js new file mode 100644 index 0000000..ecd07ac --- /dev/null +++ b/modern/src/RegisterDialog.js @@ -0,0 +1,109 @@ +import t from './common/localization' +import React, { useState } from 'react'; +import Button from '@material-ui/core/Button'; +import Dialog from '@material-ui/core/Dialog'; +import DialogActions from '@material-ui/core/DialogActions'; +import DialogContent from '@material-ui/core/DialogContent'; +import DialogContentText from '@material-ui/core/DialogContentText'; +import TextField from '@material-ui/core/TextField'; +import Alert from '@material-ui/lab/Alert'; + +const RegisterDialog = ({ open, onResult }) => { + const [formFields, setFormFields] = useState({}); + const [validationErrors, setValidationErrors] = useState({}); + const [errorResponse, setErrorResponse] = useState(null); + + const handleChange = (event) => { + setFormFields({ ...formFields, [event.target.name]: event.target.value }); + setValidationErrors({ ...validationErrors, [event.target.name]: false }); + } + + const handleRegister = async (event) => { + event.preventDefault(); + setErrorResponse(null); + let objErrors = {}; + if (formFields.name.trim() === '') { + objErrors.name = true; + } + if (! + /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(formFields.email)) { + objErrors.email = true; + } + if (formFields.password.trim() === '') { + objErrors.password = true; + } + if (Object.keys(objErrors).length !== 0) { + setValidationErrors(objErrors); + return; + } + + const response = await fetch('/api/users', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(formFields) + }); + + if (response.ok) { + onResult(true) + } else { + setErrorResponse(t('errorGeneral')); + } + } + + return ( + { onResult(false) }}> + + {t('loginRegister')} + + {errorResponse && {errorResponse}} + + + + + + + + + + + + + + ); +}; + +export default RegisterDialog; -- cgit v1.2.3