Subscribed unsubscribe Subscribe Subscribe

Rails4にdeviseをインストール

Railsでソーシャルログイン機能を実装します。

Deviseのインストール

github.com

  • Deviseとは
Devise is a flexible authentication solution for Rails based on Warden. It:

Is Rack based;
Is a complete MVC solution based on Rails engines;
Allows you to have multiple models signed in at the same time;
Is based on a modularity concept: use only what you really need.

It's composed of 10 modules:

Database Authenticatable
Omniauthable
Confirmable
Recoverable
Registerable
Rememberable
Trackable
Timeoutable
Validatable
Lockable

ごりごり実装しなくても、ログイン関連のよくある機能はやってくれるようです。

  • Gemfile
gem 'devise'
  • gemのインストール
$ bundle install --path=vendor/bundle
  • deviseのイニシャライザーのインストール
$ rails g devise:install

以下のファイルが作成されます。

config/initializers/devise.rb
config/locales/devise.en.yml
  • 設定1(config/environments/development.rb)
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  • 設定2(config/routes.rb)
root 'index#index'

when using a :user resource, the user_root_path will be used if it exists; otherwise,
the default root_path will be used.
This means that you need to set the root inside your routes:

  • 設定3(app/views/layouts/application.html.erb)
<% # settings for devise(gem) %>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Remember that Devise uses flash messages to let users know if sign in was successful or unsuccessful. Devise expects your application to call flash[:notice] and flash[:alert] as appropriate. Do not print the entire flash hash, print only specific keys. In some circumstances, Devise adds a :timedout key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.