Skip to content

Commit

Permalink
feat: magiclink to already signup users from cej
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Oct 21, 2024
1 parent d783e17 commit b1dbd67
Show file tree
Hide file tree
Showing 5 changed files with 3,876 additions and 4 deletions.
30 changes: 26 additions & 4 deletions webapp/src/pages/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { Box, Divider, Flex, Grid, Heading, Link } from "@chakra-ui/react";
import Cookies from "js-cookie";
import jwt from "jsonwebtoken";
import { GetServerSideProps } from "next";
import NextImage from "next/image";
import NextLink from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import OfferCard from "~/components/cards/OfferCard";
import Jumbotron from "~/components/landing/Jumbotron";
import CategoriesList from "~/components/lists/CategoriesList";
import TagsList from "~/components/lists/TagsList";
import SearchBar from "~/components/SearchBar";
import getPayloadClient from "~/payload/payloadClient";
import { ZWidgetToken } from "~/server/types";
import { api } from "~/utils/api";
import NextImage from "next/image";
import NextLink from "next/link";
import { decryptData } from "~/utils/tools";
import { ZWidgetToken } from "~/server/types";

type WidgetProps = {
initialToken: string;
Expand Down Expand Up @@ -223,7 +224,28 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}

const decoded = jwt.verify(widgetToken, process.env.WIDGET_SECRET_JWT!);
ZWidgetToken.parse(decoded);
const tokenObject = ZWidgetToken.parse(decoded);
const cjeUserId = decryptData(
tokenObject.user_id,
process.env.WIDGET_SECRET_DATA_ENCRYPTION!
);

const payload = await getPayloadClient({ seed: false });
const users = await payload.find({
collection: "users",
where: {
cej_id: { equals: cjeUserId },
},
});

if (!!users.docs.length) {
return {
redirect: {
destination: `/widget/magiclink?widgetToken=${widgetToken}`,
permanent: false,
},
};
}

return {
props: {
Expand Down
90 changes: 90 additions & 0 deletions webapp/src/pages/widget/magiclink/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Button, Flex, Heading, Icon } from "@chakra-ui/react";
import Cookies from "js-cookie";
import NextImage from "next/image";
import NextLink from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { HiArrowTopRightOnSquare } from "react-icons/hi2";

export default function WidgetMagicLinkPage() {
const router = useRouter();
const [isCookieSet, setIsCookieSet] = useState<boolean>(false);

useEffect(() => {
const { widgetToken: initialToken } = router.query;
if (initialToken) {
Cookies.set(
process.env.NEXT_PUBLIC_WIDGET_TOKEN_NAME!,
initialToken as string,
{
expires: 7,
path: "/",
secure: true,
sameSite: "none",
}
);
setIsCookieSet(true);
}
}, [router.query, router]);

useEffect(() => {
if (isCookieSet) {
const newQuery = { ...router.query };
delete newQuery.widgetToken;
router.replace(
{
pathname: router.pathname,
query: newQuery,
},
undefined,
{ shallow: true }
);
}
}, [isCookieSet]);

if (!isCookieSet) return;

return (
<Flex
direction={"column"}
bg="primary"
pt={18}
pb={40}
px={8}
alignItems="center"
minH="full"
>
<Flex mt={16} px={6} direction={"column"} alignItems={"center"}>
<Flex flexWrap={"wrap"} gap={6} mb={4}>
<NextImage
src="/images/landing/ministere-travail-blanc.png"
alt="Logo marianne du gouvernement français"
width={70}
height={70}
/>
<NextImage
src="/images/cje-logo-white-blue.svg"
alt="Logo de l'application Carte Jeune Engagé"
width={60}
height={33}
/>
</Flex>
<Heading size={"xl"} color="white" textAlign={"center"} my={10}>
Tous les codes de réduction sont dans l’appli carte “jeune engagé”
</Heading>
<Button
as={NextLink}
href={`/?widgetToken=${Cookies.get(process.env.NEXT_PUBLIC_WIDGET_TOKEN_NAME!)}`}
target="_blank"
colorScheme="whiteBtn"
color="black"
fontWeight={800}
w="full"
mt={10}
>
Accéder à l'application <Icon ml={2} as={HiArrowTopRightOnSquare} />
</Button>
</Flex>
</Flex>
);
}
1 change: 1 addition & 0 deletions webapp/src/payload/collections/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export const Users: CollectionConfig = {
{
name: "cej_id",
type: "text",
unique: true,
admin: {
readOnly: true,
},
Expand Down
Loading

0 comments on commit b1dbd67

Please sign in to comment.