auth secured routes

This commit is contained in:
Karol Wypchlo 2021-02-19 16:50:45 +01:00
parent 387c19e11a
commit 5472833369
2 changed files with 21 additions and 0 deletions

View File

@ -4,12 +4,15 @@ import prettyBytes from "pretty-bytes";
import Link from "next/link"; import Link from "next/link";
import useSWR from "swr"; import useSWR from "swr";
import Layout from "../components/Layout"; import Layout from "../components/Layout";
import authServerSideProps from "../services/authServerSideProps";
dayjs.extend(relativeTime); dayjs.extend(relativeTime);
const apiPrefix = process.env.NODE_ENV === "development" ? "/api/stubs" : ""; const apiPrefix = process.env.NODE_ENV === "development" ? "/api/stubs" : "";
const fetcher = (url) => fetch(url).then((r) => r.json()); const fetcher = (url) => fetch(url).then((r) => r.json());
export const getServerSideProps = authServerSideProps();
function SkylinkList({ items = [] }) { function SkylinkList({ items = [] }) {
return ( return (
<ul className="divide-y divide-gray-200"> <ul className="divide-y divide-gray-200">

View File

@ -0,0 +1,18 @@
export default function authServerSideProps(getServerSideProps) {
return function authenticate(context) {
if (!("ory_kratos_session" in context.req.cookies) || !("skynet-jwt" in context.req.cookies)) {
return {
redirect: {
permanent: false,
destination: `/api/accounts/login?return_to=${encodeURIComponent(context.resolvedUrl)}`,
},
};
}
if (getServerSideProps) {
return getServerSideProps(context);
}
return { props: {} };
};
}