mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
20 lines
784 B
TypeScript
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;
|
|
}
|