Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added a guide for adding authentication with Devise #36

Merged
merged 1 commit into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 157 additions and 1 deletion.
  1. +149 −0 _posts/2012-11-01-devise.markdown
  2. +8 −1 index.html
View
149 _posts/2012-11-01-devise.markdown
@@ -0,0 +1,149 @@
+---
+layout: default
+title: Adding Authentication with Devise
+permalink: devise
+---
+
+# Adding Authentication with Devise
+
+*Created by Piotr Steininger, [@polishprince](https://twitter.com/polishprince)*
+
+**This guide assumes that you have already built a RailsGirls app by ** [**following the app development guide**](/app).
+
+
+## Step 0: Add devise gem
+
+Open up your `Gemfile` and add this line
+
+{% highlight ruby %}
+gem 'devise'
+{% endhighlight %}
+and run
+{% highlight sh %}
+bundle install
+{% endhighlight %}
+to install the gem
+
+## Step 1: Set up devise in your app
+
+Open a terminal or command prompt (on Windows), go to your app's directory and run. Next, type these commands in the terminal:
+
+{% highlight sh %}
+rails g devise:install
+{% endhighlight %}
+
+
+
+## Step 2: Configure Devise
+
+Ensure you have defined default url options in your environments files. Open up `config/environments/development.rb` and add this line:
+{% highlight ruby %}
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
+{% endhighlight %}
+
+before the `end` keyword.
+
+Open up `config/routes.rb` and add this line:
+{% highlight ruby %}
+ root :to => "home#index"
+{% endhighlight %}
+
+before the `end` keyword.
+
+Open up `app/views/layouts/application.html.erb` and add:
+
+{% highlight erb %}
+ <p class="notice"><%= notice %></p>
+ <p class="alert"><%= alert %></p>
+{% endhighlight %}
+right above
+{% highlight ruby %}
+ <%= yield %>
+{% endhighlight %}
+
+Finally, if you plan to [deploy to heroku](/heroku) you should open up `config/application.rb` and add this line:
+{% highlight ruby %}
+ config.assets.initialize_on_precompile = false
+{% endhighlight %}
+as third line from the bottom (before the first of two `end` keywords)
+
+## Step 3: Setup the User model
+
+We'll use a bundled generator script to create a User model that is set up to user Devise.
+{% highlight sh %}
+ rails g devise user
+{% endhighlight %}
+
+Make any changes you need to the newly created `app/models/user.rb`, as well as the accompanying migration in db/migrate directory.
+Once configured to your needs run:
+{% highlight sh %}
+ rake db:migrate
+{% endhighlight %}
+to set up the database.
+
+## Step 4: Create Some Users
+
+Depending on how you configured the User model, you may be able to simply register for the account through your web browser.
+In that case, make sure your server is running, and go to [http://localhost:3000/users/sign_up](http://localhost:3000/users/sign_up).
+
+Otherwise you can create a user via rails console:
+{% highlight sh %}
+ rails c
+{% endhighlight %}
+and create a user as follows (replace the values with your own)
+{% highlight ruby %}
+ User.create(:email=>"me@example.com",:password => "test123", :password_confirmation => "test123")
+{% endhighlight %}
+
+You can now go to [http://localhost:3000/users/sign_in](http://localhost:3000/users/sign_in) to log in.
+
+## Step 5: Add sign-up and login links
+
+All we need to do now is to add appropriate links or notice about the user being logged in in the top right corner of the navigation bar.
+In order to do that, change the following lines in `app/views/layouts/application.html.erb`:
+{% highlight ruby %}
+<div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <a class="brand" href="/">The Idea app</a>
+ <ul class="nav">
+ <li class="active"><a href="/ideas">Ideas</a></li>
+ </ul>
+ </div>
+ </div>
+</div>
+{% endhighlight %}
+to
+{% highlight ruby %}
+<div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <a class="brand" href="/">The Idea app</a>
+ <ul class="nav">
+ <li class="active"><a href="/ideas">Ideas</a></li>
+ </ul>
+ <p class="navbar-text pull-right">
+ <% if user_signed_in? %>
+ Logged in as <strong><%= current_user.email %></strong>.
+ <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
+ <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
+ <% else %>
+ <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link' %> |
+ <%= link_to "Login", new_user_session_path, :class => 'navbar-link' %>
+ <% end %>
+ </p>
+ </div>
+ </div>
+</div>
+{% endhighlight %}
+
+Test it out by logging in and out
+
+## What next?
+
+* Add extra fields to the User model
+* Add relationships between users and ideas
+* Restrict users to only be able to edit their own ideas
+* Expand to use roles or permissions (use one of the popular authorization gem like CanCan)
+
+
View
9 index.html
@@ -10,7 +10,7 @@
<a href="http://railsgirls-jp.github.com/">Guides in Japanese</a><br/>
<a href="http://humancoders.github.com/railspremierspas/">Guides in French</a><br/>
<a href="http://github.com/rgcn/rgcn.github.com">Guides in Chinese</a><br/>
- <a href="http://rgua.github.com">Guides in Russian</a><br/>
+ <a href="http://rgua.github.com">Guides in Russian</a><br/>
</p>
</div>
@@ -51,6 +51,13 @@
</a>
</li>
<li>
+ <a href="devise">
+ <i>4</i>
+ <h3>Add authentication with Devise</h3>
+ <p>How to quickly add login and signup with Devise</p>
+ </a>
+ </li>
+ <li>
<a href="contributing">
<i>?</i>
<h3>Submit Your Own Guide</h3>
Something went wrong with that request. Please try again.