OmniAuth dynamic callback url to authenticate particular objects instead of current_user

…衆ロ難τιáo~ 提交于 2019-12-05 19:02:04

This will break in production. In development you can get away with a session variable. But in production you need to have the callback url contain your project_id as it could be 2 or more register with different auth_project_id's and then you have no way of knowing which one is called afterwards (the callback is asynchronous).

https://github.com/mkdynamic/omniauth-facebook#custom-callback-urlpath

something like config.path_prefix = "projects/#{@project.id}/auth" might work. I'm testing a similar situation right now.

For posterity's sake, I solved it this way:

I added an auth method to my projects controller, which set a session variable session[:auth_project_id] and then redirectes to auth/ + params[:provider].

In my callback controller authentications, I got my project with @project = Project.find(session[:auth_project_id]), created the authentication, and then session[:auth_project_id] = nil to unset the session variable.

I have done similar thing with devise omniauthable, You can pas any parameter with link. like

<%= link_to "Add twitter Account",  user_omniauth_authorize_path(:twitter, params:  { project_id: @project.id     }) %>

Then in your callback controller

before_action :set_project, only: [:twitter]

def set_project
  @project = Project.find(request.env['omniauth.params']['project_id'])
end

Note: Do NOT use request.env['omniauth.auth']

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!