updated codegen, added client query generation, etc.
This commit is contained in:
parent
864f37a652
commit
9518787206
17 changed files with 5244 additions and 445 deletions
49
app/server/graphql/resolvers/auth/register.ts
Normal file
49
app/server/graphql/resolvers/auth/register.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import bcrypt from 'bcrypt';
|
||||
|
||||
export async function SignUpResolver(_: any, input: any): Promise<any> {
|
||||
// validate
|
||||
|
||||
// db
|
||||
const users = [
|
||||
{
|
||||
id: '1',
|
||||
email: 'joe@test.com',
|
||||
username: 'Joe',
|
||||
password: 'test'
|
||||
}
|
||||
];
|
||||
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 });
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue