Skip to content

Commit

Permalink
Addition of Page Titles and Favicon (mckaywrigley#1195)
Browse files Browse the repository at this point in the history
* Add page title

* Create favicon.ico

* slight spacing tweak

---------

Co-authored-by: Mckay Wrigley <mckaywrigley@gmail.com>
  • Loading branch information
2 people authored and jollytoad committed Jan 22, 2024
1 parent f81eddd commit b41d3d8
Showing 1 changed file with 162 additions and 0 deletions.
162 changes: 162 additions & 0 deletions app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { Brand } from "@/components/ui/brand"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { createClient } from "@/lib/supabase/server"
import { Database } from "@/supabase/types"
import { createServerClient } from "@supabase/ssr"
import { Metadata } from "next"
import { cookies, headers } from "next/headers"
import { redirect } from "next/navigation"

export const metadata: Metadata = {
title: "Login"
}

export default async function Login({
searchParams
}: {
searchParams: { message: string }
}) {
const cookieStore = cookies()
const supabase = createServerClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return cookieStore.get(name)?.value
}
}
}
)
const session = (await supabase.auth.getSession()).data.session

if (session) {
return redirect("/chat")
}

const signIn = async (formData: FormData) => {
"use server"

const email = formData.get("email") as string
const password = formData.get("password") as string
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const { data, error } = await supabase.auth.signInWithPassword({
email,
password
})

if (error) {
return redirect("/login?message=Could not authenticate user")
}

return redirect("/chat")
}

const signUp = async (formData: FormData) => {
"use server"

const email = formData.get("email") as string
const password = formData.get("password") as string
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const { error } = await supabase.auth.signUp({
email,
password,
options: {
// USE IF YOU WANT TO SEND EMAIL VERIFICATION, ALSO CHANGE TOML FILE
// emailRedirectTo: `${origin}/auth/callback`
}
})

if (error) {
console.error(error)
return redirect("/login?message=Could not authenticate user")
}

return redirect("/setup")

// USE IF YOU WANT TO SEND EMAIL VERIFICATION, ALSO CHANGE TOML FILE
// return redirect("/login?message=Check email to continue sign in process")
}

const handleResetPassword = async (formData: FormData) => {
"use server"

const origin = headers().get("origin")
const email = formData.get("email") as string
const cookieStore = cookies()
const supabase = createClient(cookieStore)

const { error } = await supabase.auth.resetPasswordForEmail(email, {
redirectTo: `${origin}/auth/callback?next=/login/password`
})

if (error) {
console.error(error)
return redirect("/login?message=Could not reset password")
}
}

return (
<div className="flex w-full flex-1 flex-col justify-center gap-2 px-8 sm:max-w-md">
<form
className="animate-in text-foreground flex w-full flex-1 flex-col justify-center gap-2"
action={signIn}
>
<Brand />

<Label className="text-md mt-4" htmlFor="email">
Email
</Label>
<Input
className="mb-3 rounded-md border bg-inherit px-4 py-2"
name="email"
placeholder="you@example.com"
required
/>

<Label className="text-md" htmlFor="password">
Password
</Label>
<Input
className="mb-6 rounded-md border bg-inherit px-4 py-2"
type="password"
name="password"
placeholder="••••••••"
/>

<Button className="mb-2 rounded-md bg-blue-700 px-4 py-2 text-white">
Login
</Button>

<Button
formAction={signUp}
className="border-foreground/20 mb-2 rounded-md border px-4 py-2"
>
Sign Up
</Button>

<div className="text-muted-foreground mt-1 flex justify-center text-sm">
<span className="mr-1">Forgot your password?</span>
<button
formAction={handleResetPassword}
className="text-primary ml-1 underline hover:opacity-80"
>
Reset
</button>
</div>

{searchParams?.message && (
<p className="bg-foreground/10 text-foreground mt-4 p-4 text-center">
{searchParams.message}
</p>
)}
</form>
</div>
)
}

0 comments on commit b41d3d8

Please sign in to comment.