Skip to content

Commit

Permalink
Implement basic login
Browse files Browse the repository at this point in the history
  • Loading branch information
supiccc committed Feb 22, 2017
1 parent 28d146b commit 05a36dc
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/sessions.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

include SessionsHelper

def hello
render html: "hello, world"
end
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class SessionsController < ApplicationController
def new
end

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end

def destroy
log_out
redirect_to root_path
end
end
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def show
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
Expand Down
18 changes: 18 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end

def log_out
session.delete(:user_id)
@current_user = nil
end

def current_user
@current_user ||= User.find_by(id: session[:user_id])
end

def logged_in?
!current_user.nil?
end
end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ class User < ApplicationRecord
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password

def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
18 changes: 17 additions & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Log in", '#' %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Account<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Setting", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
Expand Down
20 changes: 20 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% provide(:title, "Log in") %>
<h1>Log in</h1>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(:session, url: login_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.submit "Log in", class: "btn btn-primary" %>
<% end %>
<p> </p>
<p>New User? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do

get 'sessions/new'

get '/signup', to: 'users#new'

get '/help', to: 'static_pages#help'
Expand All @@ -9,6 +11,12 @@
get '/contact', to: 'static_pages#contact'

post '/signup', to: 'users#create'

get '/login', to: 'sessions#new'

post '/login', to: 'sessions#create'

delete '/logout', to: 'sessions#destroy'

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'static_pages#home'
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get sessions_new_url
assert_response :success
end

end
4 changes: 4 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
40 changes: 40 additions & 0 deletions test/integration/users_login_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
def setup
@user = users(:michael)
end

test "login with invalid information" do
get login_path
assert_template 'sessions/new'
post login_path, params: { session: { email: "", password: "" } }
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end

test "login with valid information by logout" do
get login_path
post login_path, params: { session: { email: @user.email,
password: 'password' } }
assert is_logged_in?
assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_path
follow_redirect!
assert_select "a[href=?]", login_path
assert_select "a[href=?]", logout_path, count: 0
assert_select "a[href=?]", user_path(@user), count: 0
end
end
3 changes: 2 additions & 1 deletion test/integration/users_signup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
end
follow_redirect!
assert_template 'users/show'
assert_not flash.blank?
assert_not flash.empty?
assert is_logged_in?
end
end
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper

def is_logged_in?
!session[:user_id].nil?
end

# Add more helper methods to be used by all tests here...
end

0 comments on commit 05a36dc

Please sign in to comment.