Skip to content

Commit

Permalink
rest
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Nov 24, 2023
1 parent 74baa14 commit a93f6cb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
3 changes: 1 addition & 2 deletions ui/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -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('');
}
40 changes: 24 additions & 16 deletions ui/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Layout>
<LayoutMain alignSelf='center' justifySelf='center' width='xxLarge'>
Expand All @@ -32,21 +48,13 @@ export default function Password() {
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
setPass(e.target.value)
}
/>
<Button
onClick={() => {
fetch('/api/login', {
method: 'POST',
body: JSON.stringify({ password: pass }),
})
.then((res) => res.json())
.then((res) => {
setError(res.error);
});
onKeyPress={(e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
login();
}
}}
>
Login
</Button>
/>
<Button onClick={login}>Login</Button>
</LayoutMain>
</Layout>
);
Expand Down
4 changes: 1 addition & 3 deletions ui/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Logout from '@/components/Logout';
import SidebarComponent from '@/components/SidebarComponent';
import { Header } from '@/lib/Header';
import { Layout, LayoutMain } from '@/lib/Layout';
import { cookies } from 'next/headers';

export default function Home() {
return (
<Layout sidebar={<SidebarComponent />}>
<Layout sidebar={<SidebarComponent logout={!!cookies().get('auth')} />}>
<LayoutMain alignSelf='center' justifySelf='center' width='xxLarge'>
<Header variant='largeTitle'>PeerDB Home Page</Header>
{cookies().get('auth') && <Logout />}
</LayoutMain>
</Layout>
);
Expand Down
8 changes: 7 additions & 1 deletion ui/components/Logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { Button } from '@/lib/Button';

export default function Logout() {
return (
<Button onClick={() => fetch('/api/logout', { method: 'POST' })}>
<Button
onClick={() =>
fetch('/api/logout', { method: 'POST' }).then((res) =>
location.assign('/login')
)
}
>
Logout
</Button>
);
Expand Down
6 changes: 3 additions & 3 deletions ui/components/SidebarComponent.tsx
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -60,7 +60,7 @@ export default function SidebarComponent() {
/>
</div>
</div>
<Button className='w-full'>Help and Support</Button>
{props.logout && <Logout />}
</>
}
bottomLabel={<Label variant='footnote'>App. v0.7.0</Label>}
Expand Down
2 changes: 1 addition & 1 deletion ui/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit a93f6cb

Please sign in to comment.