backend overhaul
This commit is contained in:
@@ -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
45
services/AuthService.js
Normal 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
|
||||
}
|
||||
5
services/ExampleService.js
Normal file
5
services/ExampleService.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const pgp = require('pg-promise')({ capSQL: true });
|
||||
|
||||
module.exports = class ExampleService {
|
||||
|
||||
}
|
||||
26
services/UserService.js
Normal file
26
services/UserService.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user