mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
fix auth
This commit is contained in:
19
www/app/lib/useUserId.ts
Normal file
19
www/app/lib/useUserId.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user