Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

node.js - How to prevent 302 redirect from appending # to end of URL?

Not sure if anyone is going to be able to answer this, but I'm gonna try anyway.

I've just set up Google OAuth on my site using google-auth-library@^6.1.3 and fastify@^3.9.2. I've created a a login link that directs you to https://accounts.google.com/o/oauth2/v2/auth?.... When the user successfully logs in, Google redirects the user back to http://localhost:3000/google-oauth-redirect-uri?code=xxx&scope=profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile.

I use that code to create a session for the user, and then at the end of my fastify handler, I do:

reply
    .setCookie(SESSION_COOKIE_NAME, sessionKeyStr, {...SESSION_COOKIE_OPTIONS, expires: new Date(sessionExpiresAt)})
    .redirect('/')

Which creates the session cookie and redirects the user back to the homepage.

You can see the request here:

enter image description here

What I can't figure out is where that # in the URL is coming from. AFAIK, 302 header redirects can't even add URL-fragments. I thought maybe the hash was carried over from the previous page, but as you can see in the screenie, the previous page didn't have a hash either. So maybe the homepage is adding it with JS? No, it doesn't even contain a <script> tag.

What else could be adding that # to the URL?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...