OmniAuth dynamic callback url to authenticate particular objects instead of current_user

心不动则不痛 提交于 2019-12-10 10:03:29

问题


Say I have the models User and Project. Users and projects are HABTM-associated. My setup is actually a bit more complicated than this, but I think for the purposes of my question this will do.

Now, I want to use omniauth to authenticate a particular project with Twitter, Facebook, what have you. I've figured out how to define my omniauth path_prefix, but I don't know how I could pass in a variable like so: config.path_prefix = 'projects/:project_id/auth', much less make a custom callback url like project/:project_id/auth/twitter/callback.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5533064/omniauth-dynamic-callback-url-to-authenticate-particular-objects-instead-of-curr

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