Skip to content

Commit

Permalink
Merge pull request #47 from SocialGouv/feat/protect-offer-route
Browse files Browse the repository at this point in the history
feat: protect offer route
  • Loading branch information
ClementNumericite authored Feb 29, 2024
2 parents 502833d + 1514be0 commit 2f66bf6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
46 changes: 46 additions & 0 deletions webapp/src/guards/hasAccessToOffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { type GetServerSideProps } from "next";
import { appRouter } from "~/server/api/root";
import getPayloadClient from "~/payload/payloadClient";
import { PayloadJwtSession, createCallerFactory } from "~/server/api/trpc";
import { jwtDecode } from "jwt-decode";

export const hasAccessToOffer: GetServerSideProps = async (context) => {
const payload = await getPayloadClient({ seed: false });

const jwtCookie =
context.req.cookies[process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"];

if (!jwtCookie) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

const session = jwtDecode<PayloadJwtSession>(jwtCookie);

const createCaller = createCallerFactory(appRouter);

const caller = createCaller({ payload, session });

const { data: offerListAvailables } = await caller.offer.getListOfAvailables({
offerId: parseInt(context.params?.id as string),
page: 1,
perPage: 1,
});

if (offerListAvailables?.length === 0) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

return {
props: {},
};
};
8 changes: 3 additions & 5 deletions webapp/src/pages/dashboard/category/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ export default function Dashboard() {

return (
<CategoryWrapper category={category}>
{offers
?.filter((offer) => offer.kind === "code")
?.map((offer) => (
<OfferCard key={offer.id} offer={offer} />
))}
{offers?.map((offer) => (
<OfferCard key={offer.id} offer={offer} />
))}
</CategoryWrapper>
);
}
6 changes: 6 additions & 0 deletions webapp/src/pages/dashboard/offer/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useSteps,
} from "@chakra-ui/react";
import { useGSAP } from "@gsap/react";
import { GetServerSideProps } from "next";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
Expand All @@ -36,12 +37,17 @@ import StepsButtons from "~/components/offer/StepsButtons";
import CouponWrapper from "~/components/wrappers/CouponWrapper";
import OfferWrapper from "~/components/wrappers/OfferWrapper";
import StepsWrapper from "~/components/wrappers/StepsWrapper";
import { hasAccessToOffer } from "~/guards/hasAccessToOffer";
import { getItemsTermsOfUse } from "~/payload/components/CustomSelectField";
import { useAuth } from "~/providers/Auth";
import { couponAnimation } from "~/utils/animations";
import { api } from "~/utils/api";
import { getItemsExternalLink } from "~/utils/itemsOffer";

export const getServerSideProps: GetServerSideProps = async (context) => {
return hasAccessToOffer(context);
};

export default function Dashboard() {
const { user } = useAuth();

Expand Down
9 changes: 8 additions & 1 deletion webapp/src/server/api/routers/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ export const offerRouter = createTRPCRouter({
.input(
ZGetListParams.merge(
z.object({
offerId: z.number().optional(),
categoryId: z.number().optional(),
isCurrentUser: z.boolean().optional(),
})
)
)
.query(async ({ ctx, input }) => {
const { perPage, page, sort, categoryId, isCurrentUser } = input;
const { perPage, page, sort, categoryId, offerId, isCurrentUser } = input;

let where = {
...payloadWhereOfferIsValid(),
Expand All @@ -34,6 +35,12 @@ export const offerRouter = createTRPCRouter({
};
}

if (offerId) {
where.id = {
equals: offerId,
};
}

const offers = await ctx.payload.find({
collection: "offers",
limit: perPage,
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ZodError } from "zod";
import getPayloadClient from "~/payload/payloadClient";
import { jwtDecode } from "jwt-decode";

type PayloadJwtSession = {
export type PayloadJwtSession = {
id: number;
email: string;
iat: string;
Expand Down Expand Up @@ -158,6 +158,8 @@ const isAuthedAsUser = t.middleware(async ({ next, ctx }) => {
*/
export const createTRPCRouter = t.router;

export const createCallerFactory = t.createCallerFactory;

/**
* Public (unauthenticated) procedure
*
Expand Down

0 comments on commit 2f66bf6

Please sign in to comment.