From 25bd324bd04d0c196161f4e93b48589c80acee41 Mon Sep 17 00:00:00 2001 From: frankpagan Date: Sun, 21 May 2023 18:24:43 +0000 Subject: [PATCH] fix: Refactor authentication flow for CoCreateUser This commit removes document updates and replaces them with reads. It also adds a new user_id field to the response to use for token generation. Finally, some unused code blocks have been commented out in this implementation. --- src/client.js | 13 +++++++------ src/server.js | 31 +++++++++++++------------------ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/client.js b/src/client.js index 784d86e..b6c9a6d 100644 --- a/src/client.js +++ b/src/client.js @@ -95,9 +95,9 @@ const CoCreateUser = { let request = { collection, - document: { - lastSignIn: new Date().toISOString() - }, + // document: { + // lastSignIn: new Date().toISOString() + // }, filter: { query } @@ -105,12 +105,13 @@ const CoCreateUser = { const socket = crud.socket.getSockets() if (!socket[0] || !socket[0].connected || window && !window.navigator.onLine || crud.socket.serverOrganization == false) { - crud.updateDocument(request).then((response) => { + crud.readDocument(request).then((response) => { response['success'] = false response['status'] = "signIn failed" if (response.document && response.document[0]) { response['success'] = true response['status'] = "success" + response['user_id'] = response.document[0].key this.signInResponse(response) } else { this.signInResponse(response) @@ -125,13 +126,13 @@ const CoCreateUser = { }, signInResponse: function (data) { - let { success, status, message, token } = data; + let { success, status, message, user_id, token } = data; if (success) { localStorage.setItem('organization_id', crud.socket.config.organization_id); localStorage.setItem("key", crud.socket.config.key); localStorage.setItem("host", crud.socket.config.host); - localStorage.setItem('user_id', data.document[0]['_id']); + localStorage.setItem('user_id', user_id); localStorage.setItem("token", token); // document.cookie = `token=${token};path=/`; message = "Succesful signIn"; diff --git a/src/server.js b/src/server.js index 1690d2a..0979f8d 100644 --- a/src/server.js +++ b/src/server.js @@ -48,41 +48,36 @@ class CoCreateUser { async signIn(socket, data) { const self = this; try { - data.collection = 'keys' - this.crud.updateDocument(data).then(async (data) => { + this.crud.readDocument(data).then(async (data) => { let response = { - ...data, success: false, message: "signIn failed", status: "failed", userStatus: 'off' } - if (data.document[0] && data.document[0]._id) { - let token = null; - if (self.wsManager.authInstance) { - token = await self.wsManager.authInstance.generateToken({ user_id: data.document[0]._id }); - } + if (data.document[0] && data.document[0]._id && self.wsManager.authInstance) { + const user_id = data.document[0].key + const token = await self.wsManager.authInstance.generateToken({ user_id }); - if (token && token != 'null') + if (token && token != 'null') { response = { - ...response, success: true, message: "signIn successful", status: "success", userStatus: 'on', + user_id, token }; - - if (data.organization_id != process.env.organization_id) { - let Data = { organization_id: process.env.organization_id } - Data.document['_id'] = data.document[0]._id - Data.document['lastsignIn'] = data.document[0].lastsignIn - Data.document['organization_id'] = process.env.organization_id - crud.updateDocument(Data) + // if (data.organization_id != process.env.organization_id) { + // let Data = { organization_id: process.env.organization_id } + // Data.document['_id'] = data.document[0]._id + // Data.document['lastsignIn'] = data.document[0].lastsignIn + // Data.document['organization_id'] = process.env.organization_id + // crud.updateDocument(Data) + // } } - } self.wsManager.send(socket, 'signIn', response) self.wsManager.broadcast(socket, 'updateUserStatus', {