Skip to content

Commit

Permalink
Add register endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Oct 27, 2019
1 parent be75f09 commit b43913f
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ node_modules
# package-lock.json (use yarn.lock instead)
package-lock.json

# yarn error logs
yarn-error.log

# Environment varibales
.env*
!.env*.example
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"cross-env": "^6.0.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http-status": "^1.4.0",
"lodash": "^4.17.15",
"mongoose": "^5.7.7",
"pm2": "^4.1.2",
"validator": "^11.1.0",
Expand Down
8 changes: 5 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const express = require('express');
const mongoose = require('./config/mongoose');
const loggger = require('./config/logger');
const routes = require('./routes/v1');

const app = express();

app.use('/', (req, res) => {
res.send('Working');
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use('/v1', routes);

mongoose.connect().then(() => {
loggger.info('Connected to MongoDB');
Expand Down
14 changes: 14 additions & 0 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const httpStatus = require('http-status');
const { userService } = require('../services');

const register = async (req, res) => {
const user = await userService.createUser(req.body);
const response = {
user: user.transform(),
};
res.status(httpStatus.CREATED).send(response);
};

module.exports = {
register,
};
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.authController = require('../controllers/auth.controller');
43 changes: 28 additions & 15 deletions src/models/user.model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');
const validator = require('validator');
const bcrypt = require('bcryptjs');
const { omit, pick } = require('lodash');
const { roles } = require('../config/roles');

const userSchema = mongoose.Schema(
Expand All @@ -21,29 +22,41 @@ const userSchema = mongoose.Schema(
throw new Error('Invalid email');
}
},
password: {
type: String,
required: true,
trim: true,
minlength: 8,
validate(value) {
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) {
throw new Error('Password must contain at least one letter and one number');
}
},
},
role: {
type: String,
enum: roles,
defualt: 'user',
},
password: {
type: String,
required: true,
trim: true,
minlength: 8,
validate(value) {
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) {
throw new Error('Password must contain at least one letter and one number');
}
},
},
role: {
type: String,
enum: roles,
default: 'user',
},
},
{
timestamps: true,
toObject: { getters: true },
toJSON: { getters: true },
}
);

userSchema.methods.toJSON = function() {
const user = this;
return omit(user.toObject(), ['password']);
};

userSchema.methods.transform = function() {
const user = this;
return pick(user.toJSON(), ['id', 'email', 'name', 'role']);
};

userSchema.pre('save', async function(next) {
const user = this;
if (user.isModified('password')) {
Expand Down
8 changes: 8 additions & 0 deletions src/routes/v1/auth.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const authController = require('../../controllers/auth.controller');

const router = express.Router();

router.post('/register', authController.register);

module.exports = router;
8 changes: 8 additions & 0 deletions src/routes/v1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const authRoute = require('./auth.route');

const router = express.Router();

router.use('/auth', authRoute);

module.exports = router;
1 change: 1 addition & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.userService = require('./user.service');
18 changes: 18 additions & 0 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { User } = require('../models');

const checkDuplicateEmail = async (email, excludeUserId) => {
const user = await User.findOne({ email, _id: { $ne: excludeUserId } });
if (user) {
throw new Error('Email already in use');
}
};

const createUser = async userBody => {
await checkDuplicateEmail(userBody.email);
const user = await User.create(userBody);
return user;
};

module.exports = {
createUser,
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,11 @@ http-proxy-agent@^2.1.0:
agent-base "4"
debug "3.1.0"

http-status@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/http-status/-/http-status-1.4.0.tgz#1d8cc202258705c90e900cfba99f21160e7a2f4b"
integrity sha512-3w5/ENDYWShP1TmpDYwuX7QPKV8/xE7fdvr/XtGy8njDSjKljCjhHel7HJD7sR/FHEeVpAssDfsU5ntoyhquqw==

https-proxy-agent@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-3.0.1.tgz#b8c286433e87602311b01c8ea34413d856a4af81"
Expand Down

0 comments on commit b43913f

Please sign in to comment.