diff --git a/app/api/authentication.tsx b/app/api/authentication.tsx index e6637af..3275700 100644 --- a/app/api/authentication.tsx +++ b/app/api/authentication.tsx @@ -1,6 +1,7 @@ /* Copyright (C) 2024 Manuel Bustillo*/ import { getCsrfToken } from '@/app/lib/utils'; +import { User } from '@/app/lib/definitions'; export function login({ email, password, onLogin }: { email: string, password: string, onLogin: () => void }) { console.log(email, password); @@ -14,4 +15,24 @@ export function login({ email, password, onLogin }: { email: string, password: s }) .then(onLogin) .catch((error) => console.error(error)); -} \ No newline at end of file +} + +export function logout({ onLogout }: { onLogout: () => void }) { + fetch("/api/default/users/sign_out", { + method: 'DELETE', + headers: { + 'X-CSRF-TOKEN': getCsrfToken(), + } + }).then(onLogout) + .catch((error) => console.error(error)); +} + +export function getCurrentUser({ onLoad }: { onLoad: (user: User) => void }) { + fetch("/api/default/users/current") + .then((response) => response.json()) + .then((data) => { + onLoad(data); + }, (error) => { + return null; + }); +} \ No newline at end of file diff --git a/app/lib/definitions.ts b/app/lib/definitions.ts index 5c008c9..5511a46 100644 --- a/app/lib/definitions.ts +++ b/app/lib/definitions.ts @@ -54,3 +54,8 @@ export type guestsTable = { amount: number; status: 'pending' | 'paid'; }; + +export type User = { + id: string; + email: string; +} \ No newline at end of file diff --git a/app/ui/components/login-form.tsx b/app/ui/components/login-form.tsx index 492d1d2..13c6349 100644 --- a/app/ui/components/login-form.tsx +++ b/app/ui/components/login-form.tsx @@ -4,16 +4,23 @@ import { FloatLabel } from 'primereact/floatlabel'; import { InputText } from 'primereact/inputtext'; -import { login } from '../../api/authentication'; -import { useState } from 'react'; +import { getCurrentUser, login } from '../../api/authentication'; +import { useState, useEffect } from 'react'; import { classNames } from './button'; import { useRouter } from 'next/navigation' +import { User } from '../../lib/definitions'; export default function LoginForm() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); - const router = useRouter() + const router = useRouter(); + + const [currentUser, setCurrentUser] = useState(null); + + useEffect(() => { + localStorage.setItem('currentUser', JSON.stringify(currentUser)); + }, [currentUser]); return (
@@ -28,7 +35,14 @@ export default function LoginForm() {
diff --git a/app/ui/dashboard/sidenav.tsx b/app/ui/dashboard/sidenav.tsx index a13a2e6..39a2346 100644 --- a/app/ui/dashboard/sidenav.tsx +++ b/app/ui/dashboard/sidenav.tsx @@ -1,30 +1,44 @@ /* Copyright (C) 2024 Manuel Bustillo*/ +'use client'; + import Link from 'next/link'; import NavLinks from '@/app/ui/dashboard/nav-links'; import { PowerIcon } from '@heroicons/react/24/outline'; import { gloriaHallelujah } from '@/app/ui/fonts'; +import { logout } from '@/app/api/authentication'; +import { useRouter } from 'next/navigation'; export default function SideNav() { + const router = useRouter(); + return (
-
+

Wedding Planner

-
- -
+
);