Kar 2023-05-15 12:23:14 +05:30
parent 16c9718f19
commit 326d16f312
1 changed files with 52 additions and 0 deletions

52
src/pages/api/login.js Normal file
View File

@ -0,0 +1,52 @@
export default function handler(req, res) {
// const sqlite3 = require('sqlite3').verbose();
console.log(req.body)
const sqlite3 = require('sqlite3');
const argon2 = require('argon2');
const jwt = require('jsonwebtoken');
// Connect to the SQLite database
const db = new sqlite3.Database('your_database.db');
// Fetch the user record
const username = 'example_user';
const sql = 'SELECT password_hash FROM user_table WHERE username = ?';
db.get(sql, [username], async (err, userRecord) => {
if (err) {
console.error(err);
return;
}
if (userRecord) {
const storedHash = userRecord.password_hash;
const providedPassword = 'example_password';
try {
// Verify the provided password with the stored hash
const isPasswordValid = await argon2.verify(storedHash, providedPassword);
if (isPasswordValid) {
// Passwords match, generate JWT token
const secretKey = 'your_secret_key';
const tokenPayload = { username: username };
const jwtToken = jwt.sign(tokenPayload, secretKey, { algorithm: 'HS256' });
// Return the JWT token
console.log(jwtToken);
} else {
console.log('Invalid password');
}
} catch (err) {
console.error(err);
}
} else {
console.log('User not found');
}
// Close the database connection
db.close();
});
res.status(200).json({ name: 'John Doe' });
}