From a538f2312426d4f3323cde600256a28f2ecf09ad Mon Sep 17 00:00:00 2001 From: Kar Date: Mon, 15 May 2023 17:57:16 +0530 Subject: [PATCH] login --- appUser.db | Bin 12288 -> 0 bytes data/appUser.db | Bin 24576 -> 24576 bytes mydatabase.db | Bin 8192 -> 0 bytes src/pages/api/login.js | 32 ++++++++++++++++++++++++-------- 4 files changed, 24 insertions(+), 8 deletions(-) delete mode 100644 appUser.db delete mode 100644 mydatabase.db diff --git a/appUser.db b/appUser.db deleted file mode 100644 index 4a36550609db28890b465022096f0009a55a238a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI0-A)rx5XW~5+6!Gu)8M8dhHVImBtmGn#dsxDYXqxCX`**z+C8)j`<1h2HNAv{ zSMVKt7$3n0@GYEe_w-{0g>oZiPO{rMbH4ul&9up!gZE8aFyqMQE(pU^78O-hb__#N z&{UK;d}Pa@F)us#sZQ9JnTyKi(N7IWGupbc@D*RwhyW2F0z`la5CI}U1c(3;AOin7 zfwSXGR@Zg)+!WAuSQIi|8NBA6e`xHs8b)ik-fS2Hm+{E9j8@}Q>#-4e;IhOH1&An= z1GqtAihdxc^chGm&gSCK^*zxWuSbssSg#E?XP!0L4IJ>2krM~xLFnuz-k|4ua%}*i zoB>`0j@%Ns(?1-Z9dN(P!5!%rzGXR#%VoAF7>5(#%cU;Za+{7Hd4l(ok@$ zZm=xQMk>Ni&-aJx*U!3YR$p3D&&wm&G1$y+sEPCqNyoK$_TCRA*IoF&u%Ug^7Vm$- zE;S-R1c(3;AOb{y2oM1xKm=|Zfm1zO)QT%wQ7e>6T0vXY3QL8;YCf+&D-G&WGl<%b z9rlyE}T1hbI^zb?%j_F8V;+za=Z7QTs z<7VJvnurX;E6UT->JZ0LOhoPBTpG?WpH`E6+8&)g%%}axyEtm*L_Xo+aXw+pQ0w+s zLj6Up)&vJ{Ky2Ovah0snGZ*WV`Cdusn}l<i^sJr%rl_01+SpM1Tko0U|&Ih`=QTsQ#bfVuI>_d6`4?KVFKY?z{fC`u`UOZe76u diff --git a/data/appUser.db b/data/appUser.db index b9e61c8086f8e7bdd9f2fbda5ba492b4287a2946..c593af39043172585d912361d2cacec4e51dbe0f 100644 GIT binary patch delta 242 zcmZoTz}Rqrae_2s`a~IL)^rBFcs1nq()06- zGE-E_Yz-|{a&1ivEX{OEY>jmaYz=TwEOLEwBFf84(^X1C3*9p; zqXM(t+zg$Ij6m1iC zE)y;m&P$vNHmeAzbBYDKNis0VGe|P8My)g DR4!jj diff --git a/mydatabase.db b/mydatabase.db deleted file mode 100644 index 780e10475caa376302ff823c16be78b9607431f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8192 zcmeI#u};G<5C&koLSh23RIDCBLP&@gprCABfDCNaQM+x_#EzWHkQIqn;2n7jY$aE^ zv7_ILmH2#TJKyHl=VistY(i{^&Aqx%TB`?Rlu|nNS?bdA_(TTjoyUJQt!_8pXDLgc zf2s7KK>z{}fB*y_009U<00Izz00fQ%KDPSwaz59ecTDfK>pK^7pFO=4i?T4~;&EA+ z95GjwHD&Qu-k9D~36h^2Hh9t)kf$HTTJ zKh5lGl8sVuJ4M@F@KW1SCmG~>s%1%v^{^WEZe!R+Y7Xb;U~TQ9lvSQxq&){IZK#rE z)}i+thiNANxa_;GSi)p&bca)P>s<(A{dK;Zr%OK?1Rwwb2tWV=5P$##AOHafKmY>& GSl|awTYeG% diff --git a/src/pages/api/login.js b/src/pages/api/login.js index a1c7215..c3f5cdd 100644 --- a/src/pages/api/login.js +++ b/src/pages/api/login.js @@ -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' }); } \ No newline at end of file