Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a guide for adding authentication with Devise #36

Merged
merged 1 commit into from
Nov 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions _posts/2012-11-01-devise.markdown
Original file line number Diff line number Diff line change
@@ -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)


9 changes: 8 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h2>Rails Girls Guides</h2>
<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>
Expand Down Expand Up @@ -50,6 +50,13 @@ <h3>Put Your App Online With Heroku</h3>
<p>Instructions to get your app running on Heroku.</p>
</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>
Expand Down