LoginSignup
79
64

More than 5 years have passed since last update.

Ruby on Rails ~ deviseでアプリにユーザー認証、サインアップ、ログイン機能をつける(開発環境&本番環境)(コードメモ)

Last updated at Posted at 2017-01-16

deviseを導入して、簡単にユーザー周りの機能を実装していきます。

1.deviseのインストール

gem 'devise'

Gemfile.lockを削除後、

$ bundle install
$ rails g devise:install

2.deviseの環境設定。

・開発環境

config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost:3000', port: 3000 }

・本番環境

config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'http://アプリ名.herokuapp.com' }

3.ルートの設定

・トップページのルートパスを指定

config/routes.rb
root :to => "top#index"

4.Userモデルの作成

$ rails g devise User

※ここではrake db:migrateはしません。

5.Userモデルとマイグレーションファイルを編集

app/models/user.rb
devise :database_authenticate, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :confirmable
db/migrate/...
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email
$ bundle exec rake db:migrate

6.ビューファイルの作成

$ rails g devise:views
app/views/layouts/application.html.erb
<% if user_signed_in? %>
  <li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% else %>
  <li><%= link_to "Login", new_user_session_path %></li>
<% end %>
app/controllers/application_controller.rb
before_action :authenticate_user!

7.デバイスの設定を追記。

メール認証の方法は幾つかあります。
Gmailを設定する方法とSendGridを使う方法を実装しました。
ここでは、開発環境ではGmailを、本番環境ではSendGridを設定していきます。

・開発環境(Gmail)

config/environments/development.rb

Rails.application.configure do

   ...

  config.action_mailer.default_url_options = { host: 'localhost:3000', port: 3000 }
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :enable_starttls_auto => true,
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => 'smtp.gmail.com',
    :user_name => "ご自身のgmailアドレス", 
    :password => "ご自身のgmailアドレスのパスワード", 
    :authentication => 'login'
}
end

・本番環境(SendGrid)
herokuのアカウントに登録後、SendGridのアドオンの利用登録(クレジットカードの登録)を済ませます。無料で使う場合も必要です。
アカウント登録 https://signup.heroku.com/identity

クレジットカードの登録 https://dashboard.heroku.com/account/billing

また、herokuのToolbeltをインストールして、herokuコマンドをターミナル上で入力できるようにしておきます。
https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up

その後、ターミナル上でSendGridのアカウントを取得します。

$ heroku login
#ユーザー名とパスワードを入力
$ heroku addons:add sendgrid:starter

SendGridのアカウントが作成されるのでアカウント名とパスワードを表示させます。

$ heroku config:get SENDGRID_USERNAME #アカウント名
$ heroku config:get SENDGRID_PASSWORD #パスワード

上記をもとにdeviseの環境設定を編集します。

config/environments/production.rb

Rails.application.configure do

   ...
config.action_mailer.default_url_options = {host: 'http://アプリ名.herokuapp.com' }
  config.action_mailer.raise_delivery_errors = false #この一文も追記!!
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :user_name => "SendGridのアカウント名",
          :password => "SendGridのパスワード",
      :domain => "heroku.com",
      :address => "smtp.sendgrid.net",
      :port => 587,
      :authentication => :plain,
      :enable_starttls_auto => true
  }
end

config/application.rb
config.assets.initialize_on_precompile = false

必要であれば、送信元の表示設定を行います。

config/initializers/devise.rb
config.mailer_sender = '"アプリ名" <service@example.com>'

8.動作を確認する。

・開発環境の場合(Gmail)

$ rails s

ローカルサーバーを立ち上げ、ビュー画面からサインアップしてみましょう。
入力したメールアドレスに、Gmailから認証メールが届きます。
※届かない場合は、https://www.google.com/settings/security/lesssecureapps
からGmailの設定を「安全性の低いアプリのアクセス」をオンにしてみてください。(config/environments/development.rbに記述したGmailアカウント)

・本番環境の場合(SendGrid)
herokuにデプロイ後、同じようにサインアップしてください。

$ git push heroku master
$ heroku run rake db:create
$ heroku run rake db:migrate

以上で実装が完了しました。

79
64
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
79
64