axios problems

This commit is contained in:
Mikayla Dobson
2022-12-26 18:22:12 -06:00
parent ba4d72b431
commit 3831f110a3
11 changed files with 59 additions and 46 deletions

View File

@@ -9,8 +9,6 @@ const UserInstance = new User();
export default class AuthService {
// methods for local strategies
async register(data: IUser) {
const { email, password } = data;
data.datecreated = now;
data.datemodified = now;
data.active = true;
@@ -18,19 +16,23 @@ export default class AuthService {
try {
// not allowed to use email address that already exists
const user = await UserInstance.getOneByEmail(email);
const user = await UserInstance.getOneByEmail(data.email);
if (user) throw createError('409', 'Email already in use');
// hash password and create new user record
const salt = await bcrypt.genSalt();
bcrypt.hash(password!, salt, async (err, hash) => {
const salt = await bcrypt.genSalt(12);
console.log(salt);
console.log(data.password);
bcrypt.hash(data.password!, salt, (err, hash) => {
if (err) throw err;
const newData = {
...data,
password: hash
}
await UserInstance.post(newData);
UserInstance.post(newData);
})
return true;

View File

@@ -4,8 +4,6 @@ import { StatusCode } from "../util/types";
export function restrictAccess(req: Request, res: Response, next: NextFunction) {
if (req.isAuthenticated()) {
next();
} else {
res.status(StatusCode.Forbidden).send({ ok: false, user: undefined })
}
}

View File

@@ -57,7 +57,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
router.post('/register', async (req, res, next) => {
try {
const data: IUser = req.body;
const data = req.body;
const response = await AuthInstance.register(data);
if (!response) res.status(400).send({ ok: false });
res.status(200).send({ ok: true });

View File

@@ -30,6 +30,8 @@ export const collectionRoute = (app: Express) => {
router.post('/', restrictAccess, async (req, res, next) => {
const data = req.body;
console.log(data);
try {
const result = await CollectionInstance.post(data);
res.status(result.code).send(result.data);