11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsでTwitterの認証をOmniAuthで行う。

Last updated at Posted at 2012-10-27

参考

Twiwt:Blog / jugyo : OmniAuth で簡単 Twitter 認証!

Omniauth NoMethodError in SessionsController - Stack Overflow

Gemfileに以下を追記後、bundle install

gem 'omniauth'
gem 'omniauth-twitter'

その後

bundle install

Twitterのconsumer key/sercret を記入

config/initializers/omniauth.rb に取得したKeyを入れる。

omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
end

SessionsControllerを作成

rails g controller sessions

app/controllers/sessions_controller.rbを以下に編集

sessions_controller.rb
class SessionsController < ApplicationController
  def callback
    auth = request.env["omniauth.auth"]
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
    session[:user_id] = user.id
    redirect_to root_url, :notice => "Signed in!"
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url, :notice => "Signed out!"
  end
end

config/routes.rb に認証のルーティングを記入

routes.rb
match '/auth/:provider/callback' => 'sessions#callback'
match "/signout" => "sessions#destroy", :as => :signout

User モデルの作成

rails g model user

db/migrate/XXXXXXXXXXXXXX_create_users.rb を編集。

XXXXXXXXXXXXXX_create_users.rb
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :provider, :null => false
      t.string :uid, :null => false
      t.string :screen_name, :null => false, :uniq => true
      t.string :name, :null => false

      t.timestamps
    end
    add_index :users, [:provider, :uid]
    add_index :users, [:screen_name]
  end

  def self.down
    drop_table :users
  end
end

app/models/user.rbを編集。

user.rb
class User < ActiveRecord::Base
  def self.create_with_omniauth(auth)
    create! do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.screen_name = auth["info"]["nickname"]
    end
  end
end

編集後、migrate

rake db:migrate

ApplicationControllerを編集

app/controllers/application_controller.rb を編集

application_controller.rb
class ApplicationController < ActionController::Base
  ...

  helper_method :current_user

  private

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

Viewの作成

rails g controller login index

app/views/login/index.html.erb に以下を追加

index.html.erb
<% if current_user %>
  Welcome <%= current_user.name %>!
  <%= link_to "Sign Out", signout_path %>
<% else %>
  <%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>

config/routes.rbを編集

routes.rb
root :to => "login#index"

public/index.htmlがあれば、削除。

rails s

でサーバー起動。
そののち、http://localhost:3000/ にアクセスでログインできるか確認できる。

11
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?