2023-06-13 18:14:06 +04:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
import * as React from 'react'
|
2023-06-16 20:38:13 +04:00
|
|
|
import { signIn } from 'next-auth/react'
|
2023-06-13 18:14:06 +04:00
|
|
|
|
|
|
|
|
import { cn } from '@/lib/utils'
|
|
|
|
|
import { Button, type ButtonProps } from '@/components/ui/button'
|
2023-06-16 21:28:44 +04:00
|
|
|
import { IconGitHub, IconSpinner } from '@/components/ui/icons'
|
2023-06-16 15:28:43 -04:00
|
|
|
import { useSearchParams } from 'next/navigation'
|
2023-06-13 18:14:06 +04:00
|
|
|
interface LoginButtonProps extends ButtonProps {
|
2023-06-16 21:28:44 +04:00
|
|
|
showGithubIcon?: boolean
|
2023-06-13 18:14:06 +04:00
|
|
|
text?: string
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 21:28:44 +04:00
|
|
|
export function LoginButton({
|
|
|
|
|
text = 'Login with GitHub',
|
|
|
|
|
showGithubIcon = true,
|
|
|
|
|
className,
|
|
|
|
|
...props
|
|
|
|
|
}: LoginButtonProps) {
|
2023-06-13 18:14:06 +04:00
|
|
|
const [isLoading, setIsLoading] = React.useState(false)
|
2023-06-16 15:28:43 -04:00
|
|
|
const searchParams = useSearchParams()
|
2023-06-16 21:40:12 +02:00
|
|
|
const next = searchParams.get('next') || ''
|
2023-06-13 18:14:06 +04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
2023-06-16 21:28:44 +04:00
|
|
|
variant="outline"
|
2023-06-13 18:14:06 +04:00
|
|
|
onClick={() => {
|
|
|
|
|
setIsLoading(true)
|
2023-06-16 15:28:43 -04:00
|
|
|
signIn('github', {
|
|
|
|
|
callbackUrl: `${window.location.origin}${next}`,
|
|
|
|
|
redirect: true
|
|
|
|
|
})
|
2023-06-13 18:14:06 +04:00
|
|
|
}}
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
className={cn(className)}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
2023-06-16 21:28:44 +04:00
|
|
|
{isLoading ? (
|
|
|
|
|
<IconSpinner className="mr-2 animate-spin" />
|
|
|
|
|
) : showGithubIcon ? (
|
|
|
|
|
<IconGitHub className="mr-2" />
|
|
|
|
|
) : null}
|
|
|
|
|
{text}
|
2023-06-13 18:14:06 +04:00
|
|
|
</Button>
|
|
|
|
|
)
|
|
|
|
|
}
|