Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions platforms/blabsy/src/components/layout/auth-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef } from 'react';
import { useRouter } from 'next/router';
import { useAuth } from '@lib/context/auth-context';
import { sleep } from '@lib/utils';
Expand All @@ -7,7 +7,6 @@ import type { LayoutProps } from './common-layout';

export function AuthLayout({ children }: LayoutProps): JSX.Element {
const [pending, setPending] = useState(true);

const { user, loading, error } = useAuth();
const { replace } = useRouter();

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

// If there's an auth error (user not found), redirect to login
const hasRedirected = useRef(false);
useEffect(() => {
if (error) void replace('/');
if (error && !hasRedirected.current) {
hasRedirected.current = true;
alert(error.message || 'An error occurred');
void replace('/');
}
}, [error, replace]);

if (loading || pending) return <Placeholder />;
Expand Down
10 changes: 5 additions & 5 deletions platforms/blabsy/src/lib/hooks/useRequireAuth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { useAuth } from '@lib/context/auth-context';
import type { User } from '@lib/types/user';
import { useAuth } from "@lib/context/auth-context";
import type { User } from "@lib/types/user";
import { useRouter } from "next/router";
import { useEffect } from "react";

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

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

Expand Down
Loading