fix(web):update auth token consumers

This commit is contained in:
Anish Sarkar 2026-06-23 12:57:01 +05:30
parent 411bb0019e
commit 71045e552d
4 changed files with 173 additions and 103 deletions

View file

@ -1,8 +1,11 @@
"use client";
import { useAtomValue } from "jotai";
import { usePathname } from "next/navigation";
import { useEffect, useRef } from "react";
import { currentUserAtom } from "@/atoms/user/user-query.atoms";
import { useSession } from "@/hooks/use-session";
import { isPublicRoute } from "@/lib/auth-utils";
import { identifyUser, resetUser } from "@/lib/posthog/events";
/**
@ -12,7 +15,15 @@ import { identifyUser, resetUser } from "@/lib/posthog/events";
*
* This should be rendered inside the PostHogProvider.
*/
export function PostHogIdentify() {
function PostHogReset() {
useEffect(() => {
resetUser();
}, []);
return null;
}
function PostHogUserIdentify() {
const { data: user, isSuccess, isError } = useAtomValue(currentUserAtom);
const previousUserIdRef = useRef<string | null>(null);
@ -47,3 +58,27 @@ export function PostHogIdentify() {
// This component doesn't render anything
return null;
}
function SessionGatedPostHogIdentify() {
const session = useSession();
if (session.status === "loading") {
return null;
}
if (session.status === "unauthenticated") {
return <PostHogReset />;
}
return <PostHogUserIdentify />;
}
export function PostHogIdentify() {
const pathname = usePathname();
if (isPublicRoute(pathname)) {
return <PostHogReset />;
}
return <SessionGatedPostHogIdentify />;
}