2015年2月25日水曜日

Rails: Twitter Auth by 0Auth (detail)

$ vim Gemfile
 10
 11 gem 'omniauth'
 12 gem 'omniauth-twitter'
 13 gem 'twitter'
 14

$ vim config/initializers/omniauth.rb
 17 
 18 Rails.application.config.middleware.use OmniAuth::Builder do
 19   provider :twitter, 'TWITTER_CONSUMER_KEY', 'TWITTER_CONSUMER_SECURET',
 20         :request_path => 'sign_in', :callback_path => 'sign_in/callback'
 21 end
 22 

$ vim config/routes.rb
 25 
 26 get '/auth/:provider/callback'=>'sessions#create'
 27 

$ rails g controller sessions

$ vim sessions_controller.rb 
 32 
 33 class SessionsController < ApplicationController                            
 34   def create
 35    raise request.env["omniauth.auth"].to_yaml
 36   end
 37 end
 38 

$ rails g model usertwitter provider:string uid:string name:string
  
$ vim controller/usertwitter.rb
 50   class SessionsController < ApplicationController                                
 51       def create
 52           auth = request.env["omniauth.auth"]
 53           user = Usertwitter.find_by_provider_and_uid(auth["provider"], au    th["uid"]) 
 54           || Usertwitter.create_with_omniauth(auth)
 55           session[:user_id] = user.id
 56   
 57          redirect_to '/' , :notice => "Signed in!"
 58       end
 59   end

$ vim model/usertwitter.rb
 63     def self.create_with_omniauth(auth)
 64      create!do |usertwitter|
 65        usertwitter.provider = auth["provider"]
 66        usertwitter.uid = auth["uid"]
 67        usertwitter.name = auth["name"]
 68      end
 69    end

$ vim controllers/twines.rb
 80     before_filter :authenticate
 81 
 82       def authenticate
 83          auth = request.env["omniauth.auth"]
 84           redirect_to '/' unless current_user
 85      end

 ***Twitter Service Setting
 42 https://dev.twitter.com/
 43 Product → Login with Twitter
 44 Manage Your Apps






0 件のコメント:

コメントを投稿

fd-find

 $ sudo apt install fd-find $ fdfind 石