Skip to content

Commit 22cd93c

Browse files
refactor: fix migrateChatUser cache issue and add clear-cache page
Agent-Logs-Url: https://github.com/ut-code/my-code/sessions/549c963e-91ce-4740-a529-919ee34f3e49 Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
1 parent 3f0cfa0 commit 22cd93c

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

app/accountMenu.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { authClient } from "@/lib/auth-client";
4-
import { usePathname } from "next/navigation";
4+
import { usePathname, useRouter } from "next/navigation";
55
import { useEffect } from "react";
66

77
export function AutoAnonymousLogin() {
@@ -18,6 +18,7 @@ export function AutoAnonymousLogin() {
1818
export function AccountMenu() {
1919
const { data: session, isPending } = authClient.useSession();
2020
const pathname = usePathname();
21+
const router = useRouter();
2122

2223
const signout = () => {
2324
if (
@@ -27,7 +28,7 @@ export function AccountMenu() {
2728
) {
2829
authClient.signOut({
2930
fetchOptions: {
30-
onSuccess: () => window.location.reload(),
31+
onSuccess: () => router.refresh(),
3132
},
3233
});
3334
}
@@ -36,7 +37,7 @@ export function AccountMenu() {
3637
if (window.confirm("チャット履歴は削除され、アクセスできなくなります。")) {
3738
authClient.signOut({
3839
fetchOptions: {
39-
onSuccess: () => window.location.reload(),
40+
onSuccess: () => router.refresh(),
4041
},
4142
});
4243
}
@@ -102,7 +103,7 @@ export function AccountMenu() {
102103
onClick={() =>
103104
authClient.signIn.social({
104105
provider: "github",
105-
callbackURL: pathname,
106+
callbackURL: `/clear-cache?redirect=${encodeURIComponent(pathname)}`,
106107
})
107108
}
108109
>
@@ -114,7 +115,7 @@ export function AccountMenu() {
114115
onClick={() =>
115116
authClient.signIn.social({
116117
provider: "google",
117-
callbackURL: pathname,
118+
callbackURL: `/clear-cache?redirect=${encodeURIComponent(pathname)}`,
118119
})
119120
}
120121
>

app/actions/clearUserCache.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use server";
2+
3+
import { initContext, cacheKeyForPage } from "@/lib/chatHistory";
4+
import { updateTag } from "next/cache";
5+
import { getPagesList } from "@/lib/docs";
6+
import { isCloudflare } from "@/lib/detectCloudflare";
7+
8+
export async function clearUserCacheAction() {
9+
const ctx = await initContext();
10+
if (!ctx.userId) return;
11+
12+
const pagesList = await getPagesList();
13+
for (const lang of pagesList) {
14+
for (const page of lang.pages) {
15+
updateTag(cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId));
16+
}
17+
}
18+
19+
if (isCloudflare()) {
20+
const cache = await caches.open("chatHistory");
21+
for (const lang of pagesList) {
22+
for (const page of lang.pages) {
23+
await cache.delete(
24+
cacheKeyForPage({ lang: lang.id, page: page.slug }, ctx.userId)
25+
);
26+
}
27+
}
28+
}
29+
}

app/clear-cache/page.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use client";
2+
3+
import { clearUserCacheAction } from "@/actions/clearUserCache";
4+
import { useRouter, useSearchParams } from "next/navigation";
5+
import { Suspense, useEffect } from "react";
6+
7+
function ClearCacheContent() {
8+
const router = useRouter();
9+
const searchParams = useSearchParams();
10+
11+
useEffect(() => {
12+
const redirectParam = searchParams.get("redirect") ?? "/";
13+
// Only allow relative redirects to prevent open redirect attacks
14+
const redirect = redirectParam.startsWith("/") ? redirectParam : "/";
15+
clearUserCacheAction()
16+
.catch(() => {})
17+
.finally(() => {
18+
router.replace(redirect);
19+
});
20+
}, [router, searchParams]);
21+
22+
return null;
23+
}
24+
25+
export default function ClearCachePage() {
26+
return (
27+
<Suspense>
28+
<ClearCacheContent />
29+
</Suspense>
30+
);
31+
}

app/lib/chatHistory.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { and, asc, eq, exists } from "drizzle-orm";
66
import { Auth } from "better-auth";
77
import { updateTag } from "next/cache";
88
import { isCloudflare } from "./detectCloudflare";
9-
import { getPagesList, LangId, PagePath, PageSlug, SectionId } from "./docs";
9+
import { LangId, PagePath, PageSlug, SectionId } from "./docs";
1010

1111
export interface CreateChatMessage {
1212
role: "user" | "ai" | "error";
@@ -279,22 +279,4 @@ export async function migrateChatUser(oldUserId: string, newUserId: string) {
279279
.update(chat)
280280
.set({ userId: newUserId })
281281
.where(eq(chat.userId, oldUserId));
282-
const pagesList = await getPagesList();
283-
for (const lang of pagesList) {
284-
for (const page of lang.pages) {
285-
updateTag(
286-
cacheKeyForPage({ lang: lang.id, page: page.slug }, newUserId)
287-
);
288-
}
289-
}
290-
if (isCloudflare()) {
291-
const cache = await caches.open("chatHistory");
292-
for (const lang of pagesList) {
293-
for (const page of lang.pages) {
294-
await cache.delete(
295-
cacheKeyForPage({ lang: lang.id, page: page.slug }, newUserId)
296-
);
297-
}
298-
}
299-
}
300282
}

0 commit comments

Comments
 (0)