Skip to content

Commit

Permalink
fix: Refactor authentication flow for CoCreateUser
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
frankpagan committed May 21, 2023
1 parent 2a1e876 commit 25bd324
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
13 changes: 7 additions & 6 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,23 @@ const CoCreateUser = {

let request = {
collection,
document: {
lastSignIn: new Date().toISOString()
},
// document: {
// lastSignIn: new Date().toISOString()
// },
filter: {
query
}
}

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)
Expand All @@ -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";
Expand Down
31 changes: 13 additions & 18 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 25bd324

Please sign in to comment.