Skip to content

Commit 77a7eb9

Browse files
committed
Add user hocs and update link path
1 parent 83e5c85 commit 77a7eb9

File tree

10 files changed

+180
-6
lines changed

10 files changed

+180
-6
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ yarn-error.log*
3636
# typescript
3737
*.tsbuildinfo
3838

39-
storybook-static
39+
storybook-static
40+
41+
.env

components/Landing/_heroes/AnimationHero.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { NextPage } from 'next';
21
import React from 'react';
32
import Lottie from 'react-lottie';
43
import animationData from '../../../public/lotties/hello-bonjour-hola-guten-tag.json';
54
import { Box, Typography } from '@mui/material';
65
import type { ReactNode } from 'react';
76

8-
const AnimationHero: NextPage = (props: ReactNode) => {
7+
const AnimationHero = (props: ReactNode) => {
98
const defaultOptions = {
109
loop: true,
1110
autoplay: true,
@@ -61,6 +60,7 @@ const AnimationHero: NextPage = (props: ReactNode) => {
6160
ipsum dolor sit amet consectetur adipisicing elit. In, officia!
6261
</Typography>
6362
<Box mt={20} mb={20} mr={0} ml={0}>
63+
{/* @ts-ignore */}
6464
<Lottie options={defaultOptions} height={400} width={400} />
6565
</Box>
6666
</Box>

components/Landing/_heroes/CTAWithIllustration.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export default function CallToActionWithIllustration() {
4040
</Text>
4141
<Stack spacing={6} direction={'row'}>
4242
<Button
43+
as={'a'}
44+
href={'/login'}
4345
rounded={'full'}
4446
px={6}
4547
colorScheme={'orange'}

components/Landing/_heroes/LandingHero.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextPage } from 'next';
33
import Image from 'next/image';
44
import React from 'react';
55

6-
const LandingHero: NextPage = () => {
6+
const LandingHero = () => {
77
return (
88
<Box
99
padding={'0px'}

components/Landing/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextPage } from 'next';
22
import React from 'react';
33
import Image from 'next/image';
4-
import { Box, Container, Grid, Typography } from '@mui/material';
4+
import { Box } from '@mui/material';
55
import LandingHero from './_heroes/LandingHero';
66
import AnimationHero from './_heroes/AnimationHero';
77
import CallToActionWithIllustration from './_heroes/CTAWithIllustration';
@@ -11,7 +11,7 @@ import SplitScreenWithImage from './_heroes/SplitScreenWithImage';
1111
import WithBackgroundImageAndGradient from './_heroes/WithBackgroundImageAndGradient';
1212
import SimpleThreeColumnGrid from './_heroes/SimpleThreeColumnGrid';
1313

14-
const Landing: NextPage = () => {
14+
const Landing = () => {
1515
return (
1616
<Box>
1717
<CallToActionWithIllustration />

constants/index.ts

Whitespace-only changes.

hocs/users-provider/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { createContext, FunctionComponent, useContext } from 'react';
2+
import { User } from 'utils/authorization';
3+
4+
const UserProviderContext = createContext({
5+
user: null as User | null,
6+
});
7+
8+
export const useUserProvider = () => useContext(UserProviderContext);
9+
10+
export const UserProvider: FunctionComponent<{
11+
user: User | null;
12+
}> = ({ children, user }) => {
13+
return (
14+
<UserProviderContext.Provider
15+
value={{
16+
user: user || null,
17+
}}
18+
>
19+
{children}
20+
</UserProviderContext.Provider>
21+
);
22+
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@mui/material": "^5.8.4",
2323
"@reduxjs/toolkit": "^1.8.4",
2424
"framer-motion": "^5.6.0",
25+
"jsonwebtoken": "^8.5.1",
2526
"next": "12.0.8",
2627
"react": "17.0.2",
2728
"react-dom": "17.0.2",
@@ -38,6 +39,7 @@
3839
"@storybook/builder-webpack5": "^6.4.13",
3940
"@storybook/manager-webpack5": "^6.4.13",
4041
"@storybook/react": "^6.4.13",
42+
"@types/jsonwebtoken": "^8.5.8",
4143
"@types/node": "17.0.8",
4244
"@types/react": "17.0.30",
4345
"@types/react-dom": "^18.0.6",

utils/authorization.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { IncomingMessage } from "http";
2+
import jwt from "jsonwebtoken";
3+
import { NextApiRequest } from "next";
4+
import { NextApiRequestCookies } from "next/dist/server/api-utils";
5+
6+
export type User = {
7+
id: string;
8+
name: string;
9+
email: string;
10+
surname: string;
11+
role: string;
12+
}
13+
14+
export type MaybeAuthorized = {
15+
user: User | null;
16+
}
17+
18+
export type Authorized = {
19+
registeredUser: User;
20+
}
21+
22+
export async function authorize(cookies: { [key: string]: string }): Promise<unknown> {
23+
return new Promise((resolve, reject) => {
24+
const authToken = cookies.auth_token;
25+
if (!authToken) return reject(new Error("No auth token"));
26+
27+
jwt.verify(
28+
authToken,
29+
process.env.JWT_SECRET as string,
30+
(err: any, decoded: any) => {
31+
if (err) return reject(new Error("Invalid auth token"));
32+
33+
resolve(decoded)
34+
}
35+
)
36+
})
37+
}
38+
39+
export type AuthorizationResult = {
40+
authorized: boolean;
41+
user: User | null;
42+
}
43+
44+
export async function checkAuthorization(
45+
req: NextApiRequest | (IncomingMessage & { cookies: NextApiRequestCookies })
46+
): Promise<AuthorizationResult> {
47+
try {
48+
const decoded = await authorize(req.cookies);
49+
return {
50+
authorized: true,
51+
user: decoded as User
52+
}
53+
} catch (e) {
54+
return {
55+
authorized: false,
56+
user: null
57+
}
58+
}
59+
}

yarn.lock

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3398,6 +3398,13 @@
33983398
resolved "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz"
33993399
integrity "sha1-7ihweulOEdK4J7y+UnC86n8+ce4= sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ=="
34003400

3401+
"@types/jsonwebtoken@^8.5.8":
3402+
version "8.5.8"
3403+
resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-8.5.8.tgz#01b39711eb844777b7af1d1f2b4cf22fda1c0c44"
3404+
integrity sha512-zm6xBQpFDIDM6o9r6HSgDeIcLy82TKWctCXEPbJJcXb5AKmi5BNNdLXneixK4lplX3PqIVcwLBCGE/kAGnlD4A==
3405+
dependencies:
3406+
"@types/node" "*"
3407+
34013408
34023409
version "4.6.6"
34033410
resolved "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz"
@@ -4674,6 +4681,11 @@ [email protected]:
46744681
dependencies:
46754682
node-int64 "^0.4.0"
46764683

4684+
4685+
version "1.0.1"
4686+
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
4687+
integrity sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==
4688+
46774689
buffer-from@^1.0.0:
46784690
version "1.1.2"
46794691
resolved "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz"
@@ -5706,6 +5718,13 @@ duplexify@^3.4.2, duplexify@^3.6.0:
57065718
readable-stream "^2.0.0"
57075719
stream-shift "^1.0.0"
57085720

5721+
5722+
version "1.0.11"
5723+
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
5724+
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
5725+
dependencies:
5726+
safe-buffer "^5.0.1"
5727+
57095728
57105729
version "1.1.1"
57115730
resolved "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz"
@@ -7922,6 +7941,22 @@ jsonfile@^6.0.1:
79227941
optionalDependencies:
79237942
graceful-fs "^4.1.6"
79247943

7944+
jsonwebtoken@^8.5.1:
7945+
version "8.5.1"
7946+
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
7947+
integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==
7948+
dependencies:
7949+
jws "^3.2.2"
7950+
lodash.includes "^4.3.0"
7951+
lodash.isboolean "^3.0.3"
7952+
lodash.isinteger "^4.0.4"
7953+
lodash.isnumber "^3.0.3"
7954+
lodash.isplainobject "^4.0.6"
7955+
lodash.isstring "^4.0.1"
7956+
lodash.once "^4.0.0"
7957+
ms "^2.1.1"
7958+
semver "^5.6.0"
7959+
79257960
"jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.2.1:
79267961
version "3.2.1"
79277962
resolved "https://registry.npmjs.org/jsx-ast-utils/-/jsx-ast-utils-3.2.1.tgz"
@@ -7935,6 +7970,23 @@ junk@^3.1.0:
79357970
resolved "https://registry.npmjs.org/junk/-/junk-3.1.0.tgz"
79367971
integrity sha512-pBxcB3LFc8QVgdggvZWyeys+hnrNWg4OcZIU/1X59k5jQdLBlCsYGRQaz234SqoRLTCgMH00fY0xRJH+F9METQ==
79377972

7973+
jwa@^1.4.1:
7974+
version "1.4.1"
7975+
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
7976+
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
7977+
dependencies:
7978+
buffer-equal-constant-time "1.0.1"
7979+
ecdsa-sig-formatter "1.0.11"
7980+
safe-buffer "^5.0.1"
7981+
7982+
jws@^3.2.2:
7983+
version "3.2.2"
7984+
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
7985+
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
7986+
dependencies:
7987+
jwa "^1.4.1"
7988+
safe-buffer "^5.0.1"
7989+
79387990
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
79397991
version "3.2.2"
79407992
resolved "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz"
@@ -8101,6 +8153,36 @@ lodash.debounce@^4.0.8:
81018153
resolved "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz"
81028154
integrity "sha1-gteb/zCmfEAF/9XiUVMArZyk168= sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow=="
81038155

8156+
lodash.includes@^4.3.0:
8157+
version "4.3.0"
8158+
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
8159+
integrity sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==
8160+
8161+
lodash.isboolean@^3.0.3:
8162+
version "3.0.3"
8163+
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
8164+
integrity sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==
8165+
8166+
lodash.isinteger@^4.0.4:
8167+
version "4.0.4"
8168+
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
8169+
integrity sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==
8170+
8171+
lodash.isnumber@^3.0.3:
8172+
version "3.0.3"
8173+
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
8174+
integrity sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==
8175+
8176+
lodash.isplainobject@^4.0.6:
8177+
version "4.0.6"
8178+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
8179+
integrity sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==
8180+
8181+
lodash.isstring@^4.0.1:
8182+
version "4.0.1"
8183+
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
8184+
integrity sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==
8185+
81048186
lodash.merge@^4.6.2:
81058187
version "4.6.2"
81068188
resolved "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz"
@@ -8111,6 +8193,11 @@ [email protected]:
81118193
resolved "https://registry.npmjs.org/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz"
81128194
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==
81138195

8196+
lodash.once@^4.0.0:
8197+
version "4.1.1"
8198+
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
8199+
integrity sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==
8200+
81148201
81158202
version "4.5.0"
81168203
resolved "https://registry.npmjs.org/lodash.uniq/-/lodash.uniq-4.5.0.tgz"

0 commit comments

Comments
 (0)