Skip to content

Commit

Permalink
Merge pull request #2071 from Nuzhy-Deriv/nuzhy/prevent-loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhy-Deriv authored Oct 18, 2024
2 parents a51faad + c4d5af6 commit 109b6e0
Showing 1 changed file with 9 additions and 71 deletions.
80 changes: 9 additions & 71 deletions public/scripts/freshchat-temp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const getJWT = async (hostname, uuid, getTokenForWS, callDerivWS) => {
let extra_fields = {};
if (uuid) {
console.log("Setting UUID in JWT request:", uuid);
extra_fields.freshchat_uuid = uuid;
}
const token = await getTokenForWS();
console.log("Token:", token);
let jwt;
const result = await callDerivWS(
hostname,
Expand All @@ -16,12 +14,8 @@ const getJWT = async (hostname, uuid, getTokenForWS, callDerivWS) => {
},
token
);
console.log("JWTResponse:", result);
jwt = result.service_token.freshworks_user_jwt.token;
jwt = result?.service_token?.freshworks_user_jwt?.token;

console.log("JWT:", jwt, "=>", parseJwt(jwt));

console.log("Using deriv JWT");
return jwt;
};

Expand All @@ -45,13 +39,12 @@ const parseJwt = (jwt) => {
const callDerivWS = async (hostname, params, token) => {
return new Promise((resolve, reject) => {
const wsUri = `wss://${hostname}/websockets/v3?app_id=1&l=EN&brand=deriv`;
console.log("Connecting to ", wsUri);
const ws = new WebSocket(wsUri);
let next_id = 1;
let requests = {};
let isAuthorized = false;

ws.addEventListener("error", (e) => {
console.error("deriv error " + hostname, e);
ws.close();
reject("Error connecting to deriv WS " + hostname);
});
Expand All @@ -61,48 +54,42 @@ const callDerivWS = async (hostname, params, token) => {
msg.req_id = next_id++;
}
requests[msg.req_id] = { start: new Date().getTime(), msg: msg };
console.log("deriv sending: ", msg);
ws.send(JSON.stringify(msg));
};

ws.addEventListener("close", function close() {
console.log("closed ws deriv" + hostname);
reject("Deriv WS unexpected close" + hostname);
});

ws.addEventListener("open", function open() {
console.log("connected to deriv " + hostname);
send({ authorize: token });
});

ws.addEventListener("message", function message(data) {
if (typeof data === "object") {
let jsonStr = data.data;
let json = JSON.parse(jsonStr);
if (typeof json === "object" && "authorize" in json) {
send(params);
return;
if (typeof json === "object" && "authorize" in json && !isAuthorized) {
isAuthorized = true; // Prevents reauthorization
send(params); // Send params after first authorization
} else {
resolve(json);
ws.close();
return;
}
} else {
reject("Unexpected message from deriv WS " + hostname);
}

console.log("deriv got unexpected: ", data + "");
reject("Unexpected message from deriv WS " + this.hostname);
});
});
};

class FreshChat {
tokenForWS = undefined;
hostname = "green.derivws.com";
appId = 1;
hostname = localStorage.getItem("config.server_url");
appId = localStorage.getItem("config.app_id");

constructor({ token = null, hideButton = false } = {}) {
this.authToken = token;
// this.locale = locale;
this.hideButton = hideButton;
this.init();
}
Expand Down Expand Up @@ -161,7 +148,6 @@ class FreshChat {
}

if (/^a1-.{29,29}$/.test(this.authToken)) {
console.log("Valid token: ", this.authToken);
this.tokenForWS = { token: this.authToken };
resolve(this.authToken);
} else if (/./.test(this.authToken)) {
Expand Down Expand Up @@ -189,7 +175,6 @@ class FreshChat {
domainParts.shift();
}
}
console.log("All cookies for the current domain have been cleared.");
};

callDerivWS = async (hostname, params, token) => {
Expand All @@ -201,71 +186,25 @@ window.FreshChat = FreshChat;

window.fcSettings = {
onInit: function () {
// function authenticateUser(userData) {
// let authenticateCB = async (uuid) => {
// // Signed UUID Hardcoded. Call Customer backend and generate the signed uuid from uuid

// let signedUUID = await getJWT(
// "qa179.deriv.dev",
// uuid,
// () => null,
// callDerivWS
// );
// console.log("signedUUID", signedUUID);
// window.fcWidget.authenticate(signedUUID);
// };

// if (userData && userData.freshchat_uuid) {
// authenticateCB(userData.freshchat_uuid);
// } else {
// // Generate UUID and create new user
// window.fcWidget.user.getUUID().then((resp) => {
// let uuid = resp && resp.data && resp.data.uuid;

// if (uuid) {
// authenticateCB(uuid);
// }
// });
// }
// }

// window.fcWidget.on("frame:statechange", function (data) {
// if (
// data.success === false &&
// data.data.frameState === "not_authenticated"
// ) {
// authenticateUser(data);
// }
// });

window.fcWidget.on("user:statechange", function (data) {
if (data.success) {
let userData = data.data;

console.log({
userData,
});

// authenticate user success
if (userData) {
if (userData.userState === "authenticated") {
console.log("User Authenticated");
}

if (userData.userState === "created") {
console.log("User Created");
}

if (userData.userState === "loaded") {
console.log("User Loaded");
}

if (userData.userState === "identified") {
console.log("User Identified");
}

if (userData.userState === "restored") {
console.log("User Restored");
}
}
} else {
Expand All @@ -277,7 +216,6 @@ window.fcSettings = {
userData.userState === "not_created" ||
userData.userState === "not_authenticated"
) {
// authenticateUser(userData);
}
}
}
Expand Down

0 comments on commit 109b6e0

Please sign in to comment.