mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated authentication guide for Kotlin Compose Multiplatform with revised dependency configuration. * Enhanced user-management example README with improved project structure, deployment steps, and resources. * **Chores** * Refactored example applications to use custom authentication forms instead of pre-built Auth UI components. * Removed unused authentication UI dependencies from multiple example projects. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45931) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Cemal Kılıç <cemalkilic@users.noreply.github.com>
141 lines
5.1 KiB
TypeScript
141 lines
5.1 KiB
TypeScript
import { Session } from '@supabase/supabase-js'
|
|
import Head from 'next/head'
|
|
import { FormEvent, useEffect, useState } from 'react'
|
|
|
|
import TodoList from '@/components/TodoList'
|
|
import { supabase } from '@/lib/initSupabase'
|
|
|
|
type Mode = 'sign-in' | 'sign-up'
|
|
|
|
export default function Home() {
|
|
const [session, setSession] = useState<Session | null>(null)
|
|
const [mode, setMode] = useState<Mode>('sign-in')
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [message, setMessage] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
supabase.auth.getSession().then(({ data: { session } }) => setSession(session))
|
|
|
|
const {
|
|
data: { subscription },
|
|
} = supabase.auth.onAuthStateChange((_event, session) => setSession(session))
|
|
|
|
return () => subscription.unsubscribe()
|
|
}, [])
|
|
|
|
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault()
|
|
setLoading(true)
|
|
setError(null)
|
|
setMessage(null)
|
|
|
|
if (mode === 'sign-in') {
|
|
const { error } = await supabase.auth.signInWithPassword({ email, password })
|
|
if (error) setError(error.message)
|
|
} else {
|
|
const { data, error } = await supabase.auth.signUp({
|
|
email,
|
|
password,
|
|
options: { emailRedirectTo: `${window.location.origin}/` },
|
|
})
|
|
if (error) {
|
|
setError(error.message)
|
|
} else if (data.user && !data.session) {
|
|
setMessage('Check your email for a confirmation link to complete sign up.')
|
|
}
|
|
}
|
|
|
|
setLoading(false)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>Create Next App</title>
|
|
<meta name="description" content="Generated by create next app" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<link rel="icon" href="/favicon.ico" />
|
|
</Head>
|
|
<div className="w-full h-full bg-200">
|
|
{!session ? (
|
|
<div className="min-w-full min-h-screen flex items-center justify-center">
|
|
<div className="w-full h-full flex justify-center items-center p-4">
|
|
<div className="w-full h-full sm:h-auto sm:w-2/5 max-w-sm p-5 bg-white shadow flex flex-col text-base">
|
|
<span className="font-sans text-4xl text-center pb-2 mb-1 border-b mx-4 align-center">
|
|
{mode === 'sign-in' ? 'Sign In' : 'Sign up'}
|
|
</span>
|
|
<form onSubmit={handleSubmit} className="flex flex-col gap-3 mt-4">
|
|
<label className="flex flex-col gap-1 text-sm">
|
|
Email
|
|
<input
|
|
type="email"
|
|
required
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
autoComplete="email"
|
|
className="border rounded px-2 py-1"
|
|
/>
|
|
</label>
|
|
<label className="flex flex-col gap-1 text-sm">
|
|
Password
|
|
<input
|
|
type="password"
|
|
required
|
|
minLength={6}
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
autoComplete={mode === 'sign-in' ? 'current-password' : 'new-password'}
|
|
className="border rounded px-2 py-1"
|
|
/>
|
|
</label>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="btn-black w-full disabled:opacity-50"
|
|
>
|
|
{loading ? 'Loading...' : mode === 'sign-in' ? 'Sign in' : 'Sign up'}
|
|
</button>
|
|
</form>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setMode(mode === 'sign-in' ? 'sign-up' : 'sign-in')
|
|
setError(null)
|
|
setMessage(null)
|
|
}}
|
|
className="mt-3 text-sm text-blue-600 underline"
|
|
>
|
|
{mode === 'sign-in'
|
|
? 'Need an account? Sign up'
|
|
: 'Already have an account? Sign in'}
|
|
</button>
|
|
{error && <p className="mt-3 text-sm text-red-600">{error}</p>}
|
|
{message && <p className="mt-3 text-sm text-gray-700">{message}</p>}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<div
|
|
className="w-full h-full flex flex-col justify-center items-center p-4"
|
|
style={{ minWidth: 250, maxWidth: 600, margin: 'auto' }}
|
|
>
|
|
<TodoList session={session} />
|
|
<button
|
|
className="btn-black w-full mt-12"
|
|
onClick={async () => {
|
|
const { error } = await supabase.auth.signOut()
|
|
if (error) console.log('Error logging out:', error.message)
|
|
}}
|
|
>
|
|
Logout
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</>
|
|
)
|
|
}
|