Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
MY_PRIVATE_KEY=myprivatekey
JWT_KEY=wearethechampions
MONGO_HOST=127.0.0.1
MONGO_PORT=27017
MONGO_DB=apigateway-local-dev
MONGO_USERNAME=ongdev
MONGO_PASSWORD=Domaybiet!23
PORT=3000
UAA_HOST=localhost
UAA_PORT=3333
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import helmet from 'helmet';
import morgan from 'morgan';
import { join } from 'path';
import rfs from 'rotating-file-stream';
import dotenv from 'dotenv';
import dotEnvWithDefault from 'dotenv-defaults';
import logger from './src/logger/logger';
import connectDatabase from './src/configs/db.config';

/* istanbul ignore next */
dotenv.config();
dotEnvWithDefault.config();

// configure isProduction variable
const isProduction = process.env.NODE_ENV === 'production';
Expand Down
3,877 changes: 2,467 additions & 1,410 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.6.2",
"axios": "^0.19.2",
"babel-plugin-istanbul": "^5.2.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv": "^8.1.0",
"dotenv-defaults": "^1.1.1",
"errorhandler": "^1.5.1",
"express": "^4.17.1",
"express-jwt": "^5.3.1",
Expand Down Expand Up @@ -39,8 +40,8 @@
"eslint-plugin-import": "^2.18.2",
"mocha": "^6.2.1",
"mocha-logger": "^1.0.6",
"nodemon": "^1.19.3",
"nyc": "^14.1.1",
"nodemon": "^2.0.2",
"nyc": "^15.0.0",
"sinon": "^7.5.0",
"webpack": "^4.41.0",
"webpack-cli": "^3.3.9"
Expand Down Expand Up @@ -83,4 +84,4 @@
"url": "https://github.com/OngDev/backend/issues"
},
"homepage": "https://github.com/OngDev/backend#readme"
}
}
8 changes: 8 additions & 0 deletions src/constant/response.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const OK = 200;
export const CREATED = 201;

export const BAD_REQUEST = 400;
export const UNAUTHORIZED = 401;
export const NOT_FOUND = 404;

export const INTERNAL_ERROR = 500;
10 changes: 10 additions & 0 deletions src/constant/route.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import dotEnvWithDefaults from 'dotenv-defaults';

dotEnvWithDefaults.config();

const { UAA_HOST, UAA_PORT } = process.env;
const UAA_AUTH_ROUTE = `http://${UAA_HOST}:${UAA_PORT}/api/auth`;

export const REGISTER_ROUTE = `${UAA_AUTH_ROUTE}/register`;

export const LOGIN_ROUTE = `${UAA_AUTH_ROUTE}/login`;
80 changes: 42 additions & 38 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
import AuthService from '../services/auth.service';
import { LOGIN_ROUTE, REGISTER_ROUTE } from '../constant/route.constant';
import { BAD_REQUEST, INTERNAL_ERROR, OK } from '../constant/response.constant';
import { getErrorResponse, getMessageResponse, getSuccessResponse } from '../helpers/ResponseHelper';
import Fetcher from '../utils/fetcher';
import logger from '../logger/logger';

const AuthController = {};

AuthController.login = async (req, res) => {
try {
const { email, password } = req.body;
const userData = await AuthService.login({
email,
password,
});
return res.status(200).json({ status: 200, data: userData, message: 'Succesfully logged in' });
const {
status,
data,
message,
} = await Fetcher.post(LOGIN_ROUTE, { email, password });
return getSuccessResponse(res, status, { data, message });
} catch (error) {
return res.status(400).json({ status: 400, message: error.message });
return getErrorResponse(res, BAD_REQUEST, error.message);
}
};

AuthController.register = async (req, res) => {
try {
const { email, fullName, password } = req.body;
const userData = await AuthService.register({
email,
fullName,
password,
});
return res.status(200).json({ status: 200, data: userData, message: 'Succesfully registered' });
const fetchResult = await Fetcher.post(REGISTER_ROUTE, { email, fullName, password });
if (!fetchResult.error) {
return getSuccessResponse(res, fetchResult.status, fetchResult.data);
}
return getErrorResponse(res, fetchResult.status, fetchResult.error.message);
} catch (error) {
return res.status(400).json({ status: 400, message: error.message });
return getErrorResponse(res, INTERNAL_ERROR, error.message);
}
};

AuthController.getCurrent = async (req, res) => res
.status(200)
.json({ status: 200, data: { email: req.user.email, fullName: req.user.fullName } });
// AuthController.getCurrent = async (req, res) => res
// .status(200)
// .json({ status: 200, data: { email: req.user.email, fullName: req.user.fullName } });

AuthController.logout = async (req, res) => {
try {
AuthService.logout({
user: req.user,
foundToken: req.token,
});
res.send();
} catch (error) {
res.status(500).send(error);
}
};
// AuthController.logout = async (req, res) => {
// try {
// AuthService.logout({
// user: req.user,
// foundToken: req.token,
// });
// res.send();
// } catch (error) {
// res.status(500).send(error);
// }
// };

AuthController.logoutAll = async (req, res) => {
try {
AuthService.logoutAll({
user: req.user,
});
res.send();
} catch (error) {
res.status(500).send(error);
}
};
// AuthController.logoutAll = async (req, res) => {
// try {
// AuthService.logoutAll({
// user: req.user,
// });
// res.send();
// } catch (error) {
// res.status(500).send(error);
// }
// };

export default AuthController;
16 changes: 16 additions & 0 deletions src/helpers/ResponseHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Logger from '../logger/logger';

export function getErrorResponse(res, status, message) {
Logger.error(`Response error with STATUS: ${status} and MESSAGE: ${message}`);
return res.status(status).send(message);
}

export function getSuccessResponse(res, status, data) {
Logger.info(`Send successful response with STATUS: ${status} and DATA: ${data}`);
return res.status(status).json(data);
}

export function getMessageResponse(res, status, message) {
Logger.info(`Send message response with STATUS: ${status} and MESSAGE: ${message}`);
return res.status(status).send(message);
}
1 change: 0 additions & 1 deletion src/middlewares/auth.middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import jwt from 'jsonwebtoken';
import UserModel from '../models/user.model';
import logger from '../logger/logger';

/* istanbul ignore next */
Expand Down
93 changes: 0 additions & 93 deletions src/models/user.model.js

This file was deleted.

57 changes: 0 additions & 57 deletions src/services/auth.service.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/tests/intTests/auth/service.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable no-unused-expressions */
import chai, { expect } from 'chai';
import ChaiAsPromised from 'chai-as-promised';
import dotenv from 'dotenv';
import dotEnvWithDefaults from 'dotenv-defaults';

import UserModel from '../../../models/user.model';
import AuthService from '../../../services/auth.service';

chai.use(ChaiAsPromised);
dotenv.config();
dotEnvWithDefaults.config();

describe('### Authentication service IT', () => {
describe('Register function', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/intTests/user/model.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable no-unused-expressions */
import chai, { expect } from 'chai';
import ChaiAsPromised from 'chai-as-promised';
import dotenv from 'dotenv';
import dotEnvWithDefaults from 'dotenv-defaults';

import UserModel from '../../../models/user.model';

chai.use(ChaiAsPromised);
dotenv.config();
dotEnvWithDefaults.config();

describe('### User model IT', () => {
afterEach(async () => {
Expand Down
Loading