简体   繁体   English

重定向后会话为空

[英]Session empty after redirect

I've a React JS app, which makes this request to my back-end API.我有一个 React JS 应用程序,它向我的后端 API 发出这个请求。 ie IE

window.location = "https://my-server.com" + "/gmail/add_account";

cannot set HTTP headers for window.location see this this server endpoint redirects to Google OAuth page, which returns a response to my redirect_uri .无法为window.location设置 HTTP 标头, 请参阅此服务器端点重定向到 Google OAuth 页面,该页面返回对我的redirect_uri的响应。

def add_account
  # no auth headers sent here, because front-end has used window.location
  gmail_service = GmailService.new
  session[:uid] = params["uid"]
  redirect_to gmail_service.generate_authorization_url()
end

def oauth_postback
  # session object is {} here
  # Since there are no authorization headers, I cannot identify my app's user
  # How can I identify my app's user here?
end

The problem I'm facing is that when the OAuth flow sends the response to my redirect_uri it does not return include any authorization header , due to which I'm unable to identify which user of my app has launched this OAuth flow.我面临的问题是,当 OAuth 流向我的redirect_uri发送响应时,它不返回包含任何authorization header ,因此我无法确定我的应用程序的哪个用户启动了此 OAuth 流。
I've tried setting up a session variable in the /gmail/add_account endpoint, which works fine.我尝试在/gmail/add_account端点中设置会话变量,效果很好。 After this endpoint redirects to the OAuth screen, and the Oauth flow sends a response to my Oauth redirect_uri , there my session object is {} .在此端点重定向到 OAuth 屏幕并且 Oauth 流向我的 Oauth redirect_uri发送响应后,我的session对象是{}
How can I implement this flow such that I know which user has launched this OAuth flow?如何实施此流程,以便我知道哪个用户启动了此 OAuth 流程?

You have basically two options:您基本上有两种选择:

  • the state parameter state参数

    The state parameter is part of the OAuth2 spec (and is supported by Google). state参数是 OAuth2 规范的一部分(并且受 Google 支持)。 It's a random string of characters that you add to the authorization URL (as a query parameter), and will be included when the user is redirected back to your site (as a query parameter).它是您添加到授权 URL 的随机字符串(作为查询参数),并将在用户重定向回您的站点时包含(作为查询参数)。 It's used for CSRF protection, and can also be used to identify a user.它用于 CSRF 保护,也可用于识别用户。 Be sure that if you use it, it's a one-time value (eg a random value that you store in your db, not the user's ID).请确保如果您使用它,它是一次性值(例如,您存储在数据库中的随机值,而不是用户的 ID)。

  • sessions with cookies使用 cookie 的会话

    If the user has previously logged in, you should be able to identify them by their session cookie.如果用户以前登录过,您应该能够通过他们的会话 cookie 来识别他们。 It sounds like this is the approach you're currently taking, but the session is getting reset.这听起来像是您目前正在采用的方法,但会话正在重置。

    It's difficult to debug this without knowing more about your stack/code, but a good first step would be just trying to load your callback URL without the redirection to Google to see the session object is still empty.在不了解您的堆栈/代码的情况下很难调试它,但是一个好的第一步是尝试加载您的回调 URL,而无需重定向到 Google 以查看会话对象仍然为空。 If so, that would indicate an issue with how you've implemented sessions generally and not something specific to this flow.如果是这样,则表明您通常如何实施会话存在问题,而不是特定于此流程的问题。

    As a note, based on the code you've shared, I'm not sure how params["uid"] is getting set if you're doing a redirect without any query parameters or path parameters.请注意,根据您共享的代码,如果您在没有任何查询参数或路径参数的情况下进行重定向,我不确定params["uid"]是如何设置的。

Finally, you may consider using a managed OAuth service for something like this, like Xkit , where I work.最后,您可以考虑将托管 OAuth 服务用于类似的事情,例如我工作的Xkit If you have a logged in user, you can use Xkit to connect to the user's Gmail account with one line of code, and retrieve their (always refreshed) access tokens anywhere else in your stack (backend, frontend, cloud functions) with one API call.如果您有登录用户,您可以使用 Xkit 以一行代码连接到用户的 Gmail 帐户,并使用一个 API 检索他们(始终刷新)堆栈中其他任何位置(后端、前端、云函数)的访问令牌称呼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM