cleaning shit up
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
'use client';
|
||||
import { signInWithApple } from '@/lib/queries';
|
||||
import { StatusMessage, SubmitButton } from '@/components/default/forms';
|
||||
import { useAuth } from '@/lib/hooks/context';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
import Image from 'next/image';
|
||||
import { type buttonVariants } from '@/components/ui';
|
||||
import { type ComponentProps } from 'react';
|
||||
import { type VariantProps } from 'class-variance-authority';
|
||||
import { useSupabaseClient } from '@/utils/supabase';
|
||||
|
||||
type ButtonProps = ComponentProps<'button'> & VariantProps<typeof buttonVariants>;
|
||||
type ImageProps = {
|
||||
src: string;
|
||||
alt: string;
|
||||
className?: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
}
|
||||
type FormProps = ComponentProps<'form'>;
|
||||
type TextProps = ComponentProps<'p'>;
|
||||
type SignInWithAppleProps = {
|
||||
buttonProps?: ButtonProps;
|
||||
imageProps?: ImageProps;
|
||||
formProps?: FormProps;
|
||||
textProps?: TextProps;
|
||||
};
|
||||
|
||||
export const SignInWithApple = ({
|
||||
buttonProps = {
|
||||
className: 'w-full cursor-pointer',
|
||||
type: 'submit',
|
||||
},
|
||||
imageProps = {
|
||||
src: '/icons/auth/apple.svg',
|
||||
alt: 'Apple',
|
||||
className: 'invert-75 dark:invert-25',
|
||||
width: 24,
|
||||
height: 24,
|
||||
},
|
||||
formProps = {
|
||||
className: 'my-4',
|
||||
},
|
||||
textProps = {
|
||||
className: 'text-[1.0rem]',
|
||||
},
|
||||
} : SignInWithAppleProps) => {
|
||||
const router = useRouter();
|
||||
const { loading, refreshUser } = useAuth();
|
||||
const [statusMessage, setStatusMessage] = useState('');
|
||||
const [ isLoading, setIsLoading ] = useState(false);
|
||||
const supabase = useSupabaseClient();
|
||||
|
||||
const handleSignInWithApple = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
if (!supabase) throw new Error('Supabase client not found');
|
||||
setStatusMessage('');
|
||||
setIsLoading(true);
|
||||
const result = await signInWithApple(supabase);
|
||||
if (result.data.url) window.location.href = result.data.url;
|
||||
else setStatusMessage(`There was a problem signing in with Apple!`);
|
||||
} catch (error) {
|
||||
setStatusMessage(`Error signing in: ${error as string}`);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
await refreshUser();
|
||||
router.push('');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSignInWithApple} {...formProps}>
|
||||
<SubmitButton
|
||||
disabled={isLoading || loading}
|
||||
pendingText='Signing in...'
|
||||
{...buttonProps}
|
||||
>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Image
|
||||
src={imageProps.src}
|
||||
alt={imageProps.alt}
|
||||
className={imageProps.className}
|
||||
width={imageProps.width}
|
||||
height={imageProps.height}
|
||||
/>
|
||||
<p className={textProps.className} {...textProps}>Sign In with Apple</p>
|
||||
</div>
|
||||
</SubmitButton>
|
||||
{statusMessage && <StatusMessage message={{ error: statusMessage }} />}
|
||||
</form>
|
||||
);
|
||||
};
|
Reference in New Issue
Block a user