Skip to content

Commit

Permalink
banner & fix printing login error
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Nov 24, 2023
1 parent 71efee7 commit 74baa14
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
4 changes: 2 additions & 2 deletions ui/app/api/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {NextResponse} from 'next/server'
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 NextResponse.redirect(new URL('/login', req.url));
}
67 changes: 41 additions & 26 deletions ui/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
'use client';
import Image from 'next/image'
import Image from 'next/image';
import { useState } from 'react';

import { Button } from '@/lib/Button';
import { TextField } from '@/lib/TextField';
import { Layout, LayoutMain } from '@/lib/Layout';
import { TextField } from '@/lib/TextField';

export default function Password() {
const [pass, setPass] = useState("");
const [error, setError] = useState("");
const [pass, setPass] = useState('');
const [error, setError] = useState('');
return (
<Layout>
<Layout>
<LayoutMain alignSelf='center' justifySelf='center' width='xxLarge'>
<Image src="public/images/peerdb-combinedMark.svg" alt="PeerDB" width={512} />
{error && <div style={{
borderRadius:'8px',
fontWeight:'bold',
color:'#600',
backgroundColor:'#c66'
}}>{error}</div>}
Password: <TextField variant='simple' value={pass} onChange={(e: React.ChangeEvent<HTMLInputElement>) => setPass(e.target.value)} />
<Button
onClick={() => {
fetch('/api/login', {
method: 'POST',
body: JSON.stringify({password: pass}),
}).then((res: any) => {
setError(res.error);
});
}}
>
Login
</Button>
</LayoutMain>
<Image src='/images/peerdb-combinedMark.svg' alt='PeerDB' width={512} />
{error && (
<div
style={{
borderRadius: '8px',
fontWeight: 'bold',
color: '#600',
backgroundColor: '#c66',
}}
>
{error}
</div>
)}
<TextField
variant='simple'
placeholder='Password'
value={pass}
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);
});
}}
>
Login
</Button>
</LayoutMain>
</Layout>
);
}
24 changes: 13 additions & 11 deletions ui/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type {NextRequest} from 'next/server';
import type { NextRequest } from 'next/server';

import {NextResponse} from 'next/server'
import { NextResponse } from 'next/server';

export default function middleware(req: NextRequest) {
if (req.nextUrl.pathname !== '/favicon.ico' &&
req.nextUrl.pathname !== '/login' &&
req.nextUrl.pathname !== '/api/login' &&
!req.nextUrl.pathname.startsWith("/public/") &&
!req.nextUrl.pathname.startsWith("/_next/static/") &&
process.env.PEERDB_PASSWORD &&
req.cookies.get('auth')?.value !== process.env.PEERDB_PASSWORD) {
if (
req.nextUrl.pathname !== '/favicon.ico' &&
req.nextUrl.pathname !== '/login' &&
req.nextUrl.pathname !== '/api/login' &&
!req.nextUrl.pathname.startsWith('/images/') &&
!req.nextUrl.pathname.startsWith('/_next/static/') &&
process.env.PEERDB_PASSWORD &&
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', req.url));
}
return NextResponse.next()
return NextResponse.next();
}

0 comments on commit 74baa14

Please sign in to comment.