-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-redirect-handler.tsx
More file actions
45 lines (39 loc) · 1.3 KB
/
auth-redirect-handler.tsx
File metadata and controls
45 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use client';
import { useEffect, useRef, useState } from 'react';
import { SignInButton } from '@clerk/nextjs';
import { useSearchParams } from 'next/navigation';
import { isAuthEnabled } from '@/app/env';
/**
* Component to handle authentication redirects
* This creates a professional experience by automatically handling auth redirects
*/
export function AuthRedirectHandler() {
const searchParams = useSearchParams();
const redirectUrl = searchParams.get('redirect_url');
const [authEnabled] = useState(isAuthEnabled());
// Create a ref to store the button element
const buttonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
// If auth is enabled and redirected from a protected route, trigger the sign-in modal
if (authEnabled && redirectUrl && buttonRef.current) {
// Slight delay to ensure everything is loaded
setTimeout(() => {
buttonRef.current?.click();
}, 100);
}
}, [redirectUrl, authEnabled]);
// Don't render anything if no redirect URL or auth is disabled
if (!redirectUrl || !authEnabled) return null;
return (
<div className="hidden">
<SignInButton mode="modal">
<button
ref={buttonRef}
className="hidden"
>
Sign In
</button>
</SignInButton>
</div>
);
}