A Simple Twitter App with Ruby on Rails – User Authentication
This article is the second part in a three part series. In the first part we created some basic functionality to allow the user to post messages in a similar way to Twitter. If you have not completed the first part of this tutorial, please do so now.
In this instalment, we will learn how to add some basic user authentication using a useful plugin called nifty_generators. So lets get started!
- The first part of this tutorial: A Simple Twitter App with Ruby on Rails - Messages With Ajax
- The third part of this tutorial: A Simple Twitter App with Ruby on Rails – Building Friendships
Get the Gem!
It's worth noting that there are other tools for integrating authentication, for instance technoweenie's Restful Authentication is very good. However, to keep things simple, we will stick with nifty_generators.
At a command prompt key the following command:-
> gem install nifty-generators
Generating the Authentication
Now, make sure your current directory is set to the project folder of your Rails project and run the following commands:-
> ruby script/generate nifty_authentication > ruby script/generate nifty_layout
The first command creates the relevant files for achieving authentication. The second command creates, among other things, a helper file which is required for some of the authentication view files to function correctly.
Don't forget to migrate your database!
> rake db:migrate
Making use of the Authentication Module
Ok, so we now have the functionality, which is required for authenticating users, it's just a matter of using it. First, we will create a partial allowing the user to sign up or login.
Create a file called "_login.html.erb" in the views/users folder and add the following code:
< % if logged_in? %> Welcome < %= current_user.username %>! Not you? < %= link_to "Log out", logout_path %> < % else %> < %= link_to "Sign up", signup_path %> or < %= link_to "log in", login_path %>. < % end %>
We will simply render the login partial in the "posts.html.erb" layout file (to keep things simple). This file is located in the views/layouts folder. Edit the file to include the "render" method call, as shown below.
. . <body> <div id="content"> <p><%= render :partial => "users/login" %></p> <%= yield %> </div> </body> . .
Further to this, we only want users to be able to post messages if they are logged in so change the "_message_form.html.erb" file, in the views/posts folder, to have the following condition:
<% if logged_in? %> <% form_remote_tag(:controller => "posts", :action => "create") do %> <%= label_tag(:message, "What are you doing?") %><br /> <%= text_area_tag(:message, nil, :size => "60x2") %><br /> <%= submit_tag("Update") %> <% end %> <% end %>
Let's Give it a Go!
Fire up the server.
> ruby script/server
On the home page we now have "Sign up" and "Login" links.
You'll also notice that you cannot post a message until you have a user account. So you will need to sign up first
Then you can login...
Once you have logged in, you will be able to post messages again.
In this tutorial, you have leanrt how to integrate authentication into your applications and how to make use of it. In the third and final part of this series you will learn how to add functionality, which allows users to follow other users.