Skip to content
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: 0 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import '@fontsource/geist-mono';
import '@fontsource/jetbrains-mono';

import wisp, { init } from "@renderdragonorg/wisp";
import { bindSupabase } from "@renderdragonorg/wisp/supabase";
import { supabase } from "@/integrations/supabase/client";

const convexUrl = import.meta.env.VITE_CONVEX_URL;
const wispSecret = import.meta.env.VITE_WISP_SECRET;

if (convexUrl) {
init({ convexUrl, wispSecret: wispSecret || undefined });
bindSupabase(supabase);
}

// expose for console debugging
Expand Down
39 changes: 39 additions & 0 deletions src/providers/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@ import { useState, useEffect } from "react";
import { User, Session } from "@supabase/supabase-js";
import { supabase } from "@/integrations/supabase/client";
import { AuthContext, AuthResult } from "@/providers/AuthContext";
import wisp from "@renderdragonorg/wisp";

function tryIdentify(userId: string, retries = 3) {
try { wisp.identify(userId); }
catch {
if (retries > 0) setTimeout(() => tryIdentify(userId, retries - 1), 500);
}
}
function tryReset() {
try { wisp.reset(); } catch { /* wisp not initialized */ }
}
function tryTrackIdentify(session: Session | null, retries = 3) {
const user = session?.user;
if (!user?.id) return;
try {
const meta = user.user_metadata as Record<string, unknown> || {};
const identities = (user.identities ?? []) as Array<{ provider?: string | null }>;
const primaryIdentity = identities.find((i) => i.provider === (user.app_metadata?.provider as string)) ?? identities[0];
wisp.track("session_identify", {
email: user.email,
name: meta.full_name as string | undefined,
provider: primaryIdentity?.provider ?? user.app_metadata?.provider ?? "email",
});
} catch {
if (retries > 0) setTimeout(() => tryTrackIdentify(session, retries - 1), 500);
}
}

export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const [user, setUser] = useState<User | null>(null);
const [session, setSession] = useState<Session | null>(null);
Expand All @@ -14,12 +42,23 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
setSession(session);
setUser(session?.user ?? null);
setLoading(false);

if (event === "SIGNED_IN" && session?.user.id) {
tryIdentify(session.user.id);
tryTrackIdentify(session);
} else if (event === "SIGNED_OUT") {
tryReset();
}
});

supabase.auth.getSession().then(({ data: { session } }) => {
setSession(session);
setUser(session?.user ?? null);
setLoading(false);
if (session?.user.id) {
tryIdentify(session.user.id);
tryTrackIdentify(session);
}
});

return () => subscription.unsubscribe();
Expand Down
Loading