在用 devise+omniauth-weibo-oauth2 写一个三方帐号登录,但是新浪微博不提供 email 的,而 email 为登录必须字段,每次用微博登录后又回到登录界面,我的想法是将授权信息存入 session,然后跳转到让用户补充 email 的界面,必须让用户绑定一个 email 才能完成授权,但是不知道具体该怎么去写。
参照https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview写的方法如下: def self.find_for_weibo_oauth2(auth, signed_in_resource=nil) user = User.where(:provider => auth.provider, :uid => auth.uid).first unless user user = User.create(username:auth.extra.raw_info.name, provider:auth.provider, uid:auth.uid, email:auth.info.email, password:Devise.friendly_token[0,20] ) end user end
def weibo # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.find_for_weibo_oauth2(request.env["omniauth.auth"], current_user) # debugger if @user.persisted? flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Weibo" sign_in_and_redirect @user, :event => :authentication else session["devise.weibo_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end end