-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* popup turnstile * turnstile on api * fix * trash
- Loading branch information
Din
authored
Dec 12, 2023
1 parent
5d7b813
commit b321a2f
Showing
13 changed files
with
201 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
NEXT_PUBLIC_API_URL=http://localhost:8000 | ||
NEXT_PUBLIC_API_URL=http://localhost:8000 | ||
NEXT_PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAAOd1-P2R_2ooD0h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
NEXT_PUBLIC_API_URL=https://preview.api.example.com | ||
NEXT_PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAAOd1-P2R_2ooD0h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
NEXT_PUBLIC_API_URL=https://api.example.com | ||
NEXT_PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAAOd1-P2R_2ooD0h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client' | ||
|
||
import type { TurnstileInstance } from '@marsidev/react-turnstile' | ||
import { Turnstile } from '@marsidev/react-turnstile' | ||
import * as Portal from '@radix-ui/react-portal' | ||
import { env } from '@web/env' | ||
import { atom, useAtom } from 'jotai' | ||
import { useTheme } from 'next-themes' | ||
import { useId, useRef } from 'react' | ||
import { match } from 'ts-pattern' | ||
import { useIsRendered } from '@ui/hooks/use-is-rendered' | ||
import { cn } from '@ui/lib/utils' | ||
|
||
export const turnstileTokenAtom = atom<string | null>(null) | ||
|
||
export const showTurnstileAtom = atom(false) | ||
|
||
export const turnstileRefAtom = atom<TurnstileInstance | null>(null) | ||
|
||
export default function TurnstileProvider({ children }: { children: React.ReactNode }) { | ||
const { theme } = useTheme() | ||
const [, setTurnstileToken] = useAtom(turnstileTokenAtom) | ||
const [showTurnstile] = useAtom(showTurnstileAtom) | ||
const turnstileRef = useRef<TurnstileInstance>(null) | ||
const id = useId() | ||
const isRendered = useIsRendered() | ||
const [, _setTurnstileRef] = useAtom(turnstileRefAtom) | ||
|
||
return ( | ||
<> | ||
{children} | ||
{isRendered && ( | ||
<Portal.Root> | ||
<div | ||
className={cn( | ||
'fixed top-0 left-0 right-0 bottom-0 z-[99999] flex items-center justify-center bg-background/80', | ||
{ | ||
'top-[100%] bottom-[-100%]': !showTurnstile, | ||
}, | ||
)} | ||
> | ||
<Turnstile | ||
ref={turnstileRef} | ||
id={id} | ||
siteKey={env.NEXT_PUBLIC_TURNSTILE_SITE_KEY} | ||
options={{ | ||
theme: match(theme) | ||
.with('dark', () => 'dark' as const) | ||
.with('light', () => 'light' as const) | ||
.otherwise(() => 'auto' as const), | ||
}} | ||
onSuccess={(token) => { | ||
_setTurnstileRef(turnstileRef.current) | ||
setTurnstileToken(token) | ||
}} | ||
onError={() => setTurnstileToken(null)} | ||
onExpire={() => { | ||
setTurnstileToken(null) | ||
turnstileRef.current?.reset() | ||
}} | ||
/> | ||
</div> | ||
</Portal.Root> | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.