import { useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { useForm, hasLength, matchesField } from '@mantine/form' import { Center, Container, Paper, Title, Text, Anchor, TextInput, PasswordInput, Stack, Button } from '@mantine/core' import { useAppSelector } from '../app/hooks' import { useRegisterUserMutation } from '../app/api/users' type RegistrationFormValues = { username: string; password: string; confPassword: string; } const SignUpPage = () => { const navigate = useNavigate() // Ensure the user isn't authenticated already const currentUser = useAppSelector((state) => state.auth.currentUser) if (currentUser) { throw new Error('You are already authenticated.') } const [errorMsg, setErrorMsg] = useState('') const registrationForm = useForm({ initialValues: { username: '', password: '', confPassword: '', }, validate: { username: hasLength({ min: 3, max: 32 }, 'Username must be between 3 and 32 characters'), password: hasLength({ min: 8 }, 'Password must have a minimum of 8 characters'), confPassword: matchesField('password', 'Confirmation password does not match'), } }) const [registerUser, { isLoading }] = useRegisterUserMutation() const submitRegistrationForm = async (values: RegistrationFormValues) => { await registerUser({ username: values.username, password: values.password }).unwrap().then(() => { // Redirect to homepage. navigate('/') }).catch((error) => { if (!error.data) { setErrorMsg('Failed to access the API') } else { setErrorMsg(error.data.msg) } }) } return (
Sign Up Already have an account?{' '} Sign in instead.
{ errorMsg && {'Error: ' + errorMsg} }
) } export default SignUpPage