Rails 4.1 Mailer Previews and Devise custom emails

For those looking to preview Devise emails without using custom mailers, (but still custom emails) this is what I did: Configure your app for email previewing. Set up the Devise Mailer Preview class a. Rails ~> 4.1 # mailer/previews/devise_mailer_preview.rb class Devise::MailerPreview < ActionMailer::Preview def confirmation_instructions Devise::Mailer.confirmation_instructions(User.first, “faketoken”) end def reset_password_instructions Devise::Mailer.reset_password_instructions(User.first, “faketoken”) end … end … Read more

Devise within namespace

Simply “moving” Devise to the admin namespace is wrong. Devise uses controllers like Devise::SessionsController and that cannot be “moved”. I usually create my own controllers and inherit them from Devise: class Admin::SessionsController < ::Devise::SessionsController layout “admin” # the rest is inherited, so it should work end And configure this in config/routes.rb: devise_for :admins, :controllers => … Read more

Using Devise tokens to log in, is this built in?

My understanding is that you can use the tokens to log in or to hit arbitrary pages that need authentication, even with cURL. If you look in config/initializers/devise.rb, there should be a line that says something like: config.token_authentication_key = :auth_token Whatever the name of the token_authentication_key is should match what you put as the query … Read more

Avoid sign-in after confirmation link click using devise gem?

The config.allow_insecure_sign_in_after_confirmation flag is no longer supported in Devise. While you should be aware of the possible security concerns of automatically logging users in when they confirm their account (http://blog.plataformatec.com.br/2013/08/devise-3-1-now-with-more-secure-defaults/), for some apps the benefit in terms of user experience may be worth the security tradeoff. After all, the security risk is that a) the … Read more

Devise: manually encrypt password and store directly

You should do it like this: password = ‘the secret password’ new_hashed_password = User.new(:password => password).encrypted_password This is much better than using BCrypt directly as it abstracts away how passwords are generated from your code, making it easier to understand, and also immune to changes in how devise constructs encrypted passwords. Your code should not, … Read more

Check if user is active before allowing user to sign in with devise (rails)

Add these two methods to your user model, devise should pick them up automatically – you should NOT need to extend Devise::SessionsController def active_for_authentication? super && self.your_method_for_checking_active # i.e. super && self.is_active end def inactive_message “Sorry, this account has been deactivated.” end