Skip to content

Commit ab7d9e0

Browse files
committed
move middleware search param logic to client decoder
1 parent acdb533 commit ab7d9e0

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/features/decoder/components/token-decoder.component.tsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { DecodedPayloadOutputComponent } from "@/features/decoder/components/dec
66
import { JwtInputComponent } from "@/features/decoder/components/jwt-input.component";
77
import { SecretKeyInputComponent } from "@/features/decoder/components/secret-key-input.component";
88
import { useDecoderStore } from "@/features/decoder/services/decoder.store";
9-
import { useRouter } from "next/navigation";
10-
import {
11-
SupportedTokenHashParamValues
12-
} from "@/libs/config/project.constants";
9+
import { usePathname, useRouter, useSearchParams } from "next/navigation";
10+
import { SupportedTokenHashParamValues } from "@/libs/config/project.constants";
1311
import { HomeDictionaryModel } from "@/features/localization/models/home-dictionary.model";
1412
import { ClaimDescriptionVisibilityValues } from "@/features/common/values/claim-description-visibility.values";
1513
import { useDebuggerStore } from "@/features/debugger/services/debugger.store";
@@ -41,6 +39,8 @@ export const TokenDecoderComponent: React.FC<TokenDecoderComponentProps> = ({
4139
headlineConfig,
4240
}) => {
4341
const isMounted = useRef(false);
42+
const searchParams = useSearchParams();
43+
const pathname = usePathname();
4444

4545
const router = useRouter();
4646

@@ -51,8 +51,25 @@ export const TokenDecoderComponent: React.FC<TokenDecoderComponentProps> = ({
5151

5252
useEffect(() => {
5353
const handleHashChange = () => {
54-
console.count("hash handler fired")
55-
const hash = window.location.hash.substring(1);
54+
const tokenParam =
55+
searchParams.get("token") ||
56+
searchParams.get("id_token") ||
57+
searchParams.get("access_token") ||
58+
searchParams.get("value");
59+
const isHomePage = pathname === "/";
60+
const hash = tokenParam
61+
? `token=${tokenParam}`
62+
: window.location.hash.substring(1);
63+
64+
if (tokenParam && isHomePage) {
65+
const currentParams = new URLSearchParams(searchParams.toString());
66+
currentParams.delete("token");
67+
currentParams.delete("id_token");
68+
currentParams.delete("access_token");
69+
currentParams.delete("value");
70+
router.replace(`${pathname}#${hash}`);
71+
}
72+
console.count("hash handler fired");
5673

5774
if (hash.includes("debugger-io?token=")) {
5875
const debugHash = window.location.hash
@@ -68,34 +85,32 @@ export const TokenDecoderComponent: React.FC<TokenDecoderComponentProps> = ({
6885
}
6986

7087
const newUrl = `${currentUrl}#${SupportedTokenHashParamValues.TOKEN}=${token}`;
71-
console.log("Logging hash length before replacing///", token.length)
88+
console.log("Logging hash length before replacing///", token.length);
7289
window.location.replace(newUrl);
7390

7491
return;
7592
}
7693

7794
if (!hash.includes("=")) {
78-
console.log("hash not includes =")
95+
console.log("hash not includes =");
7996
return;
8097
}
8198

82-
83-
8499
const hashParams = new URLSearchParams(hash);
85-
console.log("Hash param keys!!!", hashParams)
100+
console.log("Hash param keys!!!", hashParams);
86101
Object.values(SupportedTokenHashParamValues).forEach((hashParamKey) => {
87102
const token = hashParams.get(hashParamKey);
88103

89104
if (token) {
90-
console.count("handling jwt change")
91-
console.log("The token is:///", token)
105+
console.count("handling jwt change");
106+
console.log("The token is:///", token);
92107
handleJwtChange$(token);
93108
}
94109
});
95110
};
96111

97112
window.addEventListener("hashchange", handleHashChange);
98-
113+
99114
handleHashChange();
100115

101116
return () => {

src/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function middleware(request: NextRequest) {
3535
return;
3636
}
3737

38-
const tokenParam =
38+
/* const tokenParam =
3939
request.nextUrl.searchParams.get("token") ||
4040
request.nextUrl.searchParams.get("id_token") ||
4141
request.nextUrl.searchParams.get("access_token") ||
@@ -51,7 +51,7 @@ export function middleware(request: NextRequest) {
5151
request.nextUrl.hash = `token=${tokenParam}`;
5252
5353
return NextResponse.redirect(request.nextUrl);
54-
}
54+
} */
5555

5656
const libraryFilter = request.nextUrl.searchParams.get("language");
5757
const isLibrariesPage = pathname === "/libraries";

0 commit comments

Comments
 (0)