Monday, January 30, 2017

Authentication in Rails Using Clearance

Authentication in Rails Using Clearance

Clearance is a simple authentication system with email and password built by the team at Thoughtbot. It has opinionated defaults but is intended to be easy to override. The system is actively maintained, and you can follow up on GitHub.

In this tutorial, you will see how to integrate Clearance into a Rails application. We will make use of a miniature application. Let's begin!

Getting Started

You'll start by generating your Rails application. For the purpose of this tutorial, I'll name mine tutsplus-clearance.

That will do the magic.

You'll need bootstrap to make your application look good. Add the Bootstrap gem to your Gemfile.

Install the gem by running bundle install.

Now modify application.scss  to look like this:

Clearance Setup

Open your Gemfile to add the Clearance gem.

Now install the gem.

bundle install

At this point, run the generator command to install clearance.

rails generate clearance:install

This will generate some outputs on your terminal, which look like what I have below:

When you ran the command, a couple of files were generated in your application. One such file is clearance.rb, which you can find in the config/initializers directory. A User model was also generated, and along with that you also have a migration file that looks like this:

According to the output, the first thing you want to do is edit your config environment. To do that, navigate to config/environments/development.rb and add the line below, just above the end delimiter.

Next, navigate to config/initializers/clearance.rb to edit it, and when you're there, change the sender email address from the default to any of your choosing. This is what you will see when you open the file.

You can override the default configuration by pasting in the following code snippet and configuring it to your requirements.

Run the command to migrate your database.

rake db:migrate

Open your PagesController and add an index action.

Next, create a view for the index action you just created.

Add the code snippet below:

Edit your routes to:

Create a partial named _navigation.html.erb inside the layouts directory. This will be used to handle everything that has to do with navigation on your application.

Paste the following code and save.

Restricted Access 

With Clearance, you can be able to create restricted access to specific pages of your choice in your application. Let's see how it is done.

Create a view for a new action in app/views/pages, the name of the file should be new.html.erb. Paste in the code below.

Now you need to add the line below to config/routes.rb.

Finally, go to your PagesController make it like what I have below.

In the above code, we are making use of the Clearance helper, require_login, to restrict access to the new action. To see how it works, start up your rails server by running rails server from your terminal. Point your browser to http://locahost:3000/pages/new and it should redirect you to the sign in page.

Clearance also provides routing constraints that can be used to control access.

In the code above, a different route has been created for authenticated users.

Overriding Clearance Defaults

A lot of things happen behind the scenes when you start using Clearance, things you cannot see. There might come a time when you want to customize things differently, depending on the specification of your application. Clearance allows you to override the default configuration it comes with.

To override (or generate) Clearance routes, run this command from your terminal.

rails generate clearance:routes

Your routes file should now look like this:

The command will also set the config.routes setting to false in your config/initializers/clearance.rb file. This means that the custom file which has just been generated will be used.

To generate views for modification, run:

rails generate clearance:views

Some of the files that will be generated include:

You will see a prompt in your terminal asking to overwrite your app/views/layouts/application.html.erb file. Choose the option you want.

Layouts

By default, Clearance uses your application's default layout. If you would like to change the layout that Clearance uses when rendering its views, simply specify the layout in an initializer.

Helper Methods

Clearance provides you with helper methods that can be used in your controllers, views, and helpers. These methods include signed_in?, signed_out?, and current_user. For example:

Conclusion

Clearance has a lot to offer you when it comes to authentication, so be sure to try it out in your next project. You can learn more by checking out the GitHub page.


No comments:

Post a Comment