diff --git a/frontend/src/components/auth/form.tsx b/frontend/src/components/auth/form.tsx index fb09104..95dc7cc 100644 --- a/frontend/src/components/auth/form.tsx +++ b/frontend/src/components/auth/form.tsx @@ -2,22 +2,22 @@ import { Icons } from "@/components/icons"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; +import { login } from "@/lib/auth"; import { cn } from "@/lib/utils"; import { Link } from "@tanstack/react-router"; -import { HTMLAttributes, useState } from "react"; +import { HTMLAttributes, SyntheticEvent, useState } from "react"; interface UserAuthFormProps extends HTMLAttributes {} export function RegisterForm({ className, ...props }: UserAuthFormProps) { const [isLoading, setIsLoading] = useState(false); - async function onSubmit(event: React.SyntheticEvent) { + async function onSubmit(event: SyntheticEvent) { event.preventDefault(); - setIsLoading(true); - setTimeout(() => { - setIsLoading(false); - }, 3000); + setIsLoading(true); + // await login() + setIsLoading(false); } return ( @@ -42,7 +42,7 @@ export function RegisterForm({ className, ...props }: UserAuthFormProps) { {isLoading && ( )} - Sign In with Email + Create Account @@ -53,13 +53,16 @@ export function RegisterForm({ className, ...props }: UserAuthFormProps) { export function LoginForm({ className, ...props }: UserAuthFormProps) { const [isLoading, setIsLoading] = useState(false); - async function onSubmit(event: React.SyntheticEvent) { + async function onSubmit(event: SyntheticEvent) { event.preventDefault(); - setIsLoading(true); - setTimeout(() => { - setIsLoading(false); - }, 3000); + const email = (event.target as HTMLFormElement).email.value; + const password = (event.target as HTMLFormElement).password.value; + + setIsLoading(true); + const result = await login(email, password); + console.log({ result }); + setIsLoading(false); } return ( @@ -67,12 +70,9 @@ export function LoginForm({ className, ...props }: UserAuthFormProps) {
- +
+
+ + +

Register diff --git a/frontend/src/lib/auth.ts b/frontend/src/lib/auth.ts index cd894d3..d3a5c1a 100644 --- a/frontend/src/lib/auth.ts +++ b/frontend/src/lib/auth.ts @@ -35,3 +35,30 @@ export const getSession = async (): Promise< return err({ detail: error.detail }); } }; + +type LoginResponse = { + email: string; + expiry: string; +}; + +export const login = async ( + email: string, + password: string, +): Promise> => { + const response = await fetch(TARGET + "/api/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ email, password }), + }); + + if (response.ok) { + const user = await response.json(); + useUserStore.getState().setUser(user); + return ok(user); + } else { + const error = await response.json(); + return err({ detail: error.detail }); + } +}; diff --git a/frontend/src/routes/login.tsx b/frontend/src/routes/login.tsx index 96eb277..db38e98 100644 --- a/frontend/src/routes/login.tsx +++ b/frontend/src/routes/login.tsx @@ -23,9 +23,9 @@ function Login() {

-

Login

+

Sign In

- Enter your email below to login + Enter your email & password below to login