forked from mckaywrigley/chatbot-ui
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of Page Titles and Favicon (mckaywrigley#1195)
* Add page title * Create favicon.ico * slight spacing tweak --------- Co-authored-by: Mckay Wrigley <mckaywrigley@gmail.com>
- Loading branch information
Showing
1 changed file
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
</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> | ||
) | ||
} |