Updated users schema

This commit is contained in:
Benjamin Sherriff
2023-10-18 23:10:54 -04:00
parent 0bda10c23b
commit 55b8b1a0e3
5 changed files with 81 additions and 36 deletions

View File

@@ -4,5 +4,5 @@ CREATE TABLE IF NOT EXISTS users (
role TEXT NOT NULL, role TEXT NOT NULL,
first_name TEXT NOT NULL, first_name TEXT NOT NULL,
last_name TEXT NOT NULL, last_name TEXT NOT NULL,
verified BOOLEAN NOT NULL DEFAULT FALSE, verified BOOLEAN NOT NULL DEFAULT FALSE
); );

View File

@@ -27,6 +27,7 @@ impl RegisterUser {
role: "user".to_string(), role: "user".to_string(),
first_name: self.first_name, first_name: self.first_name,
last_name: self.last_name, last_name: self.last_name,
verified: false,
}) })
} }
} }
@@ -45,6 +46,7 @@ pub struct QueryUser {
pub role: String, pub role: String,
pub first_name: String, pub first_name: String,
pub last_name: String, pub last_name: String,
pub verified: bool,
} }
impl QueryUser { impl QueryUser {
@@ -67,6 +69,7 @@ pub struct InsertUser {
pub role: String, pub role: String,
pub first_name: String, pub first_name: String,
pub last_name: String, pub last_name: String,
pub verified: bool,
} }
impl InsertUser { impl InsertUser {

View File

@@ -350,6 +350,7 @@ pub fn init_routes(config: &mut web::ServiceConfig) {
}; };
let mut u = r.convert_to_insert().unwrap(); let mut u = r.convert_to_insert().unwrap();
u.role = "admin".to_string(); u.role = "admin".to_string();
u.verified = true;
let _ = InsertUser::insert(u); let _ = InsertUser::insert(u);
config.service(web::scope("auth") config.service(web::scope("auth")
.service(register) .service(register)

View File

@@ -46,5 +46,6 @@ diesel::table! {
role -> Text, role -> Text,
first_name -> Text, first_name -> Text,
last_name -> Text, last_name -> Text,
verified -> Bool,
} }
} }

View File

@@ -10,6 +10,7 @@ import {
Card, Card,
Checkbox, Checkbox,
Container, Container,
Grid,
Group, Group,
Menu, Menu,
Modal, Modal,
@@ -147,22 +148,38 @@ export default function Topbar() {
<Text ta='center' fz='sm' c='dimmed'> <Text ta='center' fz='sm' c='dimmed'>
{user.role} {user.role}
</Text> </Text>
<Button <Grid mt='xl'>
fullWidth <Grid.Col span={6}>
radius='md' <Button
mt='xl' fullWidth
size='md' radius='md'
variant='default' size='xs'
onClick={async () => { variant='default'
const response = await logout(); onClick={() => {
if (response?.status == 200) { toggle(undefined);
Cookies.remove('logged_in'); }}
setUser(undefined); >
} Profile
}} </Button>
> </Grid.Col>
Logout <Grid.Col span={6}>
</Button> <Button
fullWidth
radius='md'
size='xs'
variant='default'
onClick={async () => {
const response = await logout();
if (response?.status == 200) {
Cookies.remove('logged_in');
setUser(undefined);
}
}}
>
Logout
</Button>
</Grid.Col>
</Grid>
</Card> </Card>
</Menu.Dropdown> </Menu.Dropdown>
</Menu> </Menu>
@@ -207,9 +224,6 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
if (!/[!@#$%^&*]/.test(value)) { if (!/[!@#$%^&*]/.test(value)) {
return 'Password must contain at least one special character'; return 'Password must contain at least one special character';
} }
if (/(.)\1\1/.test(value)) {
return 'Password must not contain more than 2 repeating characters';
}
return null; return null;
} }
@@ -223,13 +237,12 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
return null; return null;
} }
const form = useForm({ const registerForm = useForm({
initialValues: { initialValues: {
firstName: '', firstName: '',
lastName: '', lastName: '',
email: '', email: '',
password: '', password: ''
remember: false
}, },
validate: { validate: {
firstName: (value) => (value.trim().length > 0 ? null : 'First name is required'), firstName: (value) => (value.trim().length > 0 ? null : 'First name is required'),
@@ -239,10 +252,26 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
} }
}); });
const loginForm = useForm({
initialValues: {
email: '',
password: '',
remember: false
}
});
const resetForm = useForm({
initialValues: {
email: ''
}
});
function onClose() { function onClose() {
toggle(undefined); toggle(undefined);
if (!form.values.remember) { registerForm.reset();
form.reset(); resetForm.reset();
if (!loginForm.values.remember) {
loginForm.reset();
} }
} }
@@ -258,8 +287,8 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
</Anchor> </Anchor>
</Text> </Text>
<Paper withBorder shadow='md' p={30} mt={30} radius='md'> <Paper withBorder shadow='md' p={30} mt={30} radius='md'>
<form onSubmit={form.onSubmit(async (values) => console.log(values))}> <form onSubmit={resetForm.onSubmit(async (values) => console.log(values))}>
<TextInput label='Email' placeholder='you@example.com' required {...form.getInputProps('email')} /> <TextInput label='Email' placeholder='you@example.com' required {...resetForm.getInputProps('email')} />
<Button type='submit' fullWidth mt='xl'> <Button type='submit' fullWidth mt='xl'>
Reset password Reset password
</Button> </Button>
@@ -278,7 +307,7 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
<Paper withBorder shadow='md' p={30} mt={30} radius='md'> <Paper withBorder shadow='md' p={30} mt={30} radius='md'>
<form <form
onSubmit={form.onSubmit(async (values) => { onSubmit={registerForm.onSubmit(async (values) => {
const id = notifications.show({ const id = notifications.show({
loading: true, loading: true,
title: `Creating account`, title: `Creating account`,
@@ -327,16 +356,27 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
} }
})} })}
> >
<TextInput label='First name' placeholder='John' required {...form.getInputProps('firstName')} /> <TextInput label='First name' placeholder='John' required {...registerForm.getInputProps('firstName')} />
<TextInput label='Last name' placeholder='Smith' required mt='md' {...form.getInputProps('lastName')} /> <TextInput
<TextInput label='Email' placeholder='you@example.com' required {...form.getInputProps('email')} /> label='Last name'
placeholder='Smith'
required
mt='md'
{...registerForm.getInputProps('lastName')}
/>
<TextInput
label='Email'
placeholder='you@example.com'
required
{...registerForm.getInputProps('email')}
/>
<PasswordInput <PasswordInput
label='Password' label='Password'
description='Passwords must be at least 10 characters long, contain at least one number, one uppercase letter, one lowercase letter, and one special character.' description='Passwords must be at least 10 characters long, contain at least one number, one uppercase letter, one lowercase letter, and one special character.'
placeholder='Your password' placeholder='Your password'
required required
mt='md' mt='md'
{...form.getInputProps('password')} {...registerForm.getInputProps('password')}
/> />
<Button type='submit' fullWidth mt='xl'> <Button type='submit' fullWidth mt='xl'>
Sign up Sign up
@@ -356,7 +396,7 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
<Paper withBorder shadow='md' p={30} mt={30} radius='md'> <Paper withBorder shadow='md' p={30} mt={30} radius='md'>
<form <form
onSubmit={form.onSubmit(async (values) => { onSubmit={loginForm.onSubmit(async (values) => {
const response = await login(values.email, values.password); const response = await login(values.email, values.password);
if (response) { if (response) {
setUser(response.user); setUser(response.user);
@@ -371,16 +411,16 @@ function LoginModal({ type, toggle, setUser }: LoginModalProps) {
} }
})} })}
> >
<TextInput label='Email' placeholder='you@example.com' required {...form.getInputProps('email')} /> <TextInput label='Email' placeholder='you@example.com' required {...loginForm.getInputProps('email')} />
<PasswordInput <PasswordInput
label='Password' label='Password'
placeholder='Your password' placeholder='Your password'
required required
mt='md' mt='md'
{...form.getInputProps('password')} {...loginForm.getInputProps('password')}
/> />
<Group justify='space-between' mt='lg'> <Group justify='space-between' mt='lg'>
<Checkbox label='Remember me' {...form.getInputProps('remember')} /> <Checkbox label='Remember me' {...loginForm.getInputProps('remember')} />
<Anchor component='a' size='sm' onClick={() => toggle('reset')}> <Anchor component='a' size='sm' onClick={() => toggle('reset')}>
Forgot password? Forgot password?
</Anchor> </Anchor>