import axios from 'axios'; import { validate as validateEmail } from 'email-validator'; import { FormEvent, useContext } from 'react'; import { NavBar } from '../components/navbar'; import { CenteredPage } from '../components/page'; import { ToastContext, toastType } from '../components/toast'; import { Button, Input, Vierkant } from '../components/ui'; import ErrorOutlineIcon from '@material-ui/icons/ErrorOutline'; import ReportProblemOutlinedIcon from '@material-ui/icons/ReportProblemOutlined'; function submitRegister(event?: FormEvent, toast?: toastType) { event?.preventDefault(); var formData = { username: (document.getElementById('username') as HTMLInputElement).value, email: (document.getElementById('email') as HTMLInputElement).value, password: (document.getElementById('password') as HTMLInputElement).value, }; if ( !formData.username || !formData.email || !formData.password ) { toast({ message: 'Vul alsjeblieft alle velden in!', type: 'error', icon: }); return; } var passwordRegex: RegExp = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$/; /* * ^ Begin string * (?=.*[A-Z]) Minimaal één hoofdletter * (?=.*[a-z]) Minimaal één kleine letter * (?=.*[0-9]) Minimaal één getal * .{8,} Minimaal acht tekens in lengte * $ Ende string * https://stackoverflow.com/questions/5142103/regex-to-validate-password-strength */ if (formData.username.length < 3 || formData.username.length > 35) { toast({ message: 'Ongeldige gebruikersnaam', description: 'Je gebruikersnaam moet tussen de 3 en 35 letters zijn', type: 'error', icon: , }); return; } if (!validateEmail(formData.email)) { toast({ message: 'Ongeldig email-adres', type: 'error', icon: }); return; } // TODO: wachtwoord max 72 tekens ivm bcrypt if (!formData.password.match(passwordRegex)) { toast({ message: 'Ongeldig wachtwoord', description: 'Je wachtwoord moet een hoofdletter, kleine letter en een getal bevatten', type: 'error', icon: , }); return; } axios({ method: 'post', url: `${window.location.origin}/api/auth/signup`, headers: { 'content-type': 'application/json' }, data: formData, }) .then(() => { // TODO: email verificatie // redirect naar home, automatische login window.location.pathname = '/'; }) .catch(error => { toast({ message: 'Er is iets fout gegaan', type: 'error', icon: }); console.log(error); }); } export default function RegisterPage() { var { toast } = useContext(ToastContext); return (
submitRegister(e, toast)}>
); }