generated from lucien/api-template
Added API endpoints and dockerfile
This commit is contained in:
parent
924e55b4f3
commit
5ddad2ed61
31 changed files with 3885 additions and 942 deletions
24
back/api/connect.js
Normal file
24
back/api/connect.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
const express = require('express');
|
||||
const sha256 = require("sha256");
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getUserByUsername } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
const connection = await getConnection();
|
||||
const users = await getUserByUsername(connection, username);
|
||||
connection.end();
|
||||
if (users[0]) {
|
||||
if (users[0].password === sha256(password)) {
|
||||
const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, {
|
||||
expiresIn: 1000 * 60 * 60 * 24 * 7,
|
||||
});
|
||||
res.send({ token: token });
|
||||
}
|
||||
}
|
||||
res.status(401).send({ error: 'Invalid username or password' });
|
||||
});
|
||||
|
||||
module.exports = router;
|
Loading…
Add table
Add a link
Reference in a new issue