Skip to content

Commit fd5952a

Browse files
committed
Finish sign in
1 parent cee228c commit fd5952a

File tree

19 files changed

+334
-4
lines changed

19 files changed

+334
-4
lines changed

app/assets/javascripts/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
//
1313
//= require jquery
1414
//= require jquery_ujs
15+
//= require bootstrap-dropdown
1516
//= require_tree .
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Sessions controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class ApplicationController < ActionController::Base
22
protect_from_forgery
3+
include SessionsHelper
34
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SessionsController < ApplicationController
2+
def new
3+
end
4+
5+
def create
6+
user = User.find_by_email(params[:session][:email].downcase)
7+
if user && user.authenticate(params[:session][:password])
8+
sign_in user
9+
redirect_to user
10+
else
11+
flash.now[:error] = 'Invalid email/password combination'
12+
render 'new'
13+
end
14+
end
15+
16+
def destroy
17+
sign_out
18+
redirect_to root_url
19+
end
20+
end

app/controllers/users_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def new
1010
def create
1111
@user = User.new(params[:user])
1212
if @user.save
13+
sign_in @user
1314
flash[:success] = "Welcome to the Sample App!"
1415
redirect_to @user
1516
else

app/helpers/sessions_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module SessionsHelper
2+
def sign_in(user)
3+
cookies.permanent[:remember_token] = user.remember_token
4+
self.current_user = user
5+
end
6+
7+
def signed_in?
8+
!current_user.nil?
9+
end
10+
11+
def current_user=(user)
12+
@current_user = user
13+
end
14+
15+
def current_user
16+
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
17+
end
18+
19+
def sign_out
20+
self.current_user = nil
21+
cookies.delete(:remember_token)
22+
end
23+
end

app/models/user.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ class User < ActiveRecord::Base
33
has_secure_password
44

55
before_save { self.email.downcase }
6+
before_save :create_remember_token
67

78
validates :name, presence: true, length: { maximum: 50 }
89
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
910
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
1011
uniqueness: { case_sensitive: false }
1112
validates :password, presence: true, length: { minimum: 6 }
1213
validates :password_confirmation, presence: true
14+
15+
private
16+
17+
def create_remember_token
18+
self.remember_token = SecureRandom.urlsafe_base64
19+
end
1320
end

app/views/layouts/_header.html.erb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
<header class="navbar navbar-fixed-top navbar-inverse">
22
<div class="navbar-inner">
33
<div class="container">
4-
<%= link_to "sample app", '#', id: "logo" %>
4+
<%= link_to "sample app", root_path, id: "logo" %>
55
<nav>
66
<ul class="nav pull-right">
77
<li><%= link_to "Home", root_path %></li>
88
<li><%= link_to "Help", help_path %></li>
9-
<li><%= link_to "Sign in", '#' %></li>
9+
<% if signed_in? %>
10+
<li><%= link_to "Users", '#' %></li>
11+
<li id="fat-menu" class="dropdown">
12+
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
13+
Account <b class="caret"></b>
14+
</a>
15+
<ul class="dropdown-menu">
16+
<li><%= link_to "Profile", current_user %></li>
17+
<li><%= link_to "Settings", '#' %></li>
18+
<li class="divider"></li>
19+
<li>
20+
<%= link_to "Sign out", signout_path, method: "delete" %>
21+
</li>
22+
</ul>
23+
</li>
24+
<% else %>
25+
<li><%= link_to "Sign in", signin_path %></li>
26+
<% end %>
1027
</ul>
1128
</nav>
1229
</div>

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<%= render 'layouts/header' %>
1111
<div class="container">
1212
<% flash.each do |key, value| %>
13-
<%= content_for(:div, value, class: "alert alert-#{key}") %>
13+
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
1414
<% end %>
1515
<%= yield %>
1616
<%= render 'layouts/footer' %>

0 commit comments

Comments
 (0)