diff --git a/components/navigation.tsx b/components/navigation.tsx index 17b3c90..bb248ea 100644 --- a/components/navigation.tsx +++ b/components/navigation.tsx @@ -6,7 +6,7 @@ import UserContext from '../context/user' import { USER_ROLE } from '../lib/session' const pathNameLabelMap = { - '/admin/login': 'Login', + '/login': 'Login', '/admin': 'Buchungsübersicht', '/admin/booking/[uuid]': 'Buchung Bearbeiten', '/admin/booking/[uuid]/bill': 'Buchung Rechnungsstellung', diff --git a/components/user.tsx b/components/user.tsx index b9036e4..bbccd9f 100644 --- a/components/user.tsx +++ b/components/user.tsx @@ -1,17 +1,30 @@ +import { useRouter } from 'next/router' import { useContext } from 'react' import UserContext from '../context/user' +import fetch from '../helpers/fetch' export default function User() { + const router = useRouter() const { username, role } = useContext(UserContext) if (!username || !role) { return
} + const onClickLogout = async () => { + await fetch('/api/logout', { method: 'POST' }) + router.reload() + } + return ( -
- {username} -
+ <> +
+ {username} +
+ + ) } diff --git a/lib/session.ts b/lib/session.ts index 7b75934..ba3c765 100644 --- a/lib/session.ts +++ b/lib/session.ts @@ -35,7 +35,7 @@ export function isAdminSession(req: any) { } export function redirectToLogin(req: any, res: any) { - const redirectTargetUrl = `${getBaseURL()}/admin/login?redirect=${encodeURIComponent( + const redirectTargetUrl = `${getBaseURL()}/login?redirect=${encodeURIComponent( req.url )}` diff --git a/pages/api/booking/[uuid]/bill.ts b/pages/api/booking/[uuid]/bill.ts index 9463659..741be06 100644 --- a/pages/api/booking/[uuid]/bill.ts +++ b/pages/api/booking/[uuid]/bill.ts @@ -1,6 +1,6 @@ -import { Bill } from '../../../../../db/bill' -import { createBill, patchBill } from '../../../../../db/index' -import withSession, { isAdminSession } from '../../../../../lib/session' +import { Bill } from '../../../../db/bill' +import { createBill, patchBill } from '../../../../db/index' +import withSession, { isAdminSession } from '../../../../lib/session' export default withSession(async function billHandler(req, res): Promise { if (!isAdminSession(req)) { diff --git a/pages/api/admin/login.ts b/pages/api/login.ts similarity index 85% rename from pages/api/admin/login.ts rename to pages/api/login.ts index 897cec3..f04c59e 100644 --- a/pages/api/admin/login.ts +++ b/pages/api/login.ts @@ -1,5 +1,5 @@ -import { authenticateAdmin } from '../../../lib/authenticate' -import withSession from '../../../lib/session' +import { authenticateAdmin } from '../../lib/authenticate' +import withSession from '../../lib/session' async function loginHandler(req: any, res: any): Promise { const { method } = req diff --git a/pages/api/logout.ts b/pages/api/logout.ts new file mode 100644 index 0000000..4102b2f --- /dev/null +++ b/pages/api/logout.ts @@ -0,0 +1,16 @@ +import withSession from '../../lib/session' + +async function loginHandler(req: any, res: any): Promise { + const { method } = req + + switch (method) { + case 'POST': + req.session.destroy() + res.json({ message: 'Logged out' }) + break + default: + res.setHeader('Allow', ['POST']) + res.status(405).end(`Method ${method} Not Allowed`) + } +} +export default withSession(loginHandler) diff --git a/pages/admin/login.tsx b/pages/login.tsx similarity index 89% rename from pages/admin/login.tsx rename to pages/login.tsx index ad8b2a1..d9da524 100644 --- a/pages/admin/login.tsx +++ b/pages/login.tsx @@ -1,9 +1,9 @@ import React, { useState } from 'react' import { useRouter } from 'next/router' -import Footer from '../../components/footer' -import Header from '../../components/header' -import Input from '../../components/input' -import { getBaseURL } from '../../helpers/url' +import Footer from '../components/footer' +import Header from '../components/header' +import Input from '../components/input' +import { getBaseURL } from '../helpers/url' export default function Login() { const router = useRouter() @@ -16,7 +16,7 @@ export default function Login() { event.preventDefault() try { - await fetch('/api/admin/login', { + await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json',