Skip to content

Commit

Permalink
feat: ログインページから指定のページにcallbackできるように
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Mar 22, 2024
1 parent 9df67aa commit 99ccd85
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/app/(unauthed)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { InteractionStatus } from '@azure/msal-browser';
import { useMsal } from '@azure/msal-react';
import { useRouter } from 'next/navigation';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
import {
acquireMinecraftAccessToken,
Expand All @@ -17,6 +17,8 @@ const Home = () => {
const { instance, inProgress, accounts } = useMsal();
const [isInitialized, setState] = useState(false);
const router = useRouter();
const searchParams = useSearchParams();
const callbackUrl = searchParams.get('callbackUrl');

useEffect(() => {
(async () => {
Expand Down Expand Up @@ -45,12 +47,20 @@ const Home = () => {

saveTokenToCache(mcAccessToken);
});
router.push('/');

if (callbackUrl) {
router.push(callbackUrl);
} else {
router.push('/');
}
} else if (isInitialized && inProgress === InteractionStatus.None) {
const callbackQuery = new URLSearchParams({
callbackUrl: callbackUrl ?? '/',
}).toString();
instance
.loginRedirect({
...loginRequest,
redirectStartPage: '/login',
redirectStartPage: `/login?${callbackQuery}`,
})
.catch((e) => console.log(e));
}
Expand Down

0 comments on commit 99ccd85

Please sign in to comment.