Adam O'Connor
Adam O'Connor

Reputation: 2682

Modify confirmation with Devise

I would like to set a boolean flag upon user confirmation via Devise. Essentially I want to set the user as 'active'. However, Devise simply confirms the account and logs them in.

How can I create a callback of some sorts to update my user record to set that 'active' column to true upon confirmation?

Any help very appreciated!

Upvotes: 0

Views: 1605

Answers (3)

shakerlxxv
shakerlxxv

Reputation: 437

This is basically a comment on Turadg's Answer below. If you follow that suggestion (which I did) you will have a small problem when users attempt to use an invalid confirmation_token. You will get a "Missing template users/confirmations/new". What the Devise::ConfirmationsController is doing here is sending you to devise/confirmations/new to inform you the token is invalid and allow you to send another one.

Since I had already customized the Devise views, what I ended up doing to get around this minor issue is moving the devise/confirmations/new.html.haml file into the now expected location under user/confirmations/new.html.haml.

Upvotes: 0

Turadg
Turadg

Reputation: 7691

For your particular question, you're better off implementing your active? attribute as confirmed_at being nil, as suggested by Zabba.

But here is how to do what you're asking, since it may be helpful to people trying to set other values on the user after confirmation.

class Users::ConfirmationsController < Devise::ConfirmationsController

  def show
     # let Devise actually confirm the user
    super
    # if they're confirmed, it will also log them in
    if current_user then
      # and you can do whatever you want with their record
      current_user.active = true
    end
  end

end

Upvotes: 0

Zabba
Zabba

Reputation: 65517

Presuming that your authentication model is called User, you can do this:

class User < ActiveRecord::Base
  def active?
    super and (not self.confirmed_at.nil?)
  end
end

With this, Devise will not login the user but will wait until the user confirms (the confirmed_at field will be non-NULL if a user has confirmed)

Upvotes: 1

Related Questions