Skip to content

Commit

Permalink
fix: prevent infinite rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
2paperstar committed May 14, 2024
1 parent 3133e84 commit d87999d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/pages/Authorize/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const Authorize = () => {
const { t } = useTranslation();

useEffect(() => {
if (!clientData) return;
if (!clientData || !scopesNotConsented) return;
if (scopesNotConsented.length > 0) {
setScopesConsented(
clientData.recentConsent
Expand All @@ -64,7 +64,7 @@ const Authorize = () => {
return <Container>{error}</Container>;
}

if (!clientData) return null;
if (!clientData || !scopesNotConsented) return null;

if (scopesNotConsented.length > 0) {
return (
Expand Down
18 changes: 12 additions & 6 deletions src/pages/Authorize/useAuthorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ const useAuthorize = () => {
const { user, logout } = useAuth();
const navigate = useNavigate();
const [error, setError] = useState<string>();
const [scopesConsented, setScopesConsented] = useState<Scopes>([]);
const [scopesNotConsented, setScopesNotConsented] = useState<Scopes>([]);
const [scopesConsented, setScopesConsented] = useState<Scopes>();
const [scopesNotConsented, setScopesNotConsented] = useState<Scopes>();

const consent = (scopes: Scopes) => {
setScopesConsented((prev) => [...prev, ...scopes]);
setScopesConsented((prev) => [...(prev ?? []), ...scopes]);
setScopesNotConsented([]);
};

Expand All @@ -75,14 +75,20 @@ const useAuthorize = () => {
if (
data.prompt !== "consent" &&
data.prompt !== "login" &&
scopesConsented.length === 0 &&
!scopesConsented &&
clientData.recentConsent.length
) {
setScopesConsented(clientData.recentConsent);
return;
}
setScopesConsented(scopes.filter(isNotConsentRequiredScope));
setScopesNotConsented(scopes.filter(isConsentRequiredScope));
if (!scopesConsented) {
setScopesConsented(scopes.filter(isNotConsentRequiredScope));
return;
}
if (!scopesNotConsented) {
setScopesNotConsented(scopes.filter(isConsentRequiredScope));
return;
}

if (scopesNotConsented.length !== 0) return;
sessionStorage.removeItem(recentlyLoginKey);
Expand Down

0 comments on commit d87999d

Please sign in to comment.