From 90d8e39eb3b83655e3e15ff011075010e1d652c3 Mon Sep 17 00:00:00 2001 From: counterweight Date: Fri, 14 Feb 2025 01:56:41 +0100 Subject: [PATCH] also set nym --- src/models/NymSet.js | 27 +++++++++++++++++++++++ src/public/javascript/createProfile.js | 11 ++++++++++ src/routes/apiRoutes.js | 30 +++++++++++++++++++++++++- src/services/profileService.js | 17 +++++++++++++-- 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/models/NymSet.js diff --git a/src/models/NymSet.js b/src/models/NymSet.js new file mode 100644 index 0000000..1adb37a --- /dev/null +++ b/src/models/NymSet.js @@ -0,0 +1,27 @@ +const { DataTypes } = require('sequelize'); +const sequelize = require('../database'); + +const NymSet = sequelize.define('NymSet', { + uuid: { + type: DataTypes.UUID, + allowNull: false, + unique: true, + primaryKey: true + }, + public_key: { + type: DataTypes.STRING, + allowNull: false + }, + nym: { + type: DataTypes.TEXT, + allowNull: false + }, + created_at: { + type: DataTypes.DATE, + allowNull: false + } +}, { + tableName: 'nym_set' +}); + +module.exports = NymSet; \ No newline at end of file diff --git a/src/public/javascript/createProfile.js b/src/public/javascript/createProfile.js index 7292f3d..e5a6b7b 100644 --- a/src/public/javascript/createProfile.js +++ b/src/public/javascript/createProfile.js @@ -88,6 +88,17 @@ window.onload = () => { }, body: JSON.stringify({ encryptedContactDetails }) }); + + const nym = document.querySelector('#nym-input').value; + await fetch('/api/set-nym', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ nym }) + }); + } ); }; \ No newline at end of file diff --git a/src/routes/apiRoutes.js b/src/routes/apiRoutes.js index b7f3fc6..21870e9 100644 --- a/src/routes/apiRoutes.js +++ b/src/routes/apiRoutes.js @@ -115,6 +115,34 @@ router.post( success: true, message: 'Contact details set successfully.' }) - }) + } +); + +router.post( + "/set-nym", + rejectIfNotAuthorizedMiddleware, + attachPublicKeyMiddleware, + async (req, res) => { + const nym = req.body.nym; + const publicKey = req.cookies.publicKey; + + if (!nym) { + return res.status(400).json({ + success: false, + message: 'Missing nym' + }) + } + + await profileService.setNym( + publicKey, + nym + ) + + return res.status(200).json({ + success: true, + message: 'Nym set successfully.' + }) + } +); module.exports = router; diff --git a/src/services/profileService.js b/src/services/profileService.js index f550bc4..0e4dada 100644 --- a/src/services/profileService.js +++ b/src/services/profileService.js @@ -1,8 +1,9 @@ const uuid = require('uuid'); const ContactDetailsSet = require('../models/ContactDetailsSet'); +const NymSet = require('../models/NymSet'); async function setContactDetails(publicKey, encryptedContactDetails) { - return ContactDetailsSet.create( + return await ContactDetailsSet.create( { 'uuid': uuid.v7(), public_key: publicKey, @@ -12,6 +13,18 @@ async function setContactDetails(publicKey, encryptedContactDetails) { ) } +async function setNym(publicKey, nym) { + return await NymSet.create( + { + 'uuid': uuid.v7(), + public_key: publicKey, + nym: nym, + created_at: new Date().toISOString() + } + ) +} + module.exports = { - setContactDetails + setContactDetails, + setNym }; \ No newline at end of file