Wednesday, July 16, 2014

User Authentication on ruby on rails

Devise is the ruby gem we need for this.


The official guide is here:

https://github.com/plataformatec/devise

I additionally had to make app/controllers/application_controller.rb look like the following to get the sign in to happen:
class ApplicationController < ActionController::Base  # Prevent CSRF attacks by raising an exception.  # For APIs, you may want to use :null_session instead.  protect_from_forgery with: :exception
  before_filter :require_login
  before_filter :check_if_devise
  private
    def check_if_devise        if params[:controller].include? "devise/registrations" and params[:action].include? "create"        end    end
    def require_login      if not current_user and not params[:controller].include? "devise"        redirect_to new_user_session_path      end    end
end
Then I had to make app/views/layouts/application.html.erb look like this:
<!DOCTYPE html><html><head>  <title>Universalnewsaggregator</title>  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>  <%= csrf_meta_tags %></head><body><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 class="notice"><%= notice %></p><p class="alert"><%= alert %></p><%= yield %>
</body></html>

The user auth code I got from here:
http://guides.railsgirls.com/devise/

And at this point I finally get a login for the site that looks like this:

The commands that I used for this project.
Create the heroku project and copy the given command to check out the project locally.
git clone git@heroku.com:universalnewsaggregator.git -o heroku
rails new universalnewsaggregator
cd universalnewsaggregator
vi Gemfile
  # add in devise and other required gems.
bundle install
rake db:drop
  # just to get rid of the old similar database I had created before on first try.
rails generate devise:install
rails generate devise user
  # or instead of user, you could call it member, or whatever.
rake db:migrate
vi apps/views/layouts/application.html.erb 
vi app/controllers/application_controller.rb
vi app/views/layouts/application.html.erb
rails generate scaffold news
vi config/routes.rb
  # make sure it has the following:
  devise_for :users
  resources :news
  root "news#index"

# edit the database to use postgress
vi config/database.yml
  # put in the postgres sections

#finally push it back up
git add -A .
git commit -m "Basic Site with User authentication"
git push heroku master



No comments:

Post a Comment