Once you've installed Ruby, Ruby on Rails, and RubyGems, download a copy of the CS50 Library from RubyGems via:
$ gem install cs50
Now, create a new project, like cs50id:
$ rails new cs50id
Now, add the CS50 library to the project by adding the following to /Gemfile:
gem 'cs50'
The project has been configured to use the CS50 Library, so a new controller, called Auth, can be created to handle
user authentication:
$ rails generate controller Auth
In order to log in, users will be redirected to CS50 ID, via a link generated by the CS50 Library. We must specify the
location of a temporary directory to store login information, the registered TRUST_ROOT and RETURN_TO, and the object
representing the current session. In /add/controllers/auth_controller.rb, create a new action for logging in:
def login
# user already logged in, redirect to index
if (session[:id])
redirect_to :action => :index
# redirect to CS50 ID
else
redirect_to CS50.getLoginUrl(Rails.root.join("tmp"), "http://localhost:3000",
"http://localhost:3000/auth/return", session)
end
end
Once the user authenticates via CS50 ID, they'll be brought back to `http://localhost:3000/auth/return`. At this point,
we can retrieve information associated with the user that successfully logged in. To do so, we must again specify our
temporary directory and RETURN_TO, as well as the current session and params objects (where the user information is
stored). Because our RETURN_TO is /auth/return, we need to create a return action:
def return
# get authenticated user information
user = CS50.getUser(Rails.root.join("tmp"), "http://localhost:3000/auth/return",
session, params)
# remember which user, if any, logged in
if (user)
session[:id] = user[:id]
if (user[:email])
session[:email] = user[:email]
end
if (user[:name])
session[:name] = user[:name]
end
end
redirect_to :action => :index
end
Because we stored user information in the session, a logout is accomplished by clearing the session.
def logout
# clear the user's information from the session
session[:id] = nil
session[:email] = nil
session[:name] = nil
redirect_to :action => :index
end
We have specified an index action in both login and logout, so we must create one:
def index
end
Now, create the corresponding view, /app/views/auth/index.html.erb:
<% if (session[:id]) %>
You are logged in. <%= link_to "Log out", :action => "logout" %>
Your unique ID for this site is <%= session[:id] %>
Your email address is <%= session[:email] %>
<% else %>
You are not logged in. <%= link_to "Log in", :action => "login" %>
<% end %>
Finally, we must set up routes to the four actions we've created, so add the following to /app/config/routes.rb
match 'auth' => 'auth#index'
match 'auth/login' => 'auth#login'
match 'auth/logout' => 'auth#logout'
match 'auth/return' => 'auth#return'
To run the app, start the Rails server with:
$ rails s
Now, navigate to http://localhost:3000/auth. You should see a link to log in, at which point you'll be redirected to
CS50 ID. After authenticating, you'll be brought back to your app, and you'll see your user information and a logout link!