Skip to content

Commit

Permalink
feat: loginするためのエンドポイントを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Mar 21, 2024
1 parent e5d0676 commit cfcdfdb
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@azure/msal-browser": "^3.2.0",
"@azure/msal-node": "^2.6.4",
"@azure/msal-react": "^2.0.3",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/app/layout.tsx → src/app/(authed)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import './globals.css';
import '../globals.css';
import { Inter } from 'next/font/google';
import NavBar from '@/components/NavBar';
import { AuthenticatedTemplate } from '@/features/user/components/AuthenticatedTemplate';
import { MsalProvider } from '@/features/user/components/MsalProvider';
import { NeedToSignin } from '@/features/user/components/NeedToSignin';
import { UnauthenticatedTemplate } from '@/features/user/components/UnauthenticatedTemplate';
import styles from './page.module.css';
import styles from '../page.module.css';
import type { Metadata } from 'next';
import type { ReactNode } from 'react';

Expand Down
2 changes: 2 additions & 0 deletions src/app/page.tsx → src/app/(authed)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import Menu from '@/components/Menu';

const Home = () => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions src/app/(unauthed)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '../globals.css';
import { Inter } from 'next/font/google';
import styles from '../page.module.css';
import type { Metadata } from 'next';
import type { ReactNode } from 'react';
import { MsalProvider } from '@/features/user/components/MsalProvider';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Seichi Portal',
description: '整地鯖公式のポータルサイトです。',
};

const RootLayout = ({ children }: { children: ReactNode }) => {
return (
<html lang="ja">
<body className={inter.className}>
<main className={styles['main']}>
<MsalProvider>{children}</MsalProvider>
</main>
</body>
</html>
);
};

export default RootLayout;
47 changes: 47 additions & 0 deletions src/app/(unauthed)/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use client';

import { MS_APP_REDIRECT_URL } from '@/env';
import {
acquireMinecraftAccessToken,
acquireXboxLiveToken,
acquireXboxServiceSecurityToken,
} from '@/features/user/api/login';
import { saveTokenToCache } from '@/features/user/api/mcToken';
import { loginRequest } from '@/features/user/const/authConfig';
import { useMsal } from '@azure/msal-react';
import { useEffect, useTransition } from 'react';

const Home = () => {
const { instance } = useMsal();
const [_isPending, startTransition] = useTransition();

if (typeof window === 'undefined') return <></>;

useEffect(() => {
(async () => {
await instance.initialize();
const request = {
...loginRequest,
redirectStartPage: MS_APP_REDIRECT_URL,
};
await instance.loginRedirect(request).then(async () => {
const token = await instance
.acquireTokenSilent(loginRequest)
.catch(async () => instance.acquireTokenPopup(loginRequest))
.then((r) => r.accessToken);

console.log(token);

const xblToken = await acquireXboxLiveToken(token);
const xstsToken = await acquireXboxServiceSecurityToken(xblToken);
const mcAccessToken = await acquireMinecraftAccessToken(xstsToken);

saveTokenToCache(mcAccessToken);
});
})();
}, []);

return <></>;
};

export default Home;
8 changes: 6 additions & 2 deletions src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
'use client';

import MenuIcon from '@mui/icons-material/Menu';
import { Box, AppBar, Toolbar, IconButton, Typography } from '@mui/material';
import { Link } from './Link';
import { AuthenticatedTemplate } from '../features/user/components/AuthenticatedTemplate';
import { SigninButton } from '../features/user/components/SigninButton';
import { SignoutButton } from '../features/user/components/SignoutButton';
import { UnauthenticatedTemplate } from '../features/user/components/UnauthenticatedTemplate';
import {
AuthenticatedTemplate,
UnauthenticatedTemplate,
} from '@azure/msal-react';

