LoginSignup
0
0

More than 5 years have passed since last update.

作成したメール認証機能を削除しようとして日曜日終わった

Last updated at Posted at 2018-03-18

クラウドなメール送信サービスのひとつである「SendGrid」を使用し作成した
ログイン後のメール認証機能を無くそうとして日曜日終わってしまった記録。

モデル

app\models\user.rb
class User < ActiveRecord::Base
  # モデルの設定、:confirmable,を削除する
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

  #carrierwave用の設定、deviseの設定配下に追記
  mount_uploader :avatar, AvatarUploader

  # has_many :hogesにより子モデル(hoge)が複数ひもづくアソシエーションを定義する
  has_many :hoges 

  # ツイッターでログインできるようになるためのメソッドの処理
  def self.find_for_twitter_oauth(auth, signed_in_resource = nil)
    user = User.find_by(provider: auth.provider, uid: auth.uid)
    unless user
      user = User.new(
        name:     auth.info.nickname,
        image_url: auth.info.image,
        provider: auth.provider,
        uid:      auth.uid,
        email:    auth.info.email ||= "#{auth.uid}-#{auth.provider}@example.com",
        password: Devise.friendly_token[0, 20]
      )
      user.skip_confirmation!
      user.save
    end
    user
  end
  #(アカウント新規登録)ランダムなuidを作成するcreate_unique_stringメソッド
  def self.create_unique_string
    SecureRandom.uuid
  end
  # omniauthでサインアップしたアカウントのユーザ情報の変更出来るようにする
  def update_with_password(params, *options) # pdate_with_passwordをオーバーライド(メソッドを定義しなおしている)
    if provider.blank?
      super
    else
      params.delete :current_password
      update_without_password(params, *options)
    end
  end
end

ひとまずrails s でサーバを起動してブラウザで動作を確認

rails s -b 0.0.0.0

エラー

  • NoMethodError in Users::OmniauthCallbacksController#twitter
  • Undefind method "user.skip_confirmation!"

ここで結構調べる、あきらめかけた

もう一度モデル内の記述"user.skip_confirmation!"確認

app\models\user.rb
  # ツイッターでログインできるようになるためのメソッドの処理
  def self.find_for_twitter_oauth(auth, signed_in_resource = nil)
    user = User.find_by(provider: auth.provider, uid: auth.uid)
    unless user
      user = User.new(
        name:     auth.info.nickname,
        image_url: auth.info.image,
        provider: auth.provider,
        uid:      auth.uid,
        email:    auth.info.email ||= "#{auth.uid}-#{auth.provider}@example.com",
        password: Devise.friendly_token[0, 20]
      )
      # ツイッターでログインするときはメール認証要らないですよの意味っぽいこと書いてたのを忘れてた、ここを削除してみた
      user.skip_confirmation!
      user.save
    end

再びrails s でサーバを起動してブラウザで動作を確認

rails s -b 0.0.0.0

解決

ちゃっちゃ系のラーメン屋でも探しに行こう

0
0
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
0
0