Route.ts Help getting hash from url
Answered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
So i sadly wanted to support telegram oauth. They use the [hash](https://developer.mozilla.org/en-US/docs/Web/API/URL/hash) standards to store data instead of searchParams.
Example oauth callback url
The hast does not show up in
Example oauth callback url
http://localhost:3000/api/auth/callback/telegram#tgAuthResult=eyJpZCI6NzI2NDk5MDg1NSwiZmlyc3RfbmFtZSI6IkNoYW5...The hast does not show up in
NextRequest.url or NextRequest.nextUrl.hash.Answered by Asiatic Lion
I have learned that hash's do not travel to the server from some browsers (if not all)
My fix is this
My fix is this
if (!request.nextUrl.searchParams.get('tgAuthResult')) return new Response(`<!DOCTYPE html><html lang="en">
<head><script>
window.onload = function() {
const hash = window.location.hash;
if (hash) {
const hashValue = hash.substring(1);
const newUrl = window.location.origin + window.location.pathname + '?tgAuthResult=' + encodeURIComponent(hashValue);
window.location.replace(newUrl);
}
};
</script>
</head><body>
<h1>Redirecting...</h1>
</body></html>
`, {
status: 200,
headers: {
'Content-Type': 'text/html',
}
})1 Reply
Asiatic LionOP
I have learned that hash's do not travel to the server from some browsers (if not all)
My fix is this
My fix is this
if (!request.nextUrl.searchParams.get('tgAuthResult')) return new Response(`<!DOCTYPE html><html lang="en">
<head><script>
window.onload = function() {
const hash = window.location.hash;
if (hash) {
const hashValue = hash.substring(1);
const newUrl = window.location.origin + window.location.pathname + '?tgAuthResult=' + encodeURIComponent(hashValue);
window.location.replace(newUrl);
}
};
</script>
</head><body>
<h1>Redirecting...</h1>
</body></html>
`, {
status: 200,
headers: {
'Content-Type': 'text/html',
}
})Answer