Skip to content

Commit

Permalink
Merge pull request #809 from supertokens/improve-sdk-logs
Browse files Browse the repository at this point in the history
chore: Add meta data to sdk logs
  • Loading branch information
rishabhpoddar authored Jul 3, 2024
2 parents 08a8363 + 3ac4ae4 commit cdcef3e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion v2/src/components/httpNetworking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ async function sendAnalyticsIfFrontTokenRemoved(url: string, frontTokenExists: b
}
}

function cookieExists(name: string) {
export function cookieExists(name: string) {
const cookies = document.cookie;
const regex = new RegExp("(^|; )" + encodeURIComponent(name) + "=");
return regex.test(cookies);
Expand Down
38 changes: 16 additions & 22 deletions v2/src/components/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import axios from "axios";

declare global {
interface Window {
Expand Down Expand Up @@ -145,28 +144,23 @@ export const saveSDKLogsConsoleOverride = (data: any, oldConsoleImplementation:
}
};

export async function sendSDKLogsToBackend() {
export async function sendSDKLogsToBackend(customData?: Record<string, any>) {
const sdkLogs = localStorage.getItem(SDK_LOGS_STORAGE_KEY) || "[]";
const parsedSDKLogs = JSON.parse(sdkLogs);

if (isDev()) {
console.log(parsedSDKLogs, "auth_error_sdk_logs");
} else {
await axios
.post(
"https://api.supertokens.com/0/antcs/ents",
{
eventName: "auth_error_sdk_logs",
data: {
version: "1",
userId: "1",
timestamp: Date.now(),
page: "",
type: "auth_error_sdk_logs",
logs: parsedSDKLogs,
},
},
{}
)
}
getAnalytics().then((stAnalytics: any) => {
if (stAnalytics === undefined) {
console.log("mocked event send:", "auth_error_sdk_logs", parsedSDKLogs);
return;
}
stAnalytics.sendEvent(
'auth_error_sdk_logs',
{
type: 'auth_error_sdk_logs',
logs: parsedSDKLogs,
...customData
},
'v1'
)
});
}
24 changes: 22 additions & 2 deletions v2/src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Head from '@docusaurus/Head';
import { useLocation } from '@docusaurus/router';
import './styles.css';
import supertokens from "supertokens-website";
import {overrideConsoleImplementation,saveSDKLogsConsoleOverride} from '../../components/utils'
import {overrideConsoleImplementation,saveSDKLogsConsoleOverride, sendSDKLogsToBackend} from '../../components/utils'
import {cookieExists} from '../../components/httpNetworking'
import styles from "./styles.module.css";


Expand All @@ -37,7 +38,26 @@ if (typeof window !== 'undefined') {
supertokens.init({
apiDomain: API_DOMAIN,
apiBasePath: API_BASE_PATH,
enableDebugLogs:true,
enableDebugLogs: true,
cookieHandler: (original) => {
return {
...original,
setCookie: (cookieString) => {
const cookieName = cookieString.split(";")[0].split("=")[0];
if (cookieName === "sFrontToken") {
let cookieValue = cookieString.split(";")[0].split("=")[1].trim();
if (cookieValue === "" && cookieExists("sFrontToken")) {
const stack = new Error().stack;
sendSDKLogsToBackend({
stack,
title: "front_token_cookie_removed",
});
}
}
return original.setCookie(cookieString);
},
};
},
sessionExpiredStatusCode,
preAPIHook: async (context) => {
return {
Expand Down

0 comments on commit cdcef3e

Please sign in to comment.