Skip to content

Commit

Permalink
Merge pull request #824 from supertokens/fix/remove-sdk-logs
Browse files Browse the repository at this point in the history
Chore: Remove sdk logs
  • Loading branch information
rishabhpoddar authored Aug 1, 2024
2 parents efe931c + 937000c commit 47714dd
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 221 deletions.
2 changes: 1 addition & 1 deletion v2/src/components/customAdmonition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function CustomAdmonition(props: PropsWithChildren<{
<h5>
<span className="admonition-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path>
<path fillRule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path>
</svg>
</span>
{allowedTypes[props.type]}
Expand Down
151 changes: 17 additions & 134 deletions v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";
import { getAnalytics, sendSDKLogsToBackend } from "./utils";
import { getUserInformation } from "./api/user/info";
import { getHttpNetworkingSDKLogsHooks } from "./sdklogsutils";

export enum HTTP_REQUEST_ERROR {
SESSION_EXPIRED,
Expand Down Expand Up @@ -102,11 +101,12 @@ export async function simpleGETRequest(url: string, userConfig: any = {}, versio
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
const sdkLogsHooks = getHttpNetworkingSDKLogsHooks();
sdkLogsHooks.preApiExecutionHook();
let response = await axios.get(url, userConfig);
let data = await response.data;
let headers = response.headers;
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
sdkLogsHooks.postApiExecutionHook(url, headers);
return { data, headers };
}

Expand All @@ -122,11 +122,12 @@ export async function simplePOSTRequest(url: string, data: any, userConfig: POST
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
const sdkLogsHooks = getHttpNetworkingSDKLogsHooks();
sdkLogsHooks.preApiExecutionHook();
let response = await axios.post(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
sdkLogsHooks.postApiExecutionHook(url, headers);
return { data: responseData, headers, status: response.status, statusText: response.statusText };
}

Expand All @@ -142,11 +143,12 @@ export async function simplePATCHRequest(url: string, data: any, userConfig: PAT
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
const sdkLogsHooks = getHttpNetworkingSDKLogsHooks();
sdkLogsHooks.preApiExecutionHook();
let response = await axios.patch(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
sdkLogsHooks.postApiExecutionHook(url, headers);
return { data: responseData, headers };
}

Expand All @@ -162,11 +164,12 @@ export async function simplePUTRequest(url: string, data: any, userConfig: POSTR
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
const sdkLogsHooks = getHttpNetworkingSDKLogsHooks();
sdkLogsHooks.preApiExecutionHook();
let response = await axios.put(url, data, userConfig);
let responseData = response.data;
let headers = response.headers;
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
sdkLogsHooks.postApiExecutionHook(url, headers);
return { data: responseData, headers };
}

Expand All @@ -185,132 +188,12 @@ export async function simpleDELETERequest(url: string, userConfig: DELETERequest
"api-version": version + ""
}
};
let frontTokenExists = cookieExists("sFrontToken");
const sdkLogsHooks = getHttpNetworkingSDKLogsHooks();
sdkLogsHooks.preApiExecutionHook();
delete userConfig.params;
let response = await axios.delete(url, userConfig);
let data = await response.data;
let headers = response.headers;
await sendAnalyticsIfFrontTokenRemoved(url, frontTokenExists, headers);
sdkLogsHooks.postApiExecutionHook(url, headers);
return { data, headers };
}

async function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: boolean, headers: any) {
if (!frontTokenExists) {
return;
}
let updatedFrontTokenExists = cookieExists("sFrontToken");
if (!updatedFrontTokenExists) {
// this means it was removed between the api call!
// send analytics
sendAuthAnalytics("front_token_removed", {
url,
headers
});
await sendSDKLogsToBackend()
}
}

export function cookieExists(name: string) {
const cookies = document.cookie;
const regex = new RegExp("(^|; )" + encodeURIComponent(name) + "=");
return regex.test(cookies);
}

const sendAuthAnalytics = (eventName: string, payload: Record<string, unknown>, version = "v1") => {
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log("mocked event send:", eventName, version, payload);
return;
}
stAnalytics.sendEvent(
eventName,
{
type: "auth",
...payload
},
version
);
});
};

function getCookieValue(cookieName: string) {
const cookies = document.cookie;
const cookieArray = cookies.split(';');
for (let i = 0; i < cookieArray.length; i++) {
const cookie = cookieArray[i].trim();
if (cookie.startsWith(cookieName + '=')) {
return cookie.substring(cookieName.length + 1);
}
}
return null;
}

export async function checkForDesyncedSession() {
const EVENT_NAME = 'desynced_session_state';
const didFrontTokenExistBeforeAPICall = cookieExists("sFrontToken");

try {
await getUserInformation();
const doesFrontendTokenExistAfterAPICall = cookieExists("sFrontToken");
if (!doesFrontendTokenExistAfterAPICall) {
const payload = {
didFrontTokenExistBeforeAPICall,
stLastAccessTokenUpdate: getCookieValue("st-last-access-token-update"),
statusCode: 200
};
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log('mocked event send:', EVENT_NAME, 'v1', payload);
return;
}
stAnalytics.sendEvent(
EVENT_NAME,
{
type: EVENT_NAME,
...payload,
},
'v1'
);
});
}
} catch (e:any) {
if (
"response" in e &&
e.response.status === 401 &&
e.response.data &&
e.response.data.message === "try refresh token"
) {
if (!cookieExists("sFrontToken")) {
const payload = {
didFrontTokenExistBeforeAPICall,
stLastAccessTokenUpdate: getCookieValue("st-last-access-token-update"),
statusCode: 401
};
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log("mocked event send:", EVENT_NAME, "v1", payload);
return;
}
stAnalytics.sendEvent(
EVENT_NAME,
{
type: EVENT_NAME,
...payload
},
"v1"
);
});
}
}
}
}

export function historyPushStateOverride(onPush: () => void) {
const originalPushState = history.pushState;
history.pushState = function (...args) {
const result = originalPushState.apply(this, args);
onPush();
return result;
};
}

}
Loading

0 comments on commit 47714dd

Please sign in to comment.