umami/pages/index.js

47 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-07-17 10:03:38 +02:00
import React from 'react';
2020-07-19 07:51:17 +02:00
import Link from 'next/link';
2020-07-28 08:52:14 +02:00
import { parse } from 'cookie';
2020-07-24 04:56:55 +02:00
import Layout from 'components/Layout';
import PageviewsChart from 'components/PageviewsChart';
2020-07-26 01:31:07 +02:00
import { verifySecureToken } from 'lib/crypto';
2020-07-28 08:52:14 +02:00
import { subDays, endOfDay } from 'date-fns';
import WebsiteList from '../components/WebsiteList';
2020-07-17 10:03:38 +02:00
2020-07-26 01:31:07 +02:00
export default function HomePage({ username }) {
2020-07-17 10:03:38 +02:00
return (
<Layout>
<WebsiteList />
2020-07-26 09:12:42 +02:00
<div>
<PageviewsChart
2020-07-26 09:12:42 +02:00
websiteId={3}
2020-07-28 08:52:14 +02:00
startDate={subDays(endOfDay(new Date()), 6)}
endDate={endOfDay(new Date())}
2020-07-26 09:12:42 +02:00
/>
</div>
2020-07-26 01:31:07 +02:00
<Link href="/logout">
<a>Logout 🡒</a>
</Link>
2020-07-17 10:03:38 +02:00
</Layout>
);
}
2020-07-26 01:31:07 +02:00
2020-07-28 08:52:14 +02:00
export async function getServerSideProps({ req, res }) {
2020-07-29 09:16:02 +02:00
const token = parse(req.headers.cookie || '')['umami.auth'];
2020-07-26 01:31:07 +02:00
try {
const payload = await verifySecureToken(token);
return {
props: {
2020-07-26 09:12:42 +02:00
...payload,
2020-07-26 01:31:07 +02:00
},
};
} catch {
res.statusCode = 303;
res.setHeader('Location', '/login');
res.end();
}
return { props: {} };
}