From 88f7bdadb8a06ccf6e89a671a439c3bd7b050bd1 Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Thu, 23 Nov 2023 23:20:48 +0000 Subject: [PATCH] rest --- ui/app/api/logout/route.ts | 3 +-- ui/app/login/page.tsx | 40 ++++++++++++++++++------------ ui/app/page.tsx | 4 +-- ui/components/Logout.tsx | 8 +++++- ui/components/SidebarComponent.tsx | 6 ++--- ui/middleware.ts | 2 +- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/ui/app/api/logout/route.ts b/ui/app/api/logout/route.ts index 3721a6bff8..5faf5a8c84 100644 --- a/ui/app/api/logout/route.ts +++ b/ui/app/api/logout/route.ts @@ -1,7 +1,6 @@ import { cookies } from 'next/headers'; -import { NextResponse } from 'next/server'; export async function POST(req: Request) { cookies().delete('auth'); - return NextResponse.redirect(new URL('/login', req.url)); + return new Response(''); } diff --git a/ui/app/login/page.tsx b/ui/app/login/page.tsx index e05b782905..d158978aab 100644 --- a/ui/app/login/page.tsx +++ b/ui/app/login/page.tsx @@ -1,14 +1,30 @@ 'use client'; import Image from 'next/image'; +import { useRouter, useSearchParams } from 'next/navigation'; import { useState } from 'react'; import { Button } from '@/lib/Button'; import { Layout, LayoutMain } from '@/lib/Layout'; import { TextField } from '@/lib/TextField'; -export default function Password() { +export default function Login() { + const router = useRouter(); + const searchParams = useSearchParams(); const [pass, setPass] = useState(''); - const [error, setError] = useState(''); + const [error, setError] = useState(() => + searchParams.has('reject') ? 'Authentication failed, please login' : '' + ); + const login = () => { + fetch('/api/login', { + method: 'POST', + body: JSON.stringify({ password: pass }), + }) + .then((res) => res.json()) + .then((res) => { + if (res.error) setError(res.error); + else router.push('/'); + }); + }; return ( @@ -32,21 +48,13 @@ export default function Password() { onChange={(e: React.ChangeEvent) => setPass(e.target.value) } - /> - + /> + ); diff --git a/ui/app/page.tsx b/ui/app/page.tsx index 13f1ebb374..d89f981887 100644 --- a/ui/app/page.tsx +++ b/ui/app/page.tsx @@ -1,4 +1,3 @@ -import Logout from '@/components/Logout'; import SidebarComponent from '@/components/SidebarComponent'; import { Header } from '@/lib/Header'; import { Layout, LayoutMain } from '@/lib/Layout'; @@ -6,10 +5,9 @@ import { cookies } from 'next/headers'; export default function Home() { return ( - }> + }>
PeerDB Home Page
- {cookies().get('auth') && }
); diff --git a/ui/components/Logout.tsx b/ui/components/Logout.tsx index 72975b8fc3..21c8e8267a 100644 --- a/ui/components/Logout.tsx +++ b/ui/components/Logout.tsx @@ -3,7 +3,13 @@ import { Button } from '@/lib/Button'; export default function Logout() { return ( - ); diff --git a/ui/components/SidebarComponent.tsx b/ui/components/SidebarComponent.tsx index c60372da8e..c9afc54ca6 100644 --- a/ui/components/SidebarComponent.tsx +++ b/ui/components/SidebarComponent.tsx @@ -1,15 +1,15 @@ 'use client'; import useTZStore from '@/app/globalstate/time'; +import Logout from '@/components/Logout'; import { BrandLogo } from '@/lib/BrandLogo'; -import { Button } from '@/lib/Button'; import { Icon } from '@/lib/Icon'; import { Label } from '@/lib/Label'; import { RowWithSelect } from '@/lib/Layout'; import { Sidebar, SidebarItem } from '@/lib/Sidebar'; import Link from 'next/link'; -export default function SidebarComponent() { +export default function SidebarComponent(props: { logout?: boolean }) { const timezones = ['UTC', 'Local', 'Relative']; const setZone = useTZStore((state) => state.setZone); const zone = useTZStore((state) => state.timezone); @@ -60,7 +60,7 @@ export default function SidebarComponent() { /> - + {props.logout && } } bottomLabel={} diff --git a/ui/middleware.ts b/ui/middleware.ts index 53e88f6850..14c7944cb3 100644 --- a/ui/middleware.ts +++ b/ui/middleware.ts @@ -13,7 +13,7 @@ export default function middleware(req: NextRequest) { req.cookies.get('auth')?.value !== process.env.PEERDB_PASSWORD ) { req.cookies.delete('auth'); - return NextResponse.redirect(new URL('/login', req.url)); + return NextResponse.redirect(new URL('/login?reject', req.url)); } return NextResponse.next(); }