1
0
Fork 0

refactor(pages): require authentication to visit pages

This commit is contained in:
Vojtěch Mareš 2023-06-26 23:10:17 +02:00
parent 019267584a
commit 5a300f9f88
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
3 changed files with 24 additions and 5 deletions

View file

@ -1,7 +1,17 @@
import Head from "next/head";
import { type GetServerSideProps } from "next";
import { getServerAuthSession } from "~/server/auth";
import { Layout } from "~/components/Layout";
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);
if (!session) return { redirect: { destination: '/api/auth/signin', permanent: false } };
return { props: { session } };
}
export default function Home() {
return (
<>

View file

@ -5,18 +5,22 @@ import Link from "next/link";
import { formatCurrency } from "~/lib/currency/formatter";
import { getServerAuthSession } from "~/server/auth";
import { prisma } from "~/server/db";
import { Training } from "lib/content/training";
import { Layout } from "~/components/Layout";
// import { Button } from "~/components/Button";
export const getServerSideProps: GetServerSideProps = async (context) => {
const trainingSlug = context.query.slug as string;
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);
if (!session) return { redirect: { destination: '/api/auth/signin', permanent: false } };
const trainingSlug = ctx.query.slug as string;
const training = await prisma.training.findUnique({ where: { slug: trainingSlug }});
if (!training) return { notFound: true };
return { props: { training: training } };
return { props: { training: training, session } };
}
function Detail({ training }: { training: any }) {

View file

@ -4,12 +4,17 @@ import Link from "next/link";
import { formatCurrency } from "~/lib/currency/formatter";
import { getServerAuthSession } from "~/server/auth";
import { prisma } from "~/server/db";
import { Training } from "lib/content/training";
import { Layout } from "~/components/Layout";
import { Button } from "~/components/Button";
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const session = await getServerAuthSession(ctx);
if (!session) return { redirect: { destination: '/api/auth/signin', permanent: false } };
const trainings = await prisma.training.findMany({
select: {
id: true,
@ -21,7 +26,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
priceCorporate: true,
}
});
return { props: { trainings: trainings } };
return { props: { trainings: trainings, session } };
}
function Table({trainings}: { trainings: Training[] }) {