diff --git a/src/app/login/actions.ts b/src/app/login/actions.ts index 1b5acb1..c6795a4 100644 --- a/src/app/login/actions.ts +++ b/src/app/login/actions.ts @@ -2,7 +2,6 @@ import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; - import { createClient } from '@/utils/supabase/server'; interface FormData { @@ -33,3 +32,18 @@ export async function signInWithGithub() { redirect(data.url); } } + +export async function loginAnonymously() { + const supabase = createClient(); + const { error: signInError } = await supabase.auth.signInAnonymously(); + const { error: updateUserError } = await supabase.auth.updateUser({ + email: `anonymous+${Date.now().toString(36)}@example.com`, + }); + + if (signInError || updateUserError) { + return { error: true }; + } + + revalidatePath('/', 'layout'); + redirect('/'); +} diff --git a/src/components/authentication/login-form.tsx b/src/components/authentication/login-form.tsx index 6d61bd1..ccd9e18 100644 --- a/src/components/authentication/login-form.tsx +++ b/src/components/authentication/login-form.tsx @@ -2,9 +2,10 @@ import Image from 'next/image'; import { Button } from '@/components/ui/button'; -import { login } from '@/app/login/actions'; +import { login, loginAnonymously } from '@/app/login/actions'; import { useState } from 'react'; import { AuthenticationForm } from '@/components/authentication/authentication-form'; +import { Separator } from '@/components/ui/separator'; import { useToast } from '@/components/ui/use-toast'; export function LoginForm() { @@ -20,12 +21,28 @@ export function LoginForm() { }); } + function handleAnonymousLogin() { + loginAnonymously().then((data) => { + if (data?.error) { + toast({ description: 'Something went wrong. Please try again', variant: 'destructive' }); + } + }); + } + return (