簡體   English   中英

Omniauth用於Rails API中的提供程序身份驗證

[英]Omniauth for provider authentication in Rails API

我已經讓omniauth在我的網絡應用程序上完美運行。 我還為我們的iPhone應用程序創建了一個API進行交互,我試圖讓omniauth工作。

有沒有辦法將訪問令牌(從與Facebook.app的集成iOS集成接收)傳遞給omniauth以在數據庫中創建提供者條目?

現在在我的網絡應用程序中,我有一個帶有以下代碼的身份驗證控制器

  def create
    omniauth = request.env["omniauth.auth"]
    user = User.where("authentications.provider" => omniauth['provider'], "authentications.uid" => omniauth['uid']).first

    if user
      session[:user_id] = user.id
      flash[:notice] = t(:signed_in)
      redirect_to root_path
    elsif current_user
      user = User.find(current_user.id)
      user.apply_omniauth(omniauth)
      user.save
      flash[:notice] = t(:success)
      redirect_to root_path
    else
      session[:omniauth] = omniauth.except('extra')
      flash[:notice] = "user not found, please signup, or login. Authorization will be applied to new account"
      redirect_to register_path
    end
  end

在我的API用戶控制器中,我創建了以下內容:

  def create
    @user = User.new(params[:user])
    @user.save

    # Generate data for omni auth if they're a facebook user
    if params[:fb_access_token]
      graph = Koala::Facebook::API.new(params[:fb_access_token])
      profile = graph.get_object('me')

      @user['fb_id'] = profile['id']
      @user['fb_token'] = params[:fb_access_token]
      @user['gender'] = profile['gender']

      # Generate omnihash
      omnihash = Hash.new
      omnihash['provider'] = 'facebook'
      omnihash['uid'] = profile['id']

      omnihash['info'] = Hash.new
      omnihash['info']['nickname'] = profile['username']
      omnihash['info']['name'] = profile['name']
      omnihash['info']['email'] = profile['email']
      omnihash['info']['first_name'] = profile['first_name']
      omnihash['info']['last_name'] = profile['last_name']
      omnihash['info']['verified'] = profile['verified']

      omnihash['info']['urls'] = Hash.new
      omnihash['info']['urls']['Facebook'] = profile['link']

      omnihash['credentials'] = Hash.new
      omnihash['credentials']['token'] = params[:fb_access_token]

      omnihash['extra'] = Hash.new
      omnihash['extra']['raw_info'] = Hash.new

      puts omnihash

      # Save the new data
      @user.apply_omniauth(omnihash)
      @user.save
    end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM