Rails omniauth更多功能

时间:2015-12-23 13:19:28

标签: ruby-on-rails omniauth

我已遵循此tutorial

并且能够完成facebook身份验证。现在我需要一种方法从电子邮件,生日,用户名和所有内容中获取用户的一些信息。任何人都可以给我一些例子。
[omniauth.rb]

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'my-app-id', 'my-app-key', {:client_options => {:ssl => {:ca_file => Rails.root.join("cacert.pem").to_s}}}

end

[user.rb]

class User < ActiveRecord::Base
    def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.provider = auth.provider 
    user.uid      = auth.uid
    user.name     = auth.info.name
    user.save
  end
end

    def self.facebook
        @facebook ||=Koala::facebook::API.new(oauth_token)
    end
end


[application_controller]

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception


  helper_method :current_user

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

end


[session_controller]

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_path
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_path
  end
end


0 个答案:

没有答案