This commit is contained in:
Igor Loskutov
2025-09-02 14:44:10 -04:00
parent 5ffc312d4a
commit 31c44ac0bb
11 changed files with 52 additions and 78 deletions

19
www/app/lib/useUserId.ts Normal file
View File

@@ -0,0 +1,19 @@
"use client";
import { useState, useEffect } from "react";
import { useSession as useNextAuthSession } from "next-auth/react";
import { Session } from "next-auth";
import { useAuth } from "./AuthProvider";
const assertUserId = <T>(u: T): T & { id: string } => {
if (typeof (u as { id: string }).id !== "string")
throw new Error("Expected user.id to be a string");
return u as T & { id: string };
};
// the current assumption in useSessionUser is that "useNextAuthSession" also returns user.id, although useNextAuthSession documentation states it doesn't
// the hook is to isolate the potential impact and to document this behaviour
export default function useUserId() {
const auth = useAuth();
return auth.status === "authenticated" ? assertUserId(auth.user) : null;
}