Next.js Discord

Discord Forum
\n\n

Redirecting...

\n\n`, {\n status: 200,\n headers: {\n 'Content-Type': 'text/html',\n }\n})","url":"https://nextjs-forum.com/post/1253556593602330655#message-1253569944248123454","dateCreated":"2024-06-21T04:39:24.912Z","author":{"@type":"Person","name":"Asiatic Lion"},"upvoteCount":1}}}

Route.ts Help getting hash from url

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
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 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
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',
  }
})
View full answer

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
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