Add authentication endpoints with logic
This commit is contained in:
43
services/auth/authRouter.js
Normal file
43
services/auth/authRouter.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const router = require('express').Router();
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const { generateToken } = require('../auth/authenticate');
|
||||
const Users = require('../users/usersModel');
|
||||
|
||||
router.post('/register', (req, res) => {
|
||||
let user = req.body;
|
||||
const hash = bcrypt.hashSync(user.password, 10);
|
||||
user.password = hash;
|
||||
|
||||
Users.add(user)
|
||||
.then(saved => {
|
||||
res.status(201).json(saved);
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).json(error);
|
||||
});
|
||||
});
|
||||
|
||||
router.post('/login', (req, res) => {
|
||||
let { username, password } = req.body;
|
||||
|
||||
Users.findBy({ username })
|
||||
.first()
|
||||
.then(user => {
|
||||
if (user && bcrypt.compareSync(password, user.password)) {
|
||||
const token = generateToken(user);
|
||||
|
||||
res.status(200).json({
|
||||
message: `Welcome ${user.username}!`,
|
||||
token
|
||||
});
|
||||
} else {
|
||||
res.status(401).json({ message: 'Invalid Credentials' });
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).json(error);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
37
services/auth/authenticate.js
Normal file
37
services/auth/authenticate.js
Normal file
@@ -0,0 +1,37 @@
|
||||
require("dotenv").config();
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const jwtKey = process.env.JWT_SECRET;
|
||||
|
||||
module.exports = {
|
||||
authenticate,
|
||||
generateToken
|
||||
};
|
||||
|
||||
function authenticate(req, res, next) {
|
||||
const token = req.get('Authorization');
|
||||
|
||||
if (token) {
|
||||
jwt.verify(token, jwtKey, (err, decoded) => {
|
||||
if (err) return res.status(401).json(err);
|
||||
req.decoded = decoded;
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
return res.status(401).json({
|
||||
error: 'No token provided, must be set on the Authorization Header',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function generateToken(user) {
|
||||
const payload = {
|
||||
username: user.username
|
||||
};
|
||||
|
||||
const options = {
|
||||
expiresIn: '3d'
|
||||
};
|
||||
|
||||
return jwt.sign(payload, jwtKey, options);
|
||||
}
|
||||
25
services/users/usersModel.js
Normal file
25
services/users/usersModel.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const db = require('../../data/dbConfig');
|
||||
|
||||
module.exports = {
|
||||
add,
|
||||
findBy,
|
||||
findById,
|
||||
};
|
||||
|
||||
function findBy(filter) {
|
||||
return db('users')
|
||||
.where(filter);
|
||||
}
|
||||
|
||||
async function add(user) {
|
||||
const [id] = await db('users')
|
||||
.insert(user);
|
||||
return findById(id);
|
||||
}
|
||||
|
||||
function findById(id) {
|
||||
return db('users')
|
||||
.where({ id })
|
||||
.first()
|
||||
.select('id', 'email', 'username');
|
||||
}
|
||||
Reference in New Issue
Block a user