Skip to content

Commit

Permalink
fix: ログアウト時に cookie の削除がうまくいかない不具合を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Jul 14, 2024
1 parent 75f9162 commit 5b36c20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/app/(unauthed)/logout/page.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
'use client';

import { useMsal } from '@azure/msal-react';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
import { clearCachedToken } from '@/user-token/mcToken';

const Home = () => {
const { instance, accounts } = useMsal();
const router = useRouter();

useEffect(() => {
(async () => {
if (accounts[0]) {
await instance.initialize().catch((e) => console.log(e));
clearCachedToken();
await clearCachedToken();
await instance.logoutRedirect({
account: instance.getAccountByHomeId(accounts[0].homeAccountId),
postLogoutRedirectUri: '/',
});
} else {
router.push('/');
}
})().catch((e) => console.log(e));
}, [accounts, instance]);
}, [accounts, instance, router]);

return <></>;
};
Expand Down
9 changes: 6 additions & 3 deletions src/user-token/mcToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ export const saveTokenToCache = (tokenWithExpires: {
});
};

export const clearCachedToken = () => {
const store = cookies();
export const clearCachedToken = async (): Promise<void> => {
return new Promise((resolve, _reject) => {
const store = cookies();

store.delete(KEY);
store.delete(KEY);
resolve();
});
};

0 comments on commit 5b36c20

Please sign in to comment.