Skip to content

Commit f03f10d

Browse files
authored
fix: multiple redirects throwing error when user not registered and no error shown to user. (#275)
1 parent fa4458e commit f03f10d

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

platforms/blabsy/src/components/layout/auth-layout.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState, useEffect, useRef } from 'react';
22
import { useRouter } from 'next/router';
33
import { useAuth } from '@lib/context/auth-context';
44
import { sleep } from '@lib/utils';
@@ -7,7 +7,6 @@ import type { LayoutProps } from './common-layout';
77

88
export function AuthLayout({ children }: LayoutProps): JSX.Element {
99
const [pending, setPending] = useState(true);
10-
1110
const { user, loading, error } = useAuth();
1211
const { replace } = useRouter();
1312

@@ -29,8 +28,13 @@ export function AuthLayout({ children }: LayoutProps): JSX.Element {
2928
}, [user, loading]);
3029

3130
// If there's an auth error (user not found), redirect to login
31+
const hasRedirected = useRef(false);
3232
useEffect(() => {
33-
if (error) void replace('/');
33+
if (error && !hasRedirected.current) {
34+
hasRedirected.current = true;
35+
alert(error.message || 'An error occurred');
36+
void replace('/');
37+
}
3438
}, [error, replace]);
3539

3640
if (loading || pending) return <Placeholder />;

platforms/blabsy/src/lib/hooks/useRequireAuth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useEffect } from 'react';
2-
import { useRouter } from 'next/router';
3-
import { useAuth } from '@lib/context/auth-context';
4-
import type { User } from '@lib/types/user';
1+
import { useAuth } from "@lib/context/auth-context";
2+
import type { User } from "@lib/types/user";
3+
import { useRouter } from "next/router";
4+
import { useEffect } from "react";
55

66
export function useRequireAuth(redirectUrl?: string): User | null {
77
const { user, loading } = useAuth();
88
const { replace } = useRouter();
99

1010
useEffect(() => {
11-
if (!loading && !user) void replace(redirectUrl ?? '/');
11+
if (!loading && !user) void replace(redirectUrl ?? "/");
1212
// eslint-disable-next-line react-hooks/exhaustive-deps
1313
}, [user, loading]);
1414

0 commit comments

Comments
 (0)