login
parent
02874ff761
commit
a538f23124
BIN
appUser.db
BIN
appUser.db
Binary file not shown.
BIN
data/appUser.db
BIN
data/appUser.db
Binary file not shown.
BIN
mydatabase.db
BIN
mydatabase.db
Binary file not shown.
|
@ -1,7 +1,7 @@
|
||||||
export default function handler(req, res) {
|
export default function handler(req, res) {
|
||||||
// const sqlite3 = require('sqlite3').verbose();
|
// const sqlite3 = require('sqlite3').verbose();
|
||||||
|
|
||||||
console.log(req.body)
|
// console.log(req.body)
|
||||||
const sqlite3 = require('sqlite3');
|
const sqlite3 = require('sqlite3');
|
||||||
const argon2 = require('argon2');
|
const argon2 = require('argon2');
|
||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
|
@ -10,8 +10,9 @@ const jwt = require('jsonwebtoken');
|
||||||
const db = new sqlite3.Database('data/appUser.db');
|
const db = new sqlite3.Database('data/appUser.db');
|
||||||
|
|
||||||
// Fetch the user record
|
// Fetch the user record
|
||||||
const username = 'example_user';
|
const username = req.body.email;
|
||||||
const sql = 'SELECT pass FROM user_table WHERE username = ?';
|
// console.log(username)
|
||||||
|
const sql = 'SELECT * FROM user WHERE email = ?';
|
||||||
db.get(sql, [username], async (err, userRecord) => {
|
db.get(sql, [username], async (err, userRecord) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
@ -19,8 +20,9 @@ db.get(sql, [username], async (err, userRecord) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userRecord) {
|
if (userRecord) {
|
||||||
const storedHash = userRecord.password_hash;
|
const storedHash = userRecord.pass;
|
||||||
const providedPassword = 'example_password';
|
console.log(storedHash)
|
||||||
|
const providedPassword = req.body.password;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Verify the provided password with the stored hash
|
// Verify the provided password with the stored hash
|
||||||
|
@ -28,11 +30,25 @@ db.get(sql, [username], async (err, userRecord) => {
|
||||||
|
|
||||||
if (isPasswordValid) {
|
if (isPasswordValid) {
|
||||||
// Passwords match, generate JWT token
|
// Passwords match, generate JWT token
|
||||||
const secretKey = process.env.JWT_SEC;
|
const secretKey = process.env.TOKEN_HEADER_KEY;
|
||||||
const tokenPayload = { username: username };
|
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 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
|
// Return the JWT token
|
||||||
|
res.status(200).json(jwtToken);
|
||||||
console.log(jwtToken);
|
console.log(jwtToken);
|
||||||
} else {
|
} else {
|
||||||
console.log('Invalid password');
|
console.log('Invalid password');
|
||||||
|
@ -48,5 +64,5 @@ db.get(sql, [username], async (err, userRecord) => {
|
||||||
db.close();
|
db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json({ name: 'John Doe' });
|
// res.status(200).json({ name: 'John Doe' });
|
||||||
}
|
}
|
Loading…
Reference in New Issue