Skip to content

Commit

Permalink
Merge pull request #4 from DarkAnyx/jwt-auth
Browse files Browse the repository at this point in the history
Added api call for files
  • Loading branch information
r34son authored Feb 18, 2023
2 parents 4e0e6c1 + 07c7220 commit 9441bcb
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@mui/icons-material": "^5.11.0",
"@mui/material": "^5.11.7",
"@next/font": "^13.1.6",
"axios": "^1.3.2",
"i18next": "^22.4.9",
"next": "13.1.6",
"next-auth": "^4.19.2",
Expand Down
1 change: 1 addition & 0 deletions environment.d.ts → src/app/typings/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare global {
interface ProcessEnv {
NEXT_PUBLIC_GOOGLE_CLIENT_ID: string;
NEXT_PUBLIC_GOOGLE_CLIENT_SECRET: string;
NEXT_PUBLIC_API_URL: string;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/app/typings/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'next-auth';

declare module 'next-auth' {
interface User {
accessToken?: string;
refreshToken?: string;
}
}

declare module 'next-auth/jwt' {
interface JWT {
accessToken?: string;
}
}
7 changes: 7 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { withAuth } from 'next-auth/middleware';

export default withAuth({
callbacks: {
authorized: ({ token }) => Boolean(token?.accessToken),
},
});
19 changes: 19 additions & 0 deletions src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { auth } from '@/shared/api';
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';

Expand All @@ -8,4 +9,22 @@ export default NextAuth({
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
signIn: async ({ account, user }) => {
if (account?.provider === 'google' && account?.access_token) {
const { data } = await auth.authorizeByGoogle(account?.access_token);
user.accessToken = data.accessToken;
user.refreshToken = data.refreshToken;
return true;
}
return false;
},
jwt: ({ token, user }) => {
if (user?.accessToken) {
token.accessToken = user.accessToken;
}

return token;
},
},
});
30 changes: 21 additions & 9 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Box, Container, Typography } from '@mui/material';
import { GetStaticProps } from 'next';
import { GetServerSideProps } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useTranslation } from 'next-i18next';
import { setAuthToken } from '@/shared/api/base';
import { file } from '@/shared/api';
import { getToken } from 'next-auth/jwt';

interface IndexProps {}
interface IndexProps {
files: unknown[];
}

const Index = () => {
const Index = (_props: IndexProps) => {
const { t } = useTranslation();

return (
Expand All @@ -27,12 +32,19 @@ const Index = () => {
);
};

export const getStaticProps: GetStaticProps<IndexProps> = async ({
export const getServerSideProps: GetServerSideProps<IndexProps> = async ({
req,
locale,
}) => ({
props: {
...(await serverSideTranslations(locale ?? 'en')),
},
});
}) => {
const token = await getToken({ req });
setAuthToken(token?.accessToken!);
const { data: files } = await file.getFiles();
return {
props: {
files,
...(await serverSideTranslations(locale ?? 'en')),
},
};
};

export default Index;
16 changes: 16 additions & 0 deletions src/shared/api/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { api } from './base';

interface AuthResponse {
accessToken: string;
refreshToken: string;
}

export const authorizeByGoogle = (token: string) =>
api.post<AuthResponse>('/auth/google-authentication', { token });

export const refreshToken = (refreshToken: string) =>
api.post('/auth/refresh', null, {
headers: { Authorization: `Bearer ${refreshToken}` },
});

export const logout = () => api.get('/auth/logout');
10 changes: 10 additions & 0 deletions src/shared/api/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import axios from 'axios';

export const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: { 'Content-Type': 'application/json' },
});

export const setAuthToken = (accessToken: string) => {
api.defaults.headers.common.Authorization = `Bearer ${accessToken}`;
};
3 changes: 3 additions & 0 deletions src/shared/api/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { api } from './base';

export const getFiles = () => api.get('/files');
2 changes: 2 additions & 0 deletions src/shared/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as auth from './auth';
export * as file from './file';
57 changes: 57 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@ astral-regex@^2.0.0:
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

available-typed-arrays@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7"
Expand All @@ -695,6 +700,15 @@ axe-core@^4.6.2:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.6.3.tgz#fc0db6fdb65cc7a80ccf85286d91d64ababa3ece"
integrity sha512-/BQzOX780JhsxDnPpH4ZiyrJAzcd8AfzFPkv+89veFSr1rcMjuq2JDCwypKaPeB6ljHp9KjXhPpjgCvQlWYuqg==

axios@^1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.2.tgz#7ac517f0fa3ec46e0e636223fd973713a09c72b3"
integrity sha512-1M3O703bYqYuPhbHeya5bnhpYVsDDRyQSabNja04mZtboLNSuZ4YrltestrLXfHgmzua4TpUqRiVKbiQuo2epw==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axobject-query@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-3.1.1.tgz#3b6e5c6d4e43ca7ba51c5babf99d22a9c68485e1"
Expand Down Expand Up @@ -838,6 +852,13 @@ colorette@^2.0.19:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.19.tgz#cdf044f47ad41a0f4b56b3a0d5b4e6e1a2d5a798"
integrity sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

commander@^9.4.1:
version "9.5.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.5.0.tgz#bc08d1eb5cedf7ccb797a96199d41c7bc3e60d30"
Expand Down Expand Up @@ -953,6 +974,11 @@ define-properties@^1.1.3, define-properties@^1.1.4:
has-property-descriptors "^1.0.0"
object-keys "^1.1.1"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
Expand Down Expand Up @@ -1417,13 +1443,27 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
integrity sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==
dependencies:
is-callable "^1.1.3"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -2092,6 +2132,18 @@ micromatch@^4.0.4, micromatch@^4.0.5:
braces "^3.0.2"
picomatch "^2.3.1"

[email protected]:
version "1.52.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

mimic-fn@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
Expand Down Expand Up @@ -2478,6 +2530,11 @@ prop-types@^15.6.2, prop-types@^15.8.1:
object-assign "^4.1.1"
react-is "^16.13.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

punycode@^2.1.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f"
Expand Down

0 comments on commit 9441bcb

Please sign in to comment.