Skip to content

Commit

Permalink
style: adjust controllers to match project code style
Browse files Browse the repository at this point in the history
  • Loading branch information
eowfenth committed May 6, 2020
1 parent 781ff1a commit f8e699d
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 202 deletions.
73 changes: 37 additions & 36 deletions src/session/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,45 @@ const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const User = require('../user/model');

const comparePassword = async (string, password) => bcrypt.compare(string, password);
const comparePassword = async (string, password) =>
bcrypt.compare(string, password);

const auth = async (req, res) => {
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).json({
error: 400,
message: 'Bad Format',
});
}

const user = await User.getOneByEmail(email);

if (user) {
const doesPasswordMatch = await comparePassword(
password,
user.password_hash,
);

if (doesPasswordMatch) {
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRE_TIME,
});

return res.json({
email,
username: user.username,
name: user.name,
token,
});
}
}

return res.status(403).json({
error: 403,
message: 'Forbidden',
});
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).json({
error: 400,
message: 'Bad Format',
});
}

const user = await User.getOneByEmail(email);

if (user) {
const doesPasswordMatch = await comparePassword(
password,
user.password_hash,
);

if (doesPasswordMatch) {
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRE_TIME,
});

return res.json({
email,
username: user.username,
name: user.name,
token,
});
}
}

return res.status(403).json({
error: 403,
message: 'Forbidden',
});
};

module.exports = { auth };
Loading

0 comments on commit f8e699d

Please sign in to comment.