From a14f0c212ee67131cf6b18d0279d9486596b2cdf Mon Sep 17 00:00:00 2001 From: MantasMikal Date: Sat, 23 Jan 2021 11:00:06 +0000 Subject: [PATCH] feat: measure auth duration --- helpers/db.js | 1 + models/user.js | 14 ++++++++++---- routes/register.js | 9 ++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/helpers/db.js b/helpers/db.js index 8461a53..66c0332 100644 --- a/helpers/db.js +++ b/helpers/db.js @@ -4,6 +4,7 @@ mongoose.set('useCreateIndex', true); mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true, + useFindAndModify: false }); const db = mongoose.connection; diff --git a/models/user.js b/models/user.js index bc3c526..59618f1 100644 --- a/models/user.js +++ b/models/user.js @@ -1,11 +1,11 @@ -import mongoose from '../helpers/db.js' +import mongoose from "../helpers/db.js"; const User = mongoose.model( "User", new mongoose.Schema({ id: { type: Buffer, - unique: true + unique: true, }, credentialId: { type: String, @@ -21,8 +21,14 @@ const User = mongoose.model( email: { type: String, unique: true, - } + }, + authDuration: { + type: Number, + }, + regDuration: { + type: Number, + }, }) ); -export default User +export default User; diff --git a/routes/register.js b/routes/register.js index c02d243..e39795a 100644 --- a/routes/register.js +++ b/routes/register.js @@ -52,7 +52,7 @@ router.post("/registration-options", async (req, res) => { }); router.post("/register", async (req, res) => { - const { credential, email, firstName, lastName } = req.body; + const { credential, email, firstName, lastName, regDuration } = req.body; const challenge = new Uint8Array(req.session.challenge.data).buffer; const base64RawId = credential.rawId; @@ -85,9 +85,11 @@ router.post("/register", async (req, res) => { firstName, lastName, email, + regDuration: regDuration, }); + user.save(); - console.log('Created new account for: ', email) + console.log("Created new account for: ", email); res.json({ status: "ok" }); } catch (e) { @@ -112,7 +114,7 @@ router.post("/authentication-options", async (req, res) => { }); router.post("/authenticate", async (req, res) => { - const { credential } = req.body; + const { credential, authDuration, email } = req.body; credential.rawId = new Uint8Array(Buffer.from(credential.rawId, "base64")).buffer; @@ -133,6 +135,7 @@ router.post("/authenticate", async (req, res) => { }; try { await fido.assertionResult(credential, assertionExpectations); + await User.findOneAndUpdate({ email: email }, { authDuration: authDuration }); res.json({ status: "ok" }); } catch (e) { console.log(e);