updated codegen, fixed tsconfig, added prisma

added login, registration and session check
This commit is contained in:
lnn0q 2025-03-23 21:06:15 +02:00
parent 9518787206
commit 04b606b7fa
24 changed files with 740 additions and 1747 deletions

View file

@ -1,49 +1,28 @@
import { prisma, uuid } from '@app/server/prisma/client';
import { RegisterInput } from '@generated/types';
import bcrypt from 'bcrypt';
export async function SignUpResolver(_: any, input: any): Promise<any> {
// validate
export async function SignUpResolver(_: any, input: RegisterInput): Promise<void> {
const existingUsername = await prisma.user.findUnique({
where: { username: input.username },
select: { id: true }
});
if (existingUsername != null) throw Error('User with this username already exists.');
// db
const users = [
{
id: '1',
email: 'joe@test.com',
username: 'Joe',
password: 'test'
const existingEmail = await prisma.user.findUnique({
where: { email: input.email },
select: { id: true }
});
if (existingEmail != null) throw Error('Email is already in use.');
const hashedPassword = await bcrypt.hash(input.password, 10);
await prisma.user.create({
data: {
id: uuid(),
email: input.email,
username: input.username,
password: hashedPassword
}
];
const usersDB = { users };
// check for duplicate email+username
// encrypt the password
const hashedPwd = await bcrypt.hash(input.pwd, 10);
// store user in the db, return new user
return user;
});
}
const handleNewUser = async (req, res) => {
const { user, pwd } = req.body;
if (!user || !pwd)
return res.status(400).json({ message: 'Username and password are required.' });
// check for duplicate usernames in the db.
const duplicate = usersDB.users.find(person => person.username === user);
if (duplicate) return res.status(409);
try {
// encrypt the password
const hashedPwd = await brcypt.hash(pwd, 10);
// store the new user
const newUser = { username: user, password: hashedPwd };
usersDB.setUsers([...usersDB.users, newUser]);
await fs_promises.writeFile(
path.join(__dirname, '..', 'model', 'users.json'),
JSON.stringify(usersDB.users)
);
console.log(usersDB.users);
res.status(201).json({ success: `New user ${user} created!` });
} catch (err) {
res.status(500).json({ message: err.message });
}
};