mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 04:39:06 +00:00
fix: remove infinite re-render loop in useSessionAccessToken
The hook was maintaining redundant local state that caused re-renders on every update, which triggered NextAuth to continuously refetch the session, resulting in hundreds of POST requests to /api/auth/session. Simplified the hook to directly return session values without unnecessary state duplication.
This commit is contained in:
@@ -1,42 +1,15 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
|
||||||
import { useSession as useNextAuthSession } from "next-auth/react";
|
import { useSession as useNextAuthSession } from "next-auth/react";
|
||||||
import { CustomSession } from "./types";
|
import { CustomSession } from "./types";
|
||||||
|
|
||||||
export default function useSessionAccessToken() {
|
export default function useSessionAccessToken() {
|
||||||
const { data: session } = useNextAuthSession();
|
const { data: session } = useNextAuthSession();
|
||||||
const customSession = session as CustomSession;
|
const customSession = session as CustomSession;
|
||||||
const naAccessToken = customSession?.accessToken;
|
|
||||||
const naAccessTokenExpires = customSession?.accessTokenExpires;
|
|
||||||
const naError = customSession?.error;
|
|
||||||
const [accessToken, setAccessToken] = useState<string | null>(null);
|
|
||||||
const [accessTokenExpires, setAccessTokenExpires] = useState<number | null>(
|
|
||||||
null,
|
|
||||||
);
|
|
||||||
const [error, setError] = useState<string | undefined>();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (naAccessToken !== accessToken) {
|
|
||||||
setAccessToken(naAccessToken);
|
|
||||||
}
|
|
||||||
}, [naAccessToken]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (naAccessTokenExpires !== accessTokenExpires) {
|
|
||||||
setAccessTokenExpires(naAccessTokenExpires);
|
|
||||||
}
|
|
||||||
}, [naAccessTokenExpires]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (naError !== error) {
|
|
||||||
setError(naError);
|
|
||||||
}
|
|
||||||
}, [naError]);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessToken,
|
accessToken: customSession?.accessToken ?? null,
|
||||||
accessTokenExpires,
|
accessTokenExpires: customSession?.accessTokenExpires ?? null,
|
||||||
error,
|
error: customSession?.error,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user