Rails4:deviseとomniauthでTwitterログイン機能を実装する(その2)
4. 認証用の画面を作成する
$ rails g devise:views Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring. invoke Devise::Generators::SharedViewsGenerator create app/views/devise/shared create app/views/devise/shared/_links.erb invoke form_for create app/views/devise/confirmations create app/views/devise/confirmations/new.html.erb create app/views/devise/passwords create app/views/devise/passwords/edit.html.erb create app/views/devise/passwords/new.html.erb create app/views/devise/registrations create app/views/devise/registrations/edit.html.erb create app/views/devise/registrations/new.html.erb create app/views/devise/sessions create app/views/devise/sessions/new.html.erb create app/views/devise/unlocks create app/views/devise/unlocks/new.html.erb invoke erb create app/views/devise/mailer create app/views/devise/mailer/confirmation_instructions.html.erb create app/views/devise/mailer/reset_password_instructions.html.erb create app/views/devise/mailer/unlock_instructions.html.erb
5. 認証用のモデルを作成する
$ rails g devise user Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring. invoke active_record create db/migrate/20140804112353_devise_create_users.rb create app/models/user.rb invoke test_unit create test/models/user_test.rb create test/fixtures/users.yml insert app/models/user.rb route devise_for :users
auth認証に必要なカラムを追加する
$ rails g migration AddTwitterFieldsToUsers name:string provider:string uid:string access_token:string Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring. invoke active_record create db/migrate/20140804112607_add_twitter_fields_to_users.rb
access_tokenとかいるのかな...
$ rake db:migrate == 20140804112353 DeviseCreateUsers: migrating ================================ -- create_table(:users) -> 0.0026s -- add_index(:users, :email, {:unique=>true}) -> 0.0006s -- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0005s == 20140804112353 DeviseCreateUsers: migrated (0.0039s) ======================= == 20140804112607 AddTwitterFieldsToUsers: migrating ========================== -- add_column(:users, :name, :string) -> 0.0005s -- add_column(:users, :provider, :string) -> 0.0002s -- add_column(:users, :uid, :string) -> 0.0002s -- add_column(:users, :access_token, :string) -> 0.0002s == 20140804112607 AddTwitterFieldsToUsers: migrated (0.0013s) =================
6. User モデルを編集する
omniauthable
を追加する。
trackable
とomniauthable
以外は使わないので、他はコメントアウトしておいたほうが良いかも。
# app/models/user.rb devise :database_authenticatable, :omniauthable, :recoverable, :registerable, :rememberable, :trackable, :validatable
続きは次回〜( ´∀`)