troubleshooting on user registration flow

This commit is contained in:
Mikayla Dobson
2022-12-01 18:49:51 -06:00
parent ba4b6e08c9
commit 671e250c60
9 changed files with 60 additions and 49 deletions

View File

@@ -18,33 +18,33 @@ export default class AuthService {
data.isadmin = false;
try {
let receivedUser: IUser | undefined;
// not allowed to use email address that already exists
const user = await UserInstance.getOneByEmail(email);
if (user) throw createError('409', 'Email already in use');
// hash password and create new user record
bcrypt.genSalt(10, (err, salt) => {
const salt = await bcrypt.genSalt();
bcrypt.hash(password!, salt, async (err, hash) => {
if (err) throw err;
bcrypt.hash(password!, salt, async (err, hash) => {
if (err) throw err;
const newData = {
...data,
password: hash
}
const result: IUser = await UserInstance.post(newData);
const newData = {
...data,
password: hash
}
// basic profile setup
await CollectionInstance.post({
name: `${data.firstname}'s Collection`,
active: true,
ismaincollection: true,
ownerid: result.id!.toString(),
datecreated: now,
datemodified: now
});
const res: IUser | null = await UserInstance.post(newData);
if (res) receivedUser = res;
return result;
})
// basic profile setup
res && await CollectionInstance.post({
name: `${data.firstname}'s Collection`,
active: true,
ismaincollection: true,
ownerid: res.id!.toString(),
datecreated: now,
datemodified: now
});
})
return true;

View File

@@ -47,7 +47,6 @@ export class Collection {
async post(data: ICollection) {
console.log('new default collection');
console.log(data);
const { name, active, ismaincollection, ownerid } = data;
try {
const statement = `

View File

@@ -72,8 +72,6 @@ export class User {
async post(data: IUser) {
const { firstname, lastname, handle, email, password, active, isadmin } = data;
const datecreated = now;
const datemodified = now;
try {
const statement = `
@@ -83,9 +81,9 @@ export class User {
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING *;
`;
const params = [firstname, lastname, handle, email, password, active, isadmin, datecreated, datemodified];
const params = [firstname, lastname, handle, email, password, active, isadmin, now, now];
const result = await pool.query(statement, params);
if (result.rows.length) return result.rows[0];
if (result.rows.length) return result.rows[0] as IUser;
return null;
} catch (error: any) {
throw new Error(error);

View File

@@ -15,7 +15,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
app.use('/auth', router);
router.get('/', restrictAccess, (req, res, next) => {
if (!req.user) return;
if (!req.user) res.send({ user: undefined });
// @ts-ignore: does not recognize structure of req.user
const { user } = req.user;
@@ -55,25 +55,28 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
}
})
router.post('/register', async (req, res, next) => {
try {
const data: IUser = req.body;
const response = await AuthInstance.register(data);
if (!response) res.sendStatus(400);
// const login = await AuthInstance.login({ email: data.email, password: data.password! });
// console.log(login);
res.status(200).redirect('/');
} catch(e) {
next(e);
}
})
router.delete('/logout', async (req, res, next) => {
try {
req.session.destroy((err) => {
if (err) throw err;
})
res.clearCookie('userid');
res.status(204).send({ message: "Logout successful", success: true });
res.status(204).redirect('/');
} catch(e) {
next(e);
}
});
router.post('/register', async (req, res, next) => {
try {
const data: IUser = req.body;
const response = await AuthInstance.register(data);
res.status(200).send(response);
} catch(e) {
next(e);
}
})
}