Skip to content

Commit

Permalink
Merge a565fea into 620e6a9
Browse files Browse the repository at this point in the history
  • Loading branch information
Paccy10 committed Apr 4, 2019
2 parents 620e6a9 + a565fea commit f39203f
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 215 deletions.
3 changes: 3 additions & 0 deletions .env_sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ PASSWORD_PRODUCTION=
DB_PRODUCTION=
DB_HOSTNAME=

GOOGLE_CLIENT_ID =
GOOGLE_CLIENT_SECRET =

SENDER_EMAIL =
SENDER_PASS =

Expand Down
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"consistent-return": 0,
"no-param-reassign": 0,
"comma-dangle": 0,
"class-methods-use-this": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { "commonjs": true }],
"no-shadow": ["error", { "allow": ["req", "res", "err"] }],
Expand Down
9 changes: 6 additions & 3 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ module.exports = {
password: process.env.PASSWORD_DEV,
database: process.env.DB_DEV,
host: '127.0.0.1',
dialect: 'postgres'
dialect: 'postgres',
logging: false
},
test: {
username: process.env.USERNAME_TEST,
password: process.env.PASSWORD_TEST,
database: process.env.DB_TEST,
host: '127.0.0.1',
dialect: 'postgres'
dialect: 'postgres',
logging: false
},
production: {
username: process.env.USERNAME_PRODUCTION,
password: process.env.PASSWORD_PRODUCTION,
database: process.env.DB_PRODUCTION,
host: process.env.DB_HOSTNAME,
dialect: 'postgres'
dialect: 'postgres',
logging: false
},
email: {
user: process.env.SENDER_EMAIL,
Expand Down
37 changes: 34 additions & 3 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import sendEmail from '../helpers/sendEmail/callMailer';

dotenv.config();
const User = models.user;
const secretKey = process.env.secretOrKey;
const expirationTime = { expiresIn: '1day' };
/**
* @user controller
* @exports
Expand All @@ -18,7 +20,7 @@ class UserController {
* @param {Object} res - Response to the user
* @returns {Object} Response
*/
static async signup(req, res) {
async signup(req, res) {
const newUser = {
username: req.body.username,
email: req.body.email,
Expand All @@ -27,7 +29,7 @@ class UserController {
try {
const { dataValues: user } = await User.create(newUser);
const payload = { id: user.id, username: user.username, email: user.email };
const token = jwt.sign(payload, process.env.secretOrKey, { expiresIn: '1day' });
const token = jwt.sign(payload, secretKey, expirationTime);
const response = await sendEmail(user.email, token);
return res.status(201).json({
email: user.email,
Expand All @@ -41,6 +43,35 @@ class UserController {
return res.status(500).json({ error: error.message });
}
}

/**
*
* @param {Object} req -requestesting from user
* @param {Object} res -responding from user
* @returns {Object} Response with status of 201
*/
login(req, res) {
const user = {
email: req.body.email,
password: req.body.password
};

return User.findOne({ where: { email: user.email } })
.then((foundUser) => {
if (foundUser && bcrypt.compareSync(user.password, foundUser.password)) {
const payload = {
username: foundUser.username,
email: foundUser.email,
};
const token = jwt.sign(payload, secretKey, expirationTime);
res.status(200).json({ status: 200, token, user: payload });
} else {
res.status(400).json({ status: 400, error: 'Incorrect username or password' });
}
}).catch((error) => {
res.status(500).json({ error });
});
}
}

export default UserController;
export default new UserController();
Loading

0 comments on commit f39203f

Please sign in to comment.