Skip to content

Commit

Permalink
fix: invalidate cache when sign in/up/out (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu authored Jan 12, 2024
1 parent 4b674f7 commit 6322ee7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/app/auth/[[...path]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { redirectToAuth } from 'supertokens-auth-react';
import { EmailPasswordPreBuiltUI } from 'supertokens-auth-react/recipe/emailpassword/prebuiltui';
import SuperTokens from 'supertokens-auth-react/ui';

import { clearAllCaches } from '../../../asyncFunctions/cache/actions';

const AuthPage: NextPage = () => {
// if the user visits a page that is not handled by us (like /auth/random), then we redirect them back to the auth page.
const [loaded, setLoaded] = useState(false);

useEffect(() => {
// サインイン/アップに伴う画面表示の変更を反映するために、全ページのキャッシュを削除する。
void clearAllCaches();

if (SuperTokens.canHandleRoute([EmailPasswordPreBuiltUI])) {
setLoaded(true);
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/asyncFunctions/cache/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use server';

import { revalidatePath } from 'next/cache';

export async function clearAllCaches(): Promise<void> {
revalidatePath('/', 'layout');
}
2 changes: 1 addition & 1 deletion src/components/organisms/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { usePathname, useRouter } from 'next/navigation';
import React from 'react';
import { SuperTokensWrapper } from 'supertokens-auth-react';

import { ensureSuperTokensReactInit, setRouter } from '../../infrastructures/supertokens/frontend';
import { ensureSuperTokensReactInit, setRouter } from '../../infrastructures/supertokens/frontendConfig';
import { theme } from '../../theme';

ensureSuperTokensReactInit();
Expand Down
4 changes: 4 additions & 0 deletions src/components/organisms/SignOutMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import React from 'react';
import { FaSignOutAlt } from 'react-icons/fa';
import { signOut } from 'supertokens-auth-react/recipe/session';

import { clearAllCaches } from '../../asyncFunctions/cache/actions';

export const SignOutMenuItem: React.FC = () => {
const router = useRouter();
return (
Expand All @@ -13,6 +15,8 @@ export const SignOutMenuItem: React.FC = () => {
onClick={async () => {
await signOut();
router.push('/');
// サインイン/アップに伴う画面表示の変更を反映するために、全ページのキャッシュを削除する。
void clearAllCaches();
}}
>
サインアウト
Expand Down
File renamed without changes.

0 comments on commit 6322ee7

Please sign in to comment.