Today I'll be showing you how to add DailyCred to a fully functional Ruby on Rails application to get comprehensive authentication functionality. This tutorial assumes you have a basic understanding of Ruby on Rails (RoR) and can follow along basic tutorials. Before you get started you need to sign up for dailycred so you can get API keys, which you can obtain on your settings page.

The first step is to clone the wonderful demo To-Do app that Engine Yard has provided on github as an example RoR app. This app already includes everything you need to create tasks and save them to a list. We will be adding the functionality to authenticate into the app and save tasks to specific user accounts. To start, open your terminal and execute:

git clone https://github.com/engineyard/todo.git todo
cd todo

Next, add the dailycred gem to your gemfile.

gem 'dailycred'

I also removed the line "gem 'mysql2', '~> 0.2.7'" as I don't wish to use or install the mysql2 gem (and you don't either to follow along with this tutorial). When you're all set, bundle your gems by running:

bundle

Now have your dailycred client_id and secret_key ready and run:

rails g dailycred your_client_id your_secret_key

This will generate everything you need to get going with authentication, including a user model, session controller, omniauth initializer, javascript tracking code, and many helper variables.

Congratulations!

You've officially set up authentication on your rails site. Start your server with rails s and point your browser to locahost:3000/auth. Click the 'Sign Up' button and go ahead an create an account.

But you may feel like something is missing. While you can sign in and out of your app, the experience isn't personal at all! We need a way to allow users to have their own lists and tasks.

Next let's add a property to tasks and lists to associate them with a specific user. We will use the classic 'belongs_to' relationship, where a user has many tasks and lists. We will you the 'has_many :through' relationship to describe how users have many tasks through lists. First run some migrations to add a user_id property to tasks and lists:

rails g migration AddUserIdToLists user_id:string
rake db:migrate

open the file app/models/list.rb and add the line:

belongs_to :user

open app/models/user.rb and add

has_many :tasks , :through => :lists
has_many :lists , :dependent => :destroy

That takes care of our relationships. Open up app/controllers/lists_controller.rb and edit line 4 to

@list = current_user.lists.new(params[:list])    

This changes the creation of the list to belong to the current signed in user. Open app/controllers/tasks_controller.rb and add the following before_filter methods.

before_filter :authenticate

This makes it so that a user who isn't yet authenticated is automatically redirected to the login screen. It also creates creates a @user instance variable that is available in all controller methods for convenience. Finally change the index method in tasks_controller.rb to the following:

def index
  @todo   = current_user.tasks.where(:done => false)
  @task   = current_user.tasks.new
  @lists  = current_user.lists
  @list   = current_user.lists.new

  respond_to do |format|
    format.html
  end
end

Success!

You are all set up. You have a fully functional to-do application where users can sign in to your app and create lists and tasks. You should be proud of yourself.

Finishing up

Custom Events

Wouldn't it be fun to be able to keep track of when your users created new tasks and lists? Dailycred has a custom events feature that lets you do just that.

Go back to your app/controllers/tasks_controller.rb and edit the create method to look like the following:

def create
  @list = List.find(params[:list_id])
  @task = @list.tasks.new(params[:task])
  if @task.save
      dailycred.event(current_user.uid, "New Task", @task.name)
      flash[:notice] = "Your task was created."
  else
      flash[:alert] = "There was an error creating your task."
  end
  redirect_to(list_tasks_url(@list))
end

And do something similiar in app/controllers/lists_controller.rb:

def create
  @list = current_user.lists.new(params[:list])
  if @list.save
      dailycred.event(current_user.uid, "New List", @list.name)
      flash[:notice] = "Your list was created"
  else
      flash[:alert] = "There was an error creating your list."
  end
  redirect_to(list_tasks_url(@list))
end

Now when new tasks and lists are created by your users, you can see the events being created in your dashboard

Finishing up

You're all set up! I hope this was helpful in getting more acquainted with Dailycred and how to work with our service. Thanks for checking us out and always email us at support@dailycred.com For any questions!