Skip to content

Commit

Permalink
Send response to client upon successful token validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarrow committed Jun 13, 2020
1 parent 3608d48 commit 354c1e7
Showing 1 changed file with 61 additions and 70 deletions.
131 changes: 61 additions & 70 deletions auth/auth-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,79 @@ const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

function generateToken(user) {
console.log(user);
console.log(user);

const payload = {
username: user.username,
id: user.id
};
const options = {
expiresIn: '1d'
};
return jwt.sign(payload, 'aeaeiouAndSometimesY', options);
const payload = {
username: user.username,
id: user.id,
};
const options = {
expiresIn: '1d',
};
return jwt.sign(payload, 'aeaeiouAndSometimesY', options);
}

router.post('/register', (req, res) => {
const { username, password, email } = req.body;
Users.insert({
username,
password: bcrypt.hashSync(password, 8),
email
})
.then(id => {
res
.status(201)
.json({
message: 'User registration complete',
id
});
const { username, password, email } = req.body;
Users.insert({
username,
password: bcrypt.hashSync(password, 8),
email,
})
.catch(err => {
console.log(err);
res
.status(500)
.json({ message: 'Failed to register user!' });
});
.then((id) => {
res.status(201).json({
message: 'User registration complete',
id,
});
})
.catch((err) => {
console.log(err);
res.status(500).json({ message: 'Failed to register user!' });
});
});

router.post('/login', (req, res) => {
const { username, password } = req.body;
Users.findByUsername(username)
.then(user => {
if (
user &&
bcrypt.compareSync(password, user.password)
) {
const token = generateToken(user);
res.status(200).json({
message: 'Login successful!',
token,
user_id: user.id,
username: user.username
const { username, password } = req.body;
Users.findByUsername(username)
.then((user) => {
if (user && bcrypt.compareSync(password, user.password)) {
const token = generateToken(user);
res.status(200).json({
message: 'Login successful!',
token,
user_id: user.id,
username: user.username,
});
} else {
res.status(401).json({ message: 'Password Incorrect!' });
}
})
.catch((err) => {
console.log(err);
res.status(500).json({ message: 'Error during login attempt!' });
});
} else {
res
.status(401)
.json({ message: 'Password Incorrect!' });
}
})
.catch(err => {
console.log(err);
res
.status(500)
.json({ message: 'Error during login attempt!' });
});
});

router.post('/validate-token', (req, res) => {
const token = req.headers.authorization;
if (token) {
jwt.verify(
token,
'aeaeiouAndSometimesY',
(err, decodedToken) => {
if (err) {
res.status(401).json({ you: 'Access Denied!' });
} else {
req.user = decodedToken;
next();
}
}
);
} else {
res.status(400).json({ message: 'Token Missing!' });
}
const token = req.headers.authorization;
if (token) {
jwt.verify(token, 'aeaeiouAndSometimesY', (err, decodedToken) => {
if (err) {
res.status(401).json({
you: 'Access Denied!',
isAuthenticated: false,
});
} else {
req.user = decodedToken;
res.status(200).send({ isAuthenticated: true });
}
});
} else {
res.status(400).json({
message: 'Token Missing!',
isAuthenticated: false,
});
}
});

module.exports = router;

0 comments on commit 354c1e7

Please sign in to comment.