简体   繁体   中英

Devise user doesn't sign in even I set up confirm_within parameter

I have an rails app, using devise gem for user authentication.

I used confirmable to send confirmation email to user. It's running well. I got an confirmation email. And I need to confirm my account before successfully signed in.

I would like to let them try our app for a while, before confirm their account. So I setup confirm_within in the devise initializer. It is failed. After user filled in the register form, they will be redirected back to login page after sign up. I checked the log, it seems that the user isn't signed in. So it failed in authentication! before filter.

Here are my configuration.

Model: user.rb

  devise :database_authenticatable, :registerable, :encryptable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

route.rb

  devise_for :users, :controllers => {:password => "passwords"}

Initializer: devise.rb

  config.confirm_within = 2.days

application_controller.rb

  def after_sign_in_path_for(resource)
      stored_location_for(resource) || jobs_path
  end

jobs_controller.rb

  class JobsController < ApplicationController
    before_filter :authenticate_user!

    ***some functions
  end

I am not sure did I missed anything. Would someone kindly tell me how to fix this problem? Thanks.

I think you are reading the wrong documentation: "* +allow_unconfirmed_access_for+: the time you want to allow the user to access his account # before confirming it. After this period, the user access is denied. You can # use this to let your user access some features of your application without # confirming the account, but blocking it after a certain period (ie 7 days). # By default allow_unconfirmed_access_for is zero, it means users always have to confirm to sign in. # * +reconfirmable+: requires any email changes to be confirmed (exactly the same way as # initial account confirmation) to be applied. Requires additional unconfirmed_email # db field to be setup (t.reconfirmable in migrations). Until confirmed new email is # stored in unconfirmed email column, and copied to email column on successful # confirmation. # * +confirm_within+: the time before a sent confirmation token becomes invalid. # You can use this to force the user to confirm within a set period of time."

in here https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb

so for allow the user to login without confirmation for a certain time you have to define:

config.allow_unconfirmed_access_for

and to allow the user to confirm, which after he cant confirm anymore which means he needs a new confirmation to be sent for that email because the token becomes invalid is:

config.confirm_within

hope it was explicit.

Sorry that I can't be of more help, I don't have any experience working with Devise's confirm_within feature. However, according to this question , it appears that the user is supposed to be signed out after registration because they aren't supposed to be logged in until they confirm their email address by responding to an email, if I'm not mistaken. If you want them to be logged in automatically after registration then I believe you need to remove the confirm_within option. It sounds to me that the confirm_within option is the amount of time the user has in order to confirm that the email address they signed up with is actually theirs (by clicking the link that gets emailed to them).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM