Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Signin

  • Loading branch information...
commit 3e861a64269ca1314f1b8fdc1266ec5409e95b0a 1 parent 81cdd76
Kevin Dias authored
43 README.md
Source Rendered
@@ -247,7 +247,48 @@ You can copy the code from [this example](https://github.com/diasks2/surveyor_ex
247 247 end
248 248 end
249 249
250   -34) Create a signin view [app/views/sessions/new.html.erb]()
  250 +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)
  251 +
  252 + <h1>Sign in</h1>
  253 +
  254 + <%= form_for(:session, url: sessions_path) do |f| %>
  255 +
  256 + <%= f.label :email %>
  257 + <%= f.text_field :email %>
  258 +
  259 + <%= f.label :password %>
  260 + <%= f.password_field :password %>
  261 +
  262 + <%= f.submit "Sign in" %>
  263 + <% end %>
  264 +
  265 +35) Update the SessionsController
  266 +
  267 +36) Update the ApplicationController
  268 +
  269 +37) Generate remember token
  270 +
  271 + $ rails generate migration add_remember_token_to_users
  272 +
  273 +38) Add the remember token to the users table (db/migrate/[timestamp]_add_remember_token_to_users.rb)
  274 +
  275 + class AddRememberTokenToUsers < ActiveRecord::Migration
  276 + def change
  277 + add_column :users, :remember_token, :string
  278 + add_index :users, :remember_token
  279 + end
  280 + end
  281 +
  282 +39) Migrate the database
  283 +
  284 + $ bundle exec rake db:migrate
  285 +
  286 +40) Update the user model
  287 +
  288 +41) Add a SessionsHelper
  289 +
  290 +
  291 +
251 292
252 293
253 294
3  app/controllers/application_controller.rb
... ... @@ -1,3 +1,4 @@
1 1 class ApplicationController < ActionController::Base
2 2 protect_from_forgery
3   -end
  3 + include SessionsHelper
  4 +end
9 app/controllers/sessions_controller.rb
@@ -3,8 +3,17 @@ def new
3 3 end
4 4
5 5 def create
  6 + user = User.find_by_email(params[:session][:email])
  7 + if user && user.authenticate(params[:session][:password])
  8 + sign_in user
  9 + redirect_to user
  10 + else
  11 + render 'new'
  12 + end
6 13 end
7 14
8 15 def destroy
  16 + sign_out
  17 + redirect_to root_path
9 18 end
10 19 end
1  app/controllers/users_controller.rb
@@ -10,6 +10,7 @@ def show
10 10 def create
11 11 @user = User.new(params[:user])
12 12 if @user.save
  13 + sign_in @user
13 14 redirect_to @user
14 15 else
15 16 render 'new'
31 app/helpers/sessions_helper.rb
... ... @@ -1,2 +1,31 @@
1 1 module SessionsHelper
2   -end
  2 +
  3 + def sign_in(user)
  4 + cookies.permanent[:remember_token] = user.remember_token
  5 + current_user = user
  6 + end
  7 +
  8 + def signed_in?
  9 + !current_user.nil?
  10 + end
  11 +
  12 + def sign_out
  13 + current_user = nil
  14 + cookies.delete(:remember_token)
  15 + end
  16 +
  17 + def current_user=(user)
  18 + @current_user = user
  19 + end
  20 +
  21 + def current_user
  22 + @current_user ||= user_from_remember_token
  23 + end
  24 +
  25 + private
  26 +
  27 + def user_from_remember_token
  28 + remember_token = cookies[:remember_token]
  29 + User.find_by_remember_token(remember_token) unless remember_token.nil?
  30 + end
  31 +end
7 app/models/user.rb
@@ -3,6 +3,7 @@ class User < ActiveRecord::Base
3 3 has_secure_password
4 4
5 5 before_save { |user| user.email = email.downcase }
  6 + before_save :create_remember_token
6 7
7 8 validates :name, presence: true, length: { maximum: 50 }
8 9 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
@@ -11,4 +12,10 @@ class User < ActiveRecord::Base
11 12 uniqueness: { case_sensitive: false }
12 13 validates :password, length: { minimum: 6 }
13 14 validates :password_confirmation, presence: true
  15 +
  16 +private
  17 +
  18 + def create_remember_token
  19 + self.remember_token = SecureRandom.urlsafe_base64
  20 + end
14 21 end
14 app/views/sessions/new.html.erb
... ... @@ -1,2 +1,12 @@
1   -<% provide(:title, "Sign in") %>
2   -<h1>Sign in</h1>
  1 +<h1>Sign in</h1>
  2 +
  3 +<%= form_for(:session, url: sessions_path) do |f| %>
  4 +
  5 +<%= f.label :email %>
  6 +<%= f.text_field :email %>
  7 +
  8 +<%= f.label :password %>
  9 +<%= f.password_field :password %>
  10 +
  11 +<%= f.submit "Sign in" %>
  12 +<% end %>
6 db/migrate/20120429084100_add_remember_token_to_users.rb
... ... @@ -0,0 +1,6 @@
  1 +class AddRememberTokenToUsers < ActiveRecord::Migration
  2 + def change
  3 + add_column :users, :remember_token, :string
  4 + add_index :users, :remember_token
  5 + end
  6 +end
4 db/schema.rb
@@ -11,7 +11,7 @@
11 11 #
12 12 # It's strongly recommended to check this file into your version control system.
13 13
14   -ActiveRecord::Schema.define(:version => 20120427001716) do
  14 +ActiveRecord::Schema.define(:version => 20120429084100) do
15 15
16 16 create_table "answers", :force => true do |t|
17 17 t.integer "question_id"
@@ -172,9 +172,11 @@
172 172 t.datetime "created_at", :null => false
173 173 t.datetime "updated_at", :null => false
174 174 t.string "password_digest"
  175 + t.string "remember_token"
175 176 end
176 177
177 178 add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  179 + add_index "users", ["remember_token"], :name => "index_users_on_remember_token"
178 180
179 181 create_table "validation_conditions", :force => true do |t|
180 182 t.integer "validation_id"

0 comments on commit 3e861a6

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