refactor(pages): require authentication to visit pages
This commit is contained in:
parent
019267584a
commit
5a300f9f88
3 changed files with 24 additions and 5 deletions
|
|
@ -1,7 +1,17 @@
|
||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
|
import { type GetServerSideProps } from "next";
|
||||||
|
|
||||||
|
import { getServerAuthSession } from "~/server/auth";
|
||||||
import { Layout } from "~/components/Layout";
|
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() {
|
export default function Home() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
|
||||||
|
|
@ -5,18 +5,22 @@ import Link from "next/link";
|
||||||
|
|
||||||
import { formatCurrency } from "~/lib/currency/formatter";
|
import { formatCurrency } from "~/lib/currency/formatter";
|
||||||
|
|
||||||
|
import { getServerAuthSession } from "~/server/auth";
|
||||||
import { prisma } from "~/server/db";
|
import { prisma } from "~/server/db";
|
||||||
import { Training } from "lib/content/training";
|
import { Training } from "lib/content/training";
|
||||||
import { Layout } from "~/components/Layout";
|
import { Layout } from "~/components/Layout";
|
||||||
// import { Button } from "~/components/Button";
|
// import { Button } from "~/components/Button";
|
||||||
|
|
||||||
export const getServerSideProps: GetServerSideProps = async (context) => {
|
export const getServerSideProps: GetServerSideProps = async (ctx) => {
|
||||||
const trainingSlug = context.query.slug as string;
|
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 }});
|
const training = await prisma.training.findUnique({ where: { slug: trainingSlug }});
|
||||||
|
|
||||||
if (!training) return { notFound: true };
|
if (!training) return { notFound: true };
|
||||||
|
|
||||||
return { props: { training: training } };
|
return { props: { training: training, session } };
|
||||||
}
|
}
|
||||||
|
|
||||||
function Detail({ training }: { training: any }) {
|
function Detail({ training }: { training: any }) {
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,17 @@ import Link from "next/link";
|
||||||
|
|
||||||
import { formatCurrency } from "~/lib/currency/formatter";
|
import { formatCurrency } from "~/lib/currency/formatter";
|
||||||
|
|
||||||
|
import { getServerAuthSession } from "~/server/auth";
|
||||||
import { prisma } from "~/server/db";
|
import { prisma } from "~/server/db";
|
||||||
import { Training } from "lib/content/training";
|
import { Training } from "lib/content/training";
|
||||||
import { Layout } from "~/components/Layout";
|
import { Layout } from "~/components/Layout";
|
||||||
import { Button } from "~/components/Button";
|
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({
|
const trainings = await prisma.training.findMany({
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|
@ -21,7 +26,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
|
||||||
priceCorporate: true,
|
priceCorporate: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return { props: { trainings: trainings } };
|
return { props: { trainings: trainings, session } };
|
||||||
}
|
}
|
||||||
|
|
||||||
function Table({trainings}: { trainings: Training[] }) {
|
function Table({trainings}: { trainings: Training[] }) {
|
||||||
|
|
|
||||||
Reference in a new issue