-
Notifications
You must be signed in to change notification settings - Fork 1k
/
auth.tsx.template
64 lines (51 loc) · 1.52 KB
/
auth.tsx.template
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, { useEffect } from 'react'
import { ClerkProvider, useUser } from '@clerk/clerk-react'
import { createAuth } from '@redwoodjs/auth-clerk-web'
import { navigate } from '@redwoodjs/router'
export const { AuthProvider: ClerkRwAuthProvider, useAuth } = createAuth()
const ClerkStatusUpdater = () => {
const { isSignedIn, user, isLoaded } = useUser()
const { reauthenticate } = useAuth()
useEffect(() => {
if (isLoaded) {
reauthenticate()
}
}, [isSignedIn, user, reauthenticate, isLoaded])
return null
}
type ClerkOptions =
| { publishableKey: string; frontendApi?: never }
| { publishableKey?: never; frontendApi: string }
interface Props {
children: React.ReactNode
}
const ClerkProviderWrapper = ({
children,
clerkOptions,
}: Props & { clerkOptions: ClerkOptions }) => {
const { reauthenticate } = useAuth()
return (
<ClerkProvider
{...clerkOptions}
navigate={(to) => reauthenticate().then(() => navigate(to))}
>
{children}
<ClerkStatusUpdater />
</ClerkProvider>
)
}
export const AuthProvider = ({ children }: Props) => {
const publishableKey = process.env.CLERK_PUBLISHABLE_KEY
const frontendApi =
process.env.CLERK_FRONTEND_API_URL || process.env.CLERK_FRONTEND_API
const clerkOptions: ClerkOptions = publishableKey
? { publishableKey }
: { frontendApi }
return (
<ClerkRwAuthProvider>
<ClerkProviderWrapper clerkOptions={clerkOptions}>
{children}
</ClerkProviderWrapper>
</ClerkRwAuthProvider>
)
}