Refactoring day3

This commit is contained in:
2020-08-24 21:59:37 +02:00
parent 3c0d0c63fb
commit 3674e4ce3c
9 changed files with 132 additions and 216 deletions

View File

@@ -1,20 +1,11 @@
import express from "express";
import mongoose from "mongoose";
import User from "../models/users.js";
import bcrypt from "bcrypt";
import * as services from "../services/services.js";
import * as config from "../config/index.js";
import AgileCRMManager from "agile_crm";
const { API_KEY, jwtSecret } = config;
const { CRM_USER, CRM_EMAIL, CRM_KEY } = config;
var router = express.Router();
var agileAPI = new AgileCRMManager("bankai", API_KEY, "bankai@bankai.pl");
var success = function (data) {
console.log("Task successfull");
};
var error = function (err) {
console.log("Task failed successfully");
};
var agileAPI = new AgileCRMManager(CRM_USER, CRM_KEY, CRM_EMAIL);
// LOGIN
router.post("/login", async (req, res) => {
@@ -23,16 +14,10 @@ router.post("/login", async (req, res) => {
throw services.newError("No input data", 204);
}
const user = await services.fetchUser(req.body.email);
/* await services.checkPassword(req.body.password, user.password);
const userNoPass = {
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
id: user._id,
};
var token = services.generateAuthToken(userNoPass);
res.header("x-auth-token", token).status(202).send(userNoPass); */
res.send(user);
await services.checkPassword(req.body.password, user.password);
const safeUser = services.prepareSafeUser(user);
var token = services.generateAuthToken(safeUser);
res.header("x-auth-token", token).status(202).send(safeUser);
} catch (error) {
services.handleError(error, res);
}
@@ -42,17 +27,10 @@ router.post("/login", async (req, res) => {
router.post("/register", async (req, res) => {
try {
await services.checkEmailTaken(req.body.email);
const password = await services.hashPass(req.body.password);
const user = new User({
_id: new mongoose.Types.ObjectId(),
email: req.body.email,
password: password,
firstname: req.body.firstname,
lastname: req.body.lastname,
});
const user = await services.createUser(req);
await user.save();
const contact = services.composeNewContact(user);
agileAPI.contactAPI.add(contact, success, error);
agileAPI.contactAPI.add(contact, null, null);
res.sendStatus(201);
} catch (e) {
services.handleError(e, res);