main
Kar 2023-05-15 17:57:16 +05:30
parent 02874ff761
commit a538f23124
4 changed files with 24 additions and 8 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,7 @@
export default function handler(req, res) {
// const sqlite3 = require('sqlite3').verbose();
console.log(req.body)
// console.log(req.body)
const sqlite3 = require('sqlite3');
const argon2 = require('argon2');
const jwt = require('jsonwebtoken');
@ -10,8 +10,9 @@ const jwt = require('jsonwebtoken');
const db = new sqlite3.Database('data/appUser.db');
// Fetch the user record
const username = 'example_user';
const sql = 'SELECT pass FROM user_table WHERE username = ?';
const username = req.body.email;
// console.log(username)
const sql = 'SELECT * FROM user WHERE email = ?';
db.get(sql, [username], async (err, userRecord) => {
if (err) {
console.error(err);
@ -19,8 +20,9 @@ db.get(sql, [username], async (err, userRecord) => {
}
if (userRecord) {
const storedHash = userRecord.password_hash;
const providedPassword = 'example_password';
const storedHash = userRecord.pass;
console.log(storedHash)
const providedPassword = req.body.password;
try {
// Verify the provided password with the stored hash
@ -28,11 +30,25 @@ db.get(sql, [username], async (err, userRecord) => {
if (isPasswordValid) {
// Passwords match, generate JWT token
const secretKey = process.env.JWT_SEC;
const tokenPayload = { username: username };
const secretKey = process.env.TOKEN_HEADER_KEY;
const tokenPayload = {
email:userRecord.email,
role: userRecord.type,
exp: Math.floor(Date.now() / 1000) + 3600*24*7,
};
const jwtToken = jwt.sign(tokenPayload, secretKey, { algorithm: 'HS256' });
const jwtTokenData ={
data:{
access_token:
username: username,
exp: Math.floor(Date.now() / 1000) + 3600*24*7,
}
};
// Return the JWT token
res.status(200).json(jwtToken);
console.log(jwtToken);
} else {
console.log('Invalid password');
@ -48,5 +64,5 @@ db.get(sql, [username], async (err, userRecord) => {
db.close();
});
res.status(200).json({ name: 'John Doe' });
// res.status(200).json({ name: 'John Doe' });
}