Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth race condition #167

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/client/src/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { analyticsReady } from '../events';
import { initAmplitude, logAmplitudeEvent, logPageView } from './amplitude';
import { AnalyticsEventArgs } from './constants';
import {
Expand All @@ -8,6 +9,7 @@ import {
export const initAnalytics = () => {
initAmplitude();
initTaskAnalytics();
dispatchEvent(analyticsReady);
};

// Connects to partytown forwarding
Expand Down Expand Up @@ -37,6 +39,7 @@ window.analyticsEvent = function (props: AnalyticsEventArgs) {
'decorator_next',
);
};

window.logPageView = logPageView;
window.logAmplitudeEvent = logAmplitudeEvent;
window.startTaskAnalyticsSurvey = startTaskAnalyticsSurvey;
Expand Down
11 changes: 11 additions & 0 deletions packages/client/src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Auth } from "./api"

export const analyticsReady = new CustomEvent('analytics-ready-event', {
bubbles: true,
})

export type AnalyticsLoaded = CustomEvent<Auth>

export const analyticsLoaded = new CustomEvent<Auth>('analytics-loaded-event', {
bubbles: true,
})
18 changes: 16 additions & 2 deletions packages/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import './views/search-input';
import './views/search-menu';
import { Auth } from './api';
import { addFaroMetaData } from './faro';
import { analyticsLoaded, analyticsReady } from './events';

import.meta.glob('./styles/*.css', { eager: true });

Expand Down Expand Up @@ -134,11 +135,16 @@ async function populateLoggedInMenu(authObject: Auth) {

//
// await populateLoggedInMenu(response);

const init = async () => {
const response = await api.checkAuth();

window.logPageView(window.__DECORATOR_DATA__.params, response);
window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);
dispatchEvent(
new CustomEvent(analyticsLoaded.type, {
bubbles: true,
detail: { response },
})
);

if (!response.authenticated) {
return;
Expand All @@ -149,6 +155,14 @@ const init = async () => {

init();

window.addEventListener(analyticsReady.type, () => {
window.addEventListener(analyticsLoaded.type, (e) => {
const response = (e as CustomEvent<Auth>).detail;
window.logPageView(window.__DECORATOR_DATA__.params, response);
window.startTaskAnalyticsSurvey(window.__DECORATOR_DATA__);
});
});

function handleLogin() {
const loginLevel = window.__DECORATOR_DATA__.params.level || 'Level4';
document.getElementById('login-button')?.addEventListener('click', async () => {
Expand Down
7 changes: 1 addition & 6 deletions packages/server/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,10 @@ const getEnvAssets = async () => {
[
'http://localhost:5173/@vite/client',
`http://localhost:5173/${entryPointPath}`,
`http://localhost:5173/${entryPointPathAnalytics}`,
]
.map(script)
.join(''),
[
vendorScripts.taskAnalytics,
hostUrl(`/public/${manifest[entryPointPathAnalytics].file}`),
]
.map(asyncScript)
.join(''),
[inlineVendorScripts.hotjar].map(partytownInlineScript).join(''),
].join(''),
};
Expand Down
Loading