diff --git a/src/pages/api/login.js b/src/pages/api/login.js new file mode 100644 index 0000000..c48f994 --- /dev/null +++ b/src/pages/api/login.js @@ -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' }); +} \ No newline at end of file