backend overhaul

This commit is contained in:
Mikayla Dobson
2022-09-22 13:14:43 -05:00
parent 905b3266a3
commit 75ebb5e43d
9 changed files with 276 additions and 16 deletions

View File

@@ -1,3 +1,10 @@
/**
* IMPORTANT:
* THIS VERSION OF THE AUTH SERVICE IS DEPRECATED
* THESE SERVICES WILL BE MIGRATED TO AuthService.js
* WITHIN THE SAME DIRECTORY
**/
const { connect } = require('../db/Pool');
const bcrypt = require('bcrypt');

45
services/AuthService.js Normal file
View File

@@ -0,0 +1,45 @@
const bcrypt = require('bcrypt');
const createError = require('http-errors');
const UserModel = require('../models/UserModel');
const UserInstance = new UserModel();
module.exports = class AuthService {
async register(data) {
const { email } = data;
try {
const user = await UserInstance.findOneById(email);
if (user) {
throw createError(409, 'Email already in use');
}
return await UserInstance.create(data);
} catch(e) {
throw new Error(e);
}
}
// yet to be implemented
async login(data) {
const { email, password } = data;
try {
const user = await UserInstance.findOneByEmail(email);
if (!user) throw createError(401, 'Incorrect email or password');
const match = await bcrypt.compare(user.password, password);
if (!match) throw createError(401, 'Incorrect email or password');
return user;
} catch(e) {
throw createError(500, e);
}
}
// yet to be implemented
async deleteOne(data) {
return data;
}
// TO IMPLEMENT: google, facebook passport strategies
}

View File

@@ -0,0 +1,5 @@
const pgp = require('pg-promise')({ capSQL: true });
module.exports = class ExampleService {
}

26
services/UserService.js Normal file
View File

@@ -0,0 +1,26 @@
const createError = require('http-errors');
const UserModel = require('../models/UserModel');
const UserInstance = new UserModel();
module.exports = class UserService {
async get(data) {
const { id } = data;
try {
const user = await UserInstance.findOneById(id);
if (!user) throw createError(404, 'User not found');
return user;
} catch(e) {
throw new Error(e);
}
}
async update(data) {
try {
const user = await UserInstance.update(data);
return user;
} catch(e) {
throw new Error(e);
}
}
}