Commit 44c45cd3 authored by takanotume24's avatar takanotume24

Implement basic login

parent 77b000b3
......@@ -11,5 +11,7 @@
// about supported directives.
//
//= require rails-ujs
//= require jquery
//= require bootstrap
//= require turbolinks
//= require_tree .
# 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/
// 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/
# frozen_string_literal: true
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
rendler html: "hello, world!"
end
include SessionsHelper
end
# frozen_string_literal: true
class SessionsController < ApplicationController
def new; end
def create
user = User.find_by(email: params[:session][:email].downcase)
if 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_url
end
end
......@@ -12,6 +12,7 @@ class UsersController < ApplicationController
def create
@user = User.new user_params
if @user.save
log_in @user
flash[:success] = 'Welcome to the Sample App!'
redirect_to @user
else
......
# frozen_string_literal: true
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def log_out
session.delete(:user_id)
@current_user = nil
end
end
......@@ -9,4 +9,9 @@ class User < ApplicationRecord
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
def self.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
end
......@@ -5,7 +5,24 @@
<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 'Settings', "#" %></li>
<li class="divider"></li>
<li>
<%= link_to 'Log out', logout_path, method: :delete %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to 'Log in', login_path %></li>
<% end %>
</ul>
</nav>
</div>
......
<%provide(:title, "Log in")%>
<h1>Log in</h1>
<div class="row">
<div class="col-md-6 cod-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>New user? <%= link_to 'Sign up now!', signup_path %></p>
</div>
</div>
\ No newline at end of file
# frozen_string_literal: true
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
# frozen_string_literal: true
require 'test_helper'
class SessionsControllerTest < ActionDispatch::IntegrationTest
test 'should get new' do
get login_path
assert_response :success
end
end
michael:
name: Micheal Example
email: [email protected]
password_digest: <%= User.digest("password")%>
\ No newline at end of file
# frozen_string_literal: true
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test 'login with valid informaiton' do
get login_path
post login_path, params: { session: { email: @user.email,
password: 'password' } }
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)
end
test 'login with invalid information' do
get login_path
assert_template 'sessions/new'
post login_path params: { session: { email: '', password: '' } }
assert_select 'div.alert' do
assert_select 'div', 'Invalid email/password combination'
end
assert_template 'sessions/new'
assert_not flash.empty?
get root_path
assert flash.empty?
end
test 'login with valid information followed 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=?][data-method=?]', logout_path, 'delete'
assert_select 'a[href=?]', user_path(@user)
delete logout_path
assert_not is_logged_in?
assert_redirected_to root_url
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
......@@ -34,5 +34,6 @@ class UsersSignupTest < ActionDispatch::IntegrationTest
follow_redirect!
assert_template 'users/show'
assert_not flash.blank?
assert is_logged_in?
end
end
# frozen_string_literal: true
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require File.expand_path('../config/environment', __dir__)
require 'rails/test_help'
class ActiveSupport::TestCase
......@@ -7,4 +9,7 @@ class ActiveSupport::TestCase
fixtures :all
include ApplicationHelper
# Add more helper methods to be used by all tests here...
def is_logged_in?
!session[:user_id].nil?
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment