mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-19 03:29:37 +02:00
Added login route and token signing
This commit is contained in:
parent
ea57dbf750
commit
5805c708d2
8 changed files with 153 additions and 1 deletions
3
controllers/auth/index.js
Normal file
3
controllers/auth/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
login: require('./login'),
|
||||
};
|
25
controllers/auth/login.js
Normal file
25
controllers/auth/login.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const signToken = require('../../utils/signToken');
|
||||
|
||||
// @desc Login user
|
||||
// @route POST /api/auth/
|
||||
// @access Public
|
||||
const login = asyncWrapper(async (req, res, next) => {
|
||||
const { password, duration } = req.body;
|
||||
|
||||
const isMatch = process.env.PASSWORD == password;
|
||||
|
||||
if (!isMatch) {
|
||||
return next(new ErrorResponse('Invalid credentials', 401));
|
||||
}
|
||||
|
||||
const token = signToken(duration);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: { token },
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = login;
|
Loading…
Add table
Add a link
Reference in a new issue