パルカワ2

最近はFlutterをやっています

deviseで登録時に本人確認メールを送信するが、本人確認してなくてもログイン出来るようにする。

使ってるバージョンは、3.4.1。

# Checks if the confirmation for the user is within the limit time.
# We do this by calculating if the difference between today and the
# confirmation sent date does not exceed the confirm in time configured.
# Confirm_within is a model configuration, must always be an integer value.
#
# Example:
#
#   # allow_unconfirmed_access_for = 1.day and confirmation_sent_at = today
#   confirmation_period_valid?   # returns true
#
#   # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 4.days.ago
#   confirmation_period_valid?   # returns true
#
#   # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 5.days.ago
#   confirmation_period_valid?   # returns false
#
#   # allow_unconfirmed_access_for = 0.days
#   confirmation_period_valid?   # will always return false
#
#   # allow_unconfirmed_access_for = nil
#   confirmation_period_valid?   # will always return true
#
def confirmation_period_valid?
  self.class.allow_unconfirmed_access_for.nil? || (confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago)
end

https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb#L166
このあたり見たら書いてた。

# config/initializers/devise.rb
Devise.setup do |config|
  config.allow_unconfirmed_access_for = nil
end

とすると出来るようになる。
allow_unconfirmed_access_forは、普通1.dayだとかが入るらしいけど、nilだと無条件にオッケーになる。

それ以外にもこう書けばよいみたいなのも見つけた。
https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users#allowing-unconfirmed-access

protected
def confirmation_required?
  false
end

ただ、コードを見ていると登録時にはメールが送られなさそうだな〜と思ったのでとりあえずnilにする方を採用した。(試していないのはなるべく早く実装したかった)