Login/logout process.

This commit is contained in:
Mike Cao 2020-07-25 16:31:07 -07:00
parent 0f0b1e39e7
commit f947c7770b
10 changed files with 91 additions and 58 deletions

View file

@ -1,17 +1,40 @@
import React from 'react';
import Link from 'next/link';
import cookies from 'next-cookies';
import Layout from 'components/Layout';
import Login from 'components/Login';
import { verifySecureToken } from 'lib/crypto';
export default function Home() {
export default function HomePage({ username }) {
return (
<Layout>
<Login />
<p>
<Link href="/test">
<a>Test page 🡒</a>
</Link>
</p>
<h2>
You've successfully logged in as <b>{username}</b>.
</h2>
<Link href="/logout">
<a>Logout 🡒</a>
</Link>
</Layout>
);
}
export async function getServerSideProps(context) {
const token = cookies(context)['umami.auth'];
try {
const payload = await verifySecureToken(token);
return {
props: {
username: payload.username,
},
};
} catch {
const { res } = context;
res.statusCode = 303;
res.setHeader('Location', '/login');
res.end();
}
return { props: {} };
}