forked from ryanb/railscasts-episodes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
10,021 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Railscasts Episode #250: Authentication from Scratch | ||
|
||
http://railscasts.com/episodes/250 | ||
|
||
Commands | ||
|
||
rails g controller users new | ||
rails g model user email:string password_hash:string password_salt:string | ||
rake db:migrate | ||
rails dbconsole | ||
rails g controller sessions new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.bundle | ||
db/*.sqlite3 | ||
log/*.log | ||
tmp/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
source 'http://rubygems.org' | ||
|
||
gem 'rails', '3.0.3' | ||
|
||
# Bundle edge Rails instead: | ||
# gem 'rails', :git => 'git://github.com/rails/rails.git' | ||
|
||
gem 'sqlite3-ruby', :require => 'sqlite3' | ||
|
||
gem "bcrypt-ruby", :require => "bcrypt" | ||
|
||
# Use unicorn as the web server | ||
# gem 'unicorn' | ||
|
||
# Deploy with Capistrano | ||
# gem 'capistrano' | ||
|
||
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+) | ||
# gem 'ruby-debug' | ||
# gem 'ruby-debug19' | ||
|
||
# Bundle the extra gems: | ||
# gem 'bj' | ||
# gem 'nokogiri' | ||
# gem 'sqlite3-ruby', :require => 'sqlite3' | ||
# gem 'aws-s3', :require => 'aws/s3' | ||
|
||
# Bundle gems for the local environment. Make sure to | ||
# put test-only gems in this group so their generators | ||
# and rake tasks are available in development mode: | ||
# group :development, :test do | ||
# gem 'webrat' | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
abstract (1.0.0) | ||
actionmailer (3.0.3) | ||
actionpack (= 3.0.3) | ||
mail (~> 2.2.9) | ||
actionpack (3.0.3) | ||
activemodel (= 3.0.3) | ||
activesupport (= 3.0.3) | ||
builder (~> 2.1.2) | ||
erubis (~> 2.6.6) | ||
i18n (~> 0.4) | ||
rack (~> 1.2.1) | ||
rack-mount (~> 0.6.13) | ||
rack-test (~> 0.5.6) | ||
tzinfo (~> 0.3.23) | ||
activemodel (3.0.3) | ||
activesupport (= 3.0.3) | ||
builder (~> 2.1.2) | ||
i18n (~> 0.4) | ||
activerecord (3.0.3) | ||
activemodel (= 3.0.3) | ||
activesupport (= 3.0.3) | ||
arel (~> 2.0.2) | ||
tzinfo (~> 0.3.23) | ||
activeresource (3.0.3) | ||
activemodel (= 3.0.3) | ||
activesupport (= 3.0.3) | ||
activesupport (3.0.3) | ||
arel (2.0.6) | ||
bcrypt-ruby (2.1.2) | ||
builder (2.1.2) | ||
erubis (2.6.6) | ||
abstract (>= 1.0.0) | ||
i18n (0.5.0) | ||
mail (2.2.13) | ||
activesupport (>= 2.3.6) | ||
i18n (>= 0.4.0) | ||
mime-types (~> 1.16) | ||
treetop (~> 1.4.8) | ||
mime-types (1.16) | ||
polyglot (0.3.1) | ||
rack (1.2.1) | ||
rack-mount (0.6.13) | ||
rack (>= 1.0.0) | ||
rack-test (0.5.7) | ||
rack (>= 1.0) | ||
rails (3.0.3) | ||
actionmailer (= 3.0.3) | ||
actionpack (= 3.0.3) | ||
activerecord (= 3.0.3) | ||
activeresource (= 3.0.3) | ||
activesupport (= 3.0.3) | ||
bundler (~> 1.0) | ||
railties (= 3.0.3) | ||
railties (3.0.3) | ||
actionpack (= 3.0.3) | ||
activesupport (= 3.0.3) | ||
rake (>= 0.8.7) | ||
thor (~> 0.14.4) | ||
rake (0.8.7) | ||
sqlite3-ruby (1.2.5) | ||
thor (0.14.6) | ||
treetop (1.4.9) | ||
polyglot (>= 0.3.1) | ||
tzinfo (0.3.23) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
bcrypt-ruby | ||
rails (= 3.0.3) | ||
sqlite3-ruby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Add your own tasks in files placed in lib/tasks ending in .rake, | ||
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. | ||
|
||
require File.expand_path('../config/application', __FILE__) | ||
require 'rake' | ||
|
||
Auth::Application.load_tasks |
10 changes: 10 additions & 0 deletions
10
episode-250/auth/app/controllers/application_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class ApplicationController < ActionController::Base | ||
protect_from_forgery | ||
helper_method :current_user | ||
|
||
private | ||
|
||
def current_user | ||
@current_user ||= User.find(session[:user_id]) if session[:user_id] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class SessionsController < ApplicationController | ||
def new | ||
end | ||
|
||
def create | ||
user = User.authenticate(params[:email], params[:password]) | ||
if user | ||
session[:user_id] = user.id | ||
redirect_to root_url, :notice => "Logged in!" | ||
else | ||
flash.now.alert = "Invalid email or password" | ||
render "new" | ||
end | ||
end | ||
|
||
def destroy | ||
session[:user_id] = nil | ||
redirect_to root_url, :notice => "Logged out!" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class UsersController < ApplicationController | ||
def new | ||
@user = User.new | ||
end | ||
|
||
def create | ||
@user = User.new(params[:user]) | ||
if @user.save | ||
redirect_to root_url, :notice => "Signed up!" | ||
else | ||
render "new" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ApplicationHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module SessionsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class User < ActiveRecord::Base | ||
attr_accessible :email, :password, :password_confirmation | ||
|
||
attr_accessor :password | ||
before_save :encrypt_password | ||
|
||
validates_confirmation_of :password | ||
validates_presence_of :password, :on => :create | ||
validates_presence_of :email | ||
validates_uniqueness_of :email | ||
|
||
def self.authenticate(email, password) | ||
user = find_by_email(email) | ||
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) | ||
user | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def encrypt_password | ||
if password.present? | ||
self.password_salt = BCrypt::Engine.generate_salt | ||
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Auth</title> | ||
<%= stylesheet_link_tag :all %> | ||
<%= javascript_include_tag :defaults %> | ||
<%= csrf_meta_tag %> | ||
</head> | ||
<body> | ||
|
||
<div id="user_nav"> | ||
<% if current_user %> | ||
Logged in as <%= current_user.email %>. | ||
<%= link_to "Log out", log_out_path %> | ||
<% else %> | ||
<%= link_to "Sign up", sign_up_path %> or | ||
<%= link_to "log in", log_in_path %> | ||
<% end %> | ||
</div> | ||
|
||
<% flash.each do |name, msg| %> | ||
<%= content_tag :div, msg, :id => "flash_#{name}" %> | ||
<% end %> | ||
<%= yield %> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>Log in</h1> | ||
|
||
<%= form_tag sessions_path do %> | ||
<p> | ||
<%= label_tag :email %><br /> | ||
<%= text_field_tag :email, params[:email] %> | ||
</p> | ||
<p> | ||
<%= label_tag :password %><br /> | ||
<%= password_field_tag :password %> | ||
</p> | ||
<p class="button"><%= submit_tag "Log in" %></p> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<h1>Sign Up</h1> | ||
|
||
<%= form_for @user do |f| %> | ||
<% if @user.errors.any? %> | ||
<div class="error_messages"> | ||
<h2>Form is invalid</h2> | ||
<ul> | ||
<% for message in @user.errors.full_messages %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
<p> | ||
<%= f.label :email %><br /> | ||
<%= f.text_field :email %> | ||
</p> | ||
<p> | ||
<%= f.label :password %><br /> | ||
<%= f.password_field :password %> | ||
</p> | ||
<p> | ||
<%= f.label :password_confirmation %><br /> | ||
<%= f.password_field :password_confirmation %> | ||
</p> | ||
<p class="button"><%= f.submit %></p> | ||
<% end %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This file is used by Rack-based servers to start the application. | ||
|
||
require ::File.expand_path('../config/environment', __FILE__) | ||
run Auth::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require File.expand_path('../boot', __FILE__) | ||
|
||
require 'rails/all' | ||
|
||
# If you have a Gemfile, require the gems listed there, including any gems | ||
# you've limited to :test, :development, or :production. | ||
Bundler.require(:default, Rails.env) if defined?(Bundler) | ||
|
||
module Auth | ||
class Application < Rails::Application | ||
# Settings in config/environments/* take precedence over those specified here. | ||
# Application configuration should go into files in config/initializers | ||
# -- all .rb files in that directory are automatically loaded. | ||
|
||
# Custom directories with classes and modules you want to be autoloadable. | ||
# config.autoload_paths += %W(#{config.root}/extras) | ||
|
||
# Only load the plugins named here, in the order given (default is alphabetical). | ||
# :all can be used as a placeholder for all plugins not explicitly named. | ||
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] | ||
|
||
# Activate observers that should always be running. | ||
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer | ||
|
||
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. | ||
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. | ||
# config.time_zone = 'Central Time (US & Canada)' | ||
|
||
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. | ||
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] | ||
# config.i18n.default_locale = :de | ||
|
||
# JavaScript files you want as :defaults (application.js is always included). | ||
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails) | ||
|
||
# Configure the default encoding used in templates for Ruby 1.9. | ||
config.encoding = "utf-8" | ||
|
||
# Configure sensitive parameters which will be filtered from the log file. | ||
config.filter_parameters += [:password] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'rubygems' | ||
|
||
# Set up gems listed in the Gemfile. | ||
gemfile = File.expand_path('../../Gemfile', __FILE__) | ||
begin | ||
ENV['BUNDLE_GEMFILE'] = gemfile | ||
require 'bundler' | ||
Bundler.setup | ||
rescue Bundler::GemNotFound => e | ||
STDERR.puts e.message | ||
STDERR.puts "Try running `bundle install`." | ||
exit! | ||
end if File.exist?(gemfile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SQLite version 3.x | ||
# gem install sqlite3-ruby (not necessary on OS X Leopard) | ||
development: | ||
adapter: sqlite3 | ||
database: db/development.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
|
||
# Warning: The database defined as "test" will be erased and | ||
# re-generated from your development database when you run "rake". | ||
# Do not set this db to the same as development or production. | ||
test: | ||
adapter: sqlite3 | ||
database: db/test.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
|
||
production: | ||
adapter: sqlite3 | ||
database: db/production.sqlite3 | ||
pool: 5 | ||
timeout: 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Load the rails application | ||
require File.expand_path('../application', __FILE__) | ||
|
||
# Initialize the rails application | ||
Auth::Application.initialize! |
Oops, something went wrong.