Clearance Tutorial

Are there any good tutorials / screencasts out there for thoughtbot’s clearance gem? I’m working on integrating it with my app but I’m having trouble wrapping my head around it’s defaults.

+1 for @edwardloveall’s request. I’m trying to integrate Clearance into an existing app with a custom password strategy and haven’t figured out all the details yet.

@edwardloveall - I got mine to work this afternoon, using a CustomPasswordStrategy. I had to integrate it with an existing app that I’m upgrading so the CustomPS was to make sure that existing users could still login with their old passwords. I also had to rename a few columns as I used ‘password’ and ‘emailaddr’ whereas Clearance uses ‘encrypted_password’ and ‘email’. Finally, I had to also refactor my ‘User’ information as it was setup as ‘Account’… fun!

I don’t know what your particular use case is: I think that if you are building a greenfield app, then it would be pretty straight-forward: they’ve got all the sign-up/sign_in/sign_out dialed in. In my case, I had to do the following:

  1. Define my CustomPasswordStrategy in config/initializers/clearance.rb, plus set config.password_strategy correspondingly.
  2. Change my existing UserController to inherit from Clearance::UsersController (I have other columns that I needed to manage, and this wound up beining simpler for me.
  3. Incorporate the code/approach from Clearance::UsersController into my #create action, including: using their user_from_params() method and (finally) recognizing that this method deletes password & email from the params hash.
  4. Changing my paths/references to use the Clearance-supplied names (I had been using login, logout, signup). Once I did that, my existing sign_up and sign_in forms magically worked in place of the Clearance forms (still not exactly sure how I managed to do that :=)

Took me quite a while, but I finally worked my way through it by methodically adding logger statements so that I could follow what was happening. Probably a quicker/easier way to make this all happen, but it worked for me. Don’t know if this helps, but hopefully to some extent…

Thanks @JESii! I might try some of this in a test app. Glad you got yours to work!

Hey @edwardloveall!

Nice to see you on here.

What are you trying to do exactly? Or which parts are you trying to understand? Are you trying to config clearance a la and getting errors? I’ll ping someone to help you out.

Hey @edwin. I think I’m looking for some guidance on two things.

First is some ruby/rails patterns. For example, it wasn’t initially clear to me how to override the login views with clearance. I know now that I can just put files in the same place that the gem does. I think it’s the same problem that people have with rails when they start using it. Too much magic; it’s somewhat unclear what’s going on under the hood.

Second, with clearance in particular, I’m having some trouble configuring it. Specifically I don’t receive emails when I make a new account or use forget password. I’m pretty sure it’s just a config file with SMTP credentials that I’m missing but I don’t know. I can’t find any documentation on it in the repo.

I dunno, maybe I’m missing the boat but the repo seems to assume a lot.

rails generate clearance:views will copy the clearance views into your /app/views directory. You can modify to your heart’s content from there.

Specifically I don’t receive emails when I make a new account or use forget password. I’m pretty sure it’s just a config file with SMTP credentials that I’m missing but I don’t know

You can configure your SMTP server settings using Rails’ ActionMailer settings in your environment configuration files.

To elaborate on the views part…

This is leveraging view lookup hierarchy. When Rails is looking for a view, it’s going to look in app/views first before going down through what your various gems might have to offer. By copying clearance’s views to app/views (or writing your own from scratch in the right folders with the right names) you’re putting those copies first in the search.

Thanks @derekprior. I’m starting to get it a little bit. There seems to be a lot of default overriding needed, but maybe I’m just new to the convention.

One thing I can’t get working is the mailing still. In a previous app, I ran through the Authentication From Scratch screencast. I have my mail settings working in there. I went back and looked at that code and it seems as though clearance doesn’t send any mail message by default. I did try overriding the controller but it doesn’t seem to work. I’m testing it by just printing to the console.

class SessionsController < Clearance::SessionsController
  def new
    p "--------------"

The only email clearance sends is the password reset email. This will not actually be sent in development (via Rails settings), but you should see it output in your development log.

Clearance has strong opinions. If you agree with those opinions, there’s very little overriding that you need to do - you’ll likely just need to update the views as you see fit. If you disagree with those opinions, you have some overriding to do. We’re in the process of making some changes to make overriding things less painful, but the out of box experience will remain pretty opinionated.

Out of curiosity, what are you trying to do differently than what comes out of the Box?

Looking at the changes I was trying to make, there actually weren’t that many. The biggest one I was trying to add was sending an email message on user sign up. It’s so standard now that I expected clearance to do it. It also feels weird to sign up, and not have any confirmation. Could just be my preference though.

Currently, I’m finding Monban to be a bit more to my taste. Even though Monban seems to be a slightly lighter weight clearance with less functionality, my expectations matched up better with that monban offers.