Skip to content

Commit

Permalink
Seperate account information with user info
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyendacthienngan committed Dec 28, 2021
1 parent c34cca4 commit e250020
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
28 changes: 19 additions & 9 deletions controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const User = require('../models/user');
const Account = require('../models/account');

const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const { registerValidator } = require('../validations/auth');
Expand All @@ -9,25 +11,30 @@ exports.createUser = async function (request, response) {

if (error) return response.send(registerValidator(request.body));

const checkEmailExist = await User.findOne({ email: request.body.email });
const checkEmailExist = await Account.findOne({ email: request.body.email });

if (checkEmailExist) return response.status(422).send('Email is exist');

const salt = await bcrypt.genSalt(10);
const hashPassword = await bcrypt.hash(request.body.password, salt);

const user = new User({
username: request.body.username,
email: request.body.email,
password: hashPassword,
first_name: request.body.first_name,
last_name: request.body.last_name,
phone_number: request.body.phone_number,
birth_date: request.body.birth_date
});

let account = new Account({
username: request.body.username,
email: request.body.email,
password: hashPassword,
});
account.user_id = user._id;
try {
const newAccount = await account.save();
console.log(newAccount)
const newUser = await user.save();

const token = jwt.sign({_id: user._id}, process.env.TOKEN_SECRET, { expiresIn: 60 * 60 * 24 }); //outdated in 1 day
const result = {
"token": token,
Expand All @@ -41,12 +48,15 @@ exports.createUser = async function (request, response) {

exports.login = async function (request, response) {
//TO-DO: Check role admin or user?
const user = await User.findOne({email: request.body.email});
if (!user) return response.status(422).send('Email or Password is not correct');
const checkPassword = await bcrypt.compare(request.body.password, user.password);
const account = await Account.findOne({email: request.body.email});
if (!account) return response.status(422).send('Email is not correct');
const checkPassword = await bcrypt.compare(request.body.password, account.password);

if (!checkPassword) return response.status(422).send('Email or Password is not correct');
if (!checkPassword) return response.status(422).send('Password is not correct');

console.log("account: ", account);
const user = await Account.findOne({_id: account._id});
console.log("user: ", user);
const token = jwt.sign({_id: user._id}, process.env.TOKEN_SECRET, { expiresIn: 60 * 60 * 24 }); //outdated in 1 day
const result = {
"token": token,
Expand Down
29 changes: 29 additions & 0 deletions models/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require("mongoose");
const Schema = mongoose.Schema
const schemaOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};
const Account = new mongoose.Schema ({
username: {
type: String,
required: true,
min: 6,
max: 255
},
email: {
type: String,
required: true,
min: 6,
max: 225
},
password: {
type: String,
required: true,
min: 6,
max: 255
},
is_admin: { type: Boolean, default: false },
user_id: { type: Schema.Types.ObjectId, ref: "User", required: true },
}, schemaOptions);

module.exports = mongoose.model('Account', Account)
19 changes: 0 additions & 19 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,12 @@ const schemaOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};
const User = new mongoose.Schema ({
username: {
type: String,
required: true,
min: 6,
max: 255
},
first_name: { type: String },
last_name: { type: String },
email: {
type: String,
required: true,
min: 6,
max: 225
},
gender: { type: String },
birth_date: { type: Date },
phone_number: { type: String },
country: { type: String },
password: {
type: String,
required: true,
min: 6,
max: 255
},
is_admin: { type: Boolean, default: false },
avatar: { type: String },
description: { type: String }
}, schemaOptions);
Expand Down
2 changes: 1 addition & 1 deletion validations/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const registerValidator = (data) => {
const rule = Joi.object({
username: Joi.string().min(6).max(225).required(),
email: Joi.string().min(6).max(225).required().email(),
password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{6,20}$')).required()
password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{6,20}$')).required(),
})
return rule.validate(data);
}
Expand Down

0 comments on commit e250020

Please sign in to comment.