mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 12:19:06 +00:00
* fix: unattended component reload due to session changes When the session is updated, status goes back to loading then authenticated or unauthenticated. session.accessTokenExpires may also be updated. This triggered various component refresh at unattented times, and brake the user experience. By splitting to only what's needed with memoization, it will prevent unattented refresh. * review * review: change syntax
34 lines
734 B
TypeScript
34 lines
734 B
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
import { useSession as useNextAuthSession } from "next-auth/react";
|
|
import { Session } from "next-auth";
|
|
|
|
// user type with id, name, email
|
|
export interface User {
|
|
id?: string | null;
|
|
name?: string | null;
|
|
email?: string | null;
|
|
}
|
|
|
|
export default function useSessionUser() {
|
|
const { data: session } = useNextAuthSession();
|
|
const [user, setUser] = useState<User | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!session?.user) {
|
|
setUser(null);
|
|
return;
|
|
}
|
|
if (JSON.stringify(session.user) !== JSON.stringify(user)) {
|
|
setUser(session.user);
|
|
}
|
|
}, [session]);
|
|
|
|
return {
|
|
id: user?.id,
|
|
name: user?.name,
|
|
email: user?.email,
|
|
};
|
|
}
|