const NavBar = () => {
return (
Expand Down
14 changes: 0 additions & 14 deletions src/features/user/api/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,3 @@ export const acquireMinecraftProfile = async ({

return result;
};

export const sendJsonToBackend = async ({
token,
}: Awaited<ReturnType<typeof acquireMinecraftAccessToken>>) => {
const URL = 'http://localhost:9000/forms';

await fetch(URL, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
});
};
4 changes: 3 additions & 1 deletion src/features/user/components/SigninButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export const SigninButton = () => {
const [_isPending, startTransition] = useTransition();

const onClick = async () => {
await instance.initialize();
instance.loginRedirect();

const token = await instance
.acquireTokenSilent(loginRequest)
.catch(async () => instance.acquireTokenPopup(loginRequest))
.then((r) => r.accessToken);

startTransition(async () => {
const xblToken = await acquireXboxLiveToken(token);
const xstsToken = await acquireXboxServiceSecurityToken(xblToken);
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/components/SignoutButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const SignoutButton = () => {
const router = useRouter();

const onClick = async () => {
await instance.logoutPopup();
instance.logoutRedirect();
clearCachedToken();
router.refresh();
};
Expand Down
8 changes: 4 additions & 4 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const middleware = async (request: NextRequest) => {
return;
}

if (!!(await getCachedToken(request.cookies))) {
return;
}
// if (!!(await getCachedToken(request.cookies))) {
// return;
// }

return NextResponse.redirect(new URL('/', request.url));
// return NextResponse.redirect(new URL('/', request.url));
};
101 changes: 100 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
resolved "https://registry.yarnpkg.com/@azure/msal-common/-/msal-common-14.1.0.tgz#b3a1de13dddd0eb86b1c6a7ef052a87d3ce941b7"
integrity sha512-xphmhcfl5VL+uq5//VKMwQn+wfEZLMKNpFCcMi8Ur8ej5UT166g6chBsxgMzc9xo9Y24R9FB3m/tjDiV03xMIA==

"@azure/[email protected]":
version "14.7.1"
resolved "https://registry.yarnpkg.com/@azure/msal-common/-/msal-common-14.7.1.tgz#b13443fbacc87ce2019a91e81a6582ea73847c75"
integrity sha512-v96btzjM7KrAu4NSEdOkhQSTGOuNUIIsUdB8wlyB9cdgl5KqEKnTonHUZ8+khvZ6Ap542FCErbnTyDWl8lZ2rA==

"@azure/msal-node@^2.6.4":
version "2.6.4"
resolved "https://registry.yarnpkg.com/@azure/msal-node/-/msal-node-2.6.4.tgz#457bd86a52461178ab2d1ba3d9d6705d95b2186e"
integrity sha512-nNvEPx009/80UATCToF+29NZYocn01uKrB91xtFr7bSqkqO1PuQGXRyYwryWRztUrYZ1YsSbw9A+LmwOhpVvcg==
dependencies:
"@azure/msal-common" "14.7.1"
jsonwebtoken "^9.0.0"
uuid "^8.3.0"

"@azure/msal-react@^2.0.3":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@azure/msal-react/-/msal-react-2.0.4.tgz#b85c2028dd53d2362881d0dbca0cbf3830f2780a"
Expand Down Expand Up @@ -1052,6 +1066,11 @@ braces@^3.0.2:
dependencies:
fill-range "^7.0.1"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
integrity sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==

buffer@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
Expand Down Expand Up @@ -1357,6 +1376,13 @@ eastasianwidth@^0.2.0:
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==

[email protected]:
version "1.0.11"
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
dependencies:
safe-buffer "^5.0.1"

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
Expand Down Expand Up @@ -2431,6 +2457,22 @@ jsonparse@^1.3.1:
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg==

jsonwebtoken@^9.0.0:
version "9.0.2"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz#65ff91f4abef1784697d40952bb1998c504caaf3"
integrity sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==
dependencies:
jws "^3.2.2"
lodash.includes "^4.3.0"
lodash.isboolean "^3.0.3"
lodash.isinteger "^4.0.4"
lodash.isnumber "^3.0.3"
lodash.isplainobject "^4.0.6"
lodash.isstring "^4.0.1"
lodash.once "^4.0.0"
ms "^2.1.1"
semver "^7.5.4"

jss-plugin-camel-case@^10.5.1:
version "10.10.0"
resolved "https://registry.yarnpkg.com/jss-plugin-camel-case/-/jss-plugin-camel-case-10.10.0.tgz#27ea159bab67eb4837fa0260204eb7925d4daa1c"
Expand Down Expand Up @@ -2511,6 +2553,23 @@ [email protected], jss@^10.5.1:
object.assign "^4.1.4"
object.values "^1.1.6"

jwa@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
dependencies:
buffer-equal-constant-time "1.0.1"
ecdsa-sig-formatter "1.0.11"
safe-buffer "^5.0.1"

jws@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
dependencies:
jwa "^1.4.1"
safe-buffer "^5.0.1"

keyv@^4.5.3:
version "4.5.3"
resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.5.3.tgz#00873d2b046df737963157bd04f294ca818c9c25"
Expand Down Expand Up @@ -2550,11 +2609,46 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash.includes@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
integrity sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==

lodash.isboolean@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
integrity sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==

lodash.isinteger@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
integrity sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==

lodash.isnumber@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
integrity sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==

lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
integrity sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
integrity sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==

lodash.merge@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.once@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
integrity sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==

log-symbols@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-4.1.0.tgz#3fbdbb95b4683ac9fc785111e792e558d4abd503"
Expand Down Expand Up @@ -3233,7 +3327,7 @@ safe-array-concat@^1.0.1:
has-symbols "^1.0.3"
isarray "^2.0.5"

safe-buffer@~5.2.0:
safe-buffer@^5.0.1, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down Expand Up @@ -3671,6 +3765,11 @@ util-deprecate@^1.0.1:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

uuid@^8.3.0:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

validate-npm-package-name@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/validate-npm-package-name/-/validate-npm-package-name-5.0.0.tgz#f16afd48318e6f90a1ec101377fa0384cfc8c713"
Expand Down

0 comments on commit cfcdfdb

Please sign in to comment.