Skip to content
Snippets Groups Projects
Commit 30a4f321 authored by Jukka-Pekka Sirkiä's avatar Jukka-Pekka Sirkiä
Browse files

Added a route for user authentication

parent 07e60e27
No related branches found
No related tags found
No related merge requests found
...@@ -2,12 +2,22 @@ const userDataServices = require('../services/userData'); ...@@ -2,12 +2,22 @@ const userDataServices = require('../services/userData');
const storeUsernameAndPassword = (req, res) => { const storeUsernameAndPassword = (req, res) => {
userDataServices.storeUsernameAndPassword(req.body.username, req.body.password, (err, data) => { userDataServices.storeUsernameAndPassword(req.body.username, req.body.password, (err, data) => {
if (err) res.status(503).send('Server error'); if (err) return res.status(503).send('Server error.');
res.status(200).json(data); res.status(200).json(data);
}); });
}; };
const authenticateUser = (req, res) => {
userDataServices.authenticateUser(req.body.username, req.body.password, (err, result) => {
if (err) return res.status(404).send('User not found.');
if (!result) return res.status(401).send('Authentication failed.');
res.status(200).send('User authenticated.');
});
};
module.exports = { module.exports = {
storeUsernameAndPassword storeUsernameAndPassword,
authenticateUser
}; };
\ No newline at end of file
...@@ -66,5 +66,5 @@ const getUser = (username, callback) => { ...@@ -66,5 +66,5 @@ const getUser = (username, callback) => {
module.exports = { module.exports = {
initializeDatabase, initializeDatabase,
addRow, addRow,
getUser, getUser
}; };
\ No newline at end of file
...@@ -19,5 +19,5 @@ const initializeDatabase = () => { ...@@ -19,5 +19,5 @@ const initializeDatabase = () => {
dataAccess.initializeDatabase(); dataAccess.initializeDatabase();
}; };
//initializeDatabase(); initializeDatabase();
startServer(); startServer();
\ No newline at end of file
get http://localhost:8000/api/v1/userData
content-type: application/json
{
"username": "Superuser",
"password": "StrongPassword"
}
\ No newline at end of file
...@@ -4,5 +4,6 @@ const router = express.Router(); ...@@ -4,5 +4,6 @@ const router = express.Router();
const controllers = require('../controllers/controllers'); const controllers = require('../controllers/controllers');
router.post('/api/v1/userData', controllers.storeUsernameAndPassword); router.post('/api/v1/userData', controllers.storeUsernameAndPassword);
router.get('/api/v1/userData', controllers.authenticateUser);
module.exports = router; module.exports = router;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment