Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lot of code

  • Loading branch information...
commit d07cab64574b9bab397d7c9ac3490f80fe6956e0 1 parent f9d8d42
Gabriel Chertok authored
View
5 Gemfile
@@ -1,6 +1,6 @@
source 'http://rubygems.org'
-gem 'rails', '3.0.10'
+gem 'rails', '3.1.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -29,3 +29,6 @@ gem 'sqlite3'
# group :development, :test do
# gem 'webrat'
# end
+
+gem 'warden'
+gem 'bcrypt-ruby'
View
116 Gemfile.lock
@@ -1,78 +1,94 @@
GEM
remote: http://rubygems.org/
specs:
- abstract (1.0.0)
- actionmailer (3.0.10)
- actionpack (= 3.0.10)
- mail (~> 2.2.19)
- actionpack (3.0.10)
- activemodel (= 3.0.10)
- activesupport (= 3.0.10)
- builder (~> 2.1.2)
- erubis (~> 2.6.6)
- i18n (~> 0.5.0)
- rack (~> 1.2.1)
- rack-mount (~> 0.6.14)
- rack-test (~> 0.5.7)
- tzinfo (~> 0.3.23)
- activemodel (3.0.10)
- activesupport (= 3.0.10)
- builder (~> 2.1.2)
- i18n (~> 0.5.0)
- activerecord (3.0.10)
- activemodel (= 3.0.10)
- activesupport (= 3.0.10)
- arel (~> 2.0.10)
- tzinfo (~> 0.3.23)
- activeresource (3.0.10)
- activemodel (= 3.0.10)
- activesupport (= 3.0.10)
- activesupport (3.0.10)
- arel (2.0.10)
- builder (2.1.2)
- erubis (2.6.6)
- abstract (>= 1.0.0)
- i18n (0.5.0)
- json (1.6.3)
- mail (2.2.19)
- activesupport (>= 2.3.6)
+ actionmailer (3.1.3)
+ actionpack (= 3.1.3)
+ mail (~> 2.3.0)
+ actionpack (3.1.3)
+ activemodel (= 3.1.3)
+ activesupport (= 3.1.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.5)
+ rack-cache (~> 1.1)
+ rack-mount (~> 0.8.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.0.3)
+ activemodel (3.1.3)
+ activesupport (= 3.1.3)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.3)
+ activemodel (= 3.1.3)
+ activesupport (= 3.1.3)
+ arel (~> 2.2.1)
+ tzinfo (~> 0.3.29)
+ activeresource (3.1.3)
+ activemodel (= 3.1.3)
+ activesupport (= 3.1.3)
+ activesupport (3.1.3)
+ multi_json (~> 1.0)
+ arel (2.2.1)
+ bcrypt-ruby (3.0.1)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ json (1.6.4)
+ mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
+ multi_json (1.0.4)
polyglot (0.3.3)
- rack (1.2.4)
- rack-mount (0.6.14)
+ rack (1.3.6)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-mount (0.8.3)
rack (>= 1.0.0)
- rack-test (0.5.7)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
rack (>= 1.0)
- rails (3.0.10)
- actionmailer (= 3.0.10)
- actionpack (= 3.0.10)
- activerecord (= 3.0.10)
- activeresource (= 3.0.10)
- activesupport (= 3.0.10)
+ rails (3.1.3)
+ actionmailer (= 3.1.3)
+ actionpack (= 3.1.3)
+ activerecord (= 3.1.3)
+ activeresource (= 3.1.3)
+ activesupport (= 3.1.3)
bundler (~> 1.0)
- railties (= 3.0.10)
- railties (3.0.10)
- actionpack (= 3.0.10)
- activesupport (= 3.0.10)
+ railties (= 3.1.3)
+ railties (3.1.3)
+ actionpack (= 3.1.3)
+ activesupport (= 3.1.3)
+ rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
- thor (~> 0.14.4)
+ thor (~> 0.14.6)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ sprockets (2.0.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.5)
thor (0.14.6)
+ tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.31)
+ warden (1.1.0)
+ rack (>= 1.0)
PLATFORMS
ruby
DEPENDENCIES
- rails (= 3.0.10)
+ bcrypt-ruby
+ rails (= 3.1.3)
sqlite3
+ warden
View
11 app/controllers/application_controller.rb
@@ -1,3 +1,14 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ helper_method :current_user
+
+ def current_user
+ warden.user
+ end
+
+ def warden
+ env['warden']
+ end
+
end
View
17 app/controllers/sessions_controller.rb
@@ -0,0 +1,17 @@
+class SessionsController < ApplicationController
+
+ def new
+ flash.now.alert = warden.message if warden.message.present?
+ end
+
+ def create
+ warden.authenticate!
+ redirect_to root_url, notice: "Logged in!"
+ end
+
+ def destroy
+ warden.logout
+ redirect_to root_url, notice: "Logged out!"
+ end
+
+end
View
17 app/controllers/users_controller.rb
@@ -0,0 +1,17 @@
+class UsersController < ApplicationController
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ warden.set_user(@user)
+ redirect_to root_url, notice: "Signed up!"
+ else
+ render "new"
+ end
+ end
+
+end
View
2  app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
View
2  app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
+module UsersHelper
+end
View
7 app/models/user.rb
@@ -0,0 +1,7 @@
+class User < ActiveRecord::Base
+ attr_accessible :email, :password, :password_confirmation
+ validates_presence_of :password, on: :create
+ validates_presence_of :email
+ has_secure_password
+
+end
View
4 app/views/layouts/application.html.erb
@@ -7,7 +7,9 @@
<%= csrf_meta_tag %>
</head>
<body>
-
+<% if current_user.present? %>
+ <p>Hello! <%= current_user.email %></p>
+<% end %>
<%= yield %>
</body>
View
13 app/views/sessions/new.html.erb
@@ -0,0 +1,13 @@
+<h1>Log in</h1>
+
+<%= form_tag sessions_path do %>
+ <div class="field">
+ <%= label_tag :email %><br>
+ <%= text_field_tag :email, params[:email] %>
+ </div>
+ <div class="field">
+ <%= label_tag :password %><br>
+ <%= password_field_tag :password %>
+ </div>
+ <div class="actions"><%= submit_tag "Log in" %></div>
+<% end %>
View
27 app/views/users/new.html.erb
@@ -0,0 +1,27 @@
+<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 %>
+ <div class="field">
+ <%= f.label :email %><br>
+ <%= f.text_field :email %>
+ </div>
+ <div class="field">
+ <%= f.label :password %><br>
+ <%= f.password_field :password %>
+ </div>
+ <div class="field">
+ <%= f.label :password_confirmation %><br>
+ <%= f.password_field :password_confirmation %>
+ </div>
+ <div class="actions"><%= f.submit %></div>
+<% end %>
View
3  config/environments/development.rb
@@ -10,8 +10,7 @@
config.whiny_nils = true
# Show full error reports and disable caching
- config.consider_all_requests_local = true
- config.action_view.debug_rjs = true
+ config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
View
27 config/initializers/warden.rb
@@ -0,0 +1,27 @@
+Rails.application.config.middleware.use Warden::Manager do |manager|
+ manager.default_strategies :password
+ manager.failure_app = lambda { |env| SessionsController.action(:new).call(env) }
+end
+
+Warden::Manager.serialize_into_session do |user|
+ user.id
+end
+
+Warden::Manager.serialize_from_session do |id|
+ User.find(id)
+end
+
+Warden::Strategies.add(:password) do
+ def valid?
+ params['email'] && params['password']
+ end
+
+ def authenticate!
+ user = User.find_by_email(params['email'])
+ if user && user.authenticate(params['password'])
+ success! user
+ else
+ fail "Invalid email or password"
+ end
+ end
+end
View
4 config/routes.rb
@@ -1,5 +1,7 @@
Blog::Application.routes.draw do
resources :posts
+ resources :sessions
+ resources :users
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -50,7 +52,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- # root :to => "welcome#index"
+ root :to => "posts#index"
# See how all your routes lay out with "rake routes"
View
15 db/migrate/20120102124823_create_users.rb
@@ -0,0 +1,15 @@
+class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+ t.string :name
+ t.string :email
+ t.string :password_digest
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :users
+ end
+end
View
10 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111219200744) do
+ActiveRecord::Schema.define(:version => 20120102124823) do
create_table "posts", :force => true do |t|
t.string "title"
@@ -22,4 +22,12 @@
t.datetime "updated_at"
end
+ create_table "users", :force => true do |t|
+ t.string "name"
+ t.string "email"
+ t.string "password_digest"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
end
View
7 test/fixtures/users.yml
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ name: MyString
+
+two:
+ name: MyString
View
8 test/functional/sessions_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
8 test/functional/users_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class UsersControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
4 test/unit/helpers/sessions_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/users_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class UsersHelperTest < ActionView::TestCase
+end
View
8 test/unit/user_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit d07cab6

Please sign in to comment.
Something went wrong with that request. Please try again.