Files
reflector/www/app/lib/useUserId.ts
Igor Loskutov 31c44ac0bb fix auth
2025-09-02 14:44:10 -04:00

20 lines
784 B
TypeScript

"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;
}