Skip to content
Browse files

Signin

  • Loading branch information...
1 parent 81cdd76 commit 3e861a64269ca1314f1b8fdc1266ec5409e95b0a @diasks2 committed Apr 29, 2012
View
43 README.md
@@ -247,7 +247,48 @@ You can copy the code from [this example](https://github.com/diasks2/surveyor_ex
end
end
-34) Create a signin view [app/views/sessions/new.html.erb]()
+34) Create a signin view [app/views/sessions/new.html.erb](https://github.com/diasks2/surveyor_example/blob/master/app/views/sessions/new.html.erb)
+
+ <h1>Sign in</h1>
+
+ <%= form_for(:session, url: sessions_path) do |f| %>
+
+ <%= f.label :email %>
+ <%= f.text_field :email %>
+
+ <%= f.label :password %>
+ <%= f.password_field :password %>
+
+ <%= f.submit "Sign in" %>
+ <% end %>
+
+35) Update the SessionsController
+
+36) Update the ApplicationController
+
+37) Generate remember token
+
+ $ rails generate migration add_remember_token_to_users
+
+38) Add the remember token to the users table (db/migrate/[timestamp]_add_remember_token_to_users.rb)
+
+ class AddRememberTokenToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :remember_token, :string
+ add_index :users, :remember_token
+ end
+ end
+
+39) Migrate the database
+
+ $ bundle exec rake db:migrate
+
+40) Update the user model
+
+41) Add a SessionsHelper
+
+
+
View
3 app/controllers/application_controller.rb
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
-end
+ include SessionsHelper
+end
View
9 app/controllers/sessions_controller.rb
@@ -3,8 +3,17 @@ def new
end
def create
+ user = User.find_by_email(params[:session][:email])
+ if user && user.authenticate(params[:session][:password])
+ sign_in user
+ redirect_to user
+ else
+ render 'new'
+ end
end
def destroy
+ sign_out
+ redirect_to root_path
end
end
View
1 app/controllers/users_controller.rb
@@ -10,6 +10,7 @@ def show
def create
@user = User.new(params[:user])
if @user.save
+ sign_in @user
redirect_to @user
else
render 'new'
View
31 app/helpers/sessions_helper.rb
@@ -1,2 +1,31 @@
module SessionsHelper
-end
+
+ def sign_in(user)
+ cookies.permanent[:remember_token] = user.remember_token
+ current_user = user
+ end
+
+ def signed_in?
+ !current_user.nil?
+ end
+
+ def sign_out
+ current_user = nil
+ cookies.delete(:remember_token)
+ end
+
+ def current_user=(user)
+ @current_user = user
+ end
+
+ def current_user
+ @current_user ||= user_from_remember_token
+ end
+
+ private
+
+ def user_from_remember_token
+ remember_token = cookies[:remember_token]
+ User.find_by_remember_token(remember_token) unless remember_token.nil?
+ end
+end
View
7 app/models/user.rb
@@ -3,6 +3,7 @@ class User < ActiveRecord::Base
has_secure_password
before_save { |user| user.email = email.downcase }
+ before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
@@ -11,4 +12,10 @@ class User < ActiveRecord::Base
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
+
+private
+
+ def create_remember_token
+ self.remember_token = SecureRandom.urlsafe_base64
+ end
end
View
14 app/views/sessions/new.html.erb
@@ -1,2 +1,12 @@
-<% provide(:title, "Sign in") %>
-<h1>Sign in</h1>
+<h1>Sign in</h1>
+
+<%= form_for(:session, url: sessions_path) do |f| %>
+
+<%= f.label :email %>
+<%= f.text_field :email %>
+
+<%= f.label :password %>
+<%= f.password_field :password %>
+
+<%= f.submit "Sign in" %>
+<% end %>
View
6 db/migrate/20120429084100_add_remember_token_to_users.rb
@@ -0,0 +1,6 @@
+class AddRememberTokenToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :remember_token, :string
+ add_index :users, :remember_token
+ end
+end
View
4 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120427001716) do
+ActiveRecord::Schema.define(:version => 20120429084100) do
create_table "answers", :force => true do |t|
t.integer "question_id"
@@ -172,9 +172,11 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
+ t.string "remember_token"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
+ add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
create_table "validation_conditions", :force => true do |t|
t.integer "validation_id"

0 comments on commit 3e861a6

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