feat(sample): add session and header
This commit is contained in:
parent
9fac43f46d
commit
8baa51f611
@ -3,5 +3,6 @@
|
||||
|
||||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
||||
import "@hotwired/turbo-rails"
|
||||
import "sample_app/app/assets/javascripts/controllers"
|
||||
// import "sample_app/app/assets/javascripts/controllers"
|
||||
//= require "controllers"
|
||||
//= require bootstrap-sprockets
|
20
app/controllers/sessions_controller.rb
Normal file
20
app/controllers/sessions_controller.rb
Normal file
@ -0,0 +1,20 @@
|
||||
class SessionsController < ApplicationController
|
||||
include SessionsHelper
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find_by(email: params[:session][:email].downcase)
|
||||
if user && user.authenticate(params[:session][:password])
|
||||
reset_session
|
||||
log_in user
|
||||
redirect_to user
|
||||
else
|
||||
flash.now[:danger] = 'Invalid email/password combination'
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
end
|
15
app/helpers/sessions_helper.rb
Normal file
15
app/helpers/sessions_helper.rb
Normal file
@ -0,0 +1,15 @@
|
||||
module SessionsHelper
|
||||
def log_in(user)
|
||||
session[:user_id] = user.id
|
||||
end
|
||||
|
||||
def current_user
|
||||
if session[:user_id]
|
||||
@current_user ||= User.find_by(id: session[:user_id])
|
||||
end
|
||||
end
|
||||
|
||||
def logged_in?
|
||||
!current_user.nil?
|
||||
end
|
||||
end
|
@ -5,7 +5,24 @@
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li><%= link_to "Home", root_url %></li>
|
||||
<li><%= link_to "Help", help_url %></li>
|
||||
<li><%= link_to "Login 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 "Login in", login_url %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
|
18
app/views/sessions/new.html.erb
Normal file
18
app/views/sessions/new.html.erb
Normal file
@ -0,0 +1,18 @@
|
||||
<% provide(:title, "Log in") %>
|
||||
|
||||
<h1>Log in</h1>
|
||||
<div class ="row">
|
||||
<div class="col-md-6 col-md-offset-3">
|
||||
<%= form_with(url: login_path, scope: :session, local: true) 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>
|
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
# get "sessions/new"
|
||||
# get "users/new"
|
||||
# get "static_pages/home"
|
||||
# get "static_pages/help"
|
||||
@ -7,13 +8,17 @@ Rails.application.routes.draw do
|
||||
|
||||
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'
|
||||
get '/help', to: 'static_pages#help'
|
||||
get '/about', to: 'static_pages#about'
|
||||
get '/contact', to: 'static_pages#contact'
|
||||
get '/signup', to: 'users#new'
|
||||
|
||||
get '/login', to: 'sessions#new'
|
||||
post 'login', to: 'sessions#create'
|
||||
delete '/logout', to: 'sessions#destroy'
|
||||
|
||||
resources :users
|
||||
|
||||
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
||||
|
8
test/controllers/sessions_controller_test.rb
Normal file
8
test/controllers/sessions_controller_test.rb
Normal file
@ -0,0 +1,8 @@
|
||||
require "test_helper"
|
||||
|
||||
class SessionsControllerTest < ActionDispatch::IntegrationTest
|
||||
test "should get new" do
|
||||
get login_path
|
||||
assert_response :success
|
||||
end
|
||||
end
|
14
test/integration/users_login_test.rb
Normal file
14
test/integration/users_login_test.rb
Normal file
@ -0,0 +1,14 @@
|
||||
require "test_helper"
|
||||
|
||||
class UsersLoginTest < ActionDispatch::IntegrationTest
|
||||
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
|
||||
end
|
Loading…
Reference in New Issue
Block a user