From 8baa51f6119194a215b63395dbda54fd278e5c5b Mon Sep 17 00:00:00 2001 From: songtianlun Date: Wed, 1 Jan 2025 15:44:50 +0800 Subject: [PATCH] feat(sample): add session and header --- app/assets/javascripts/application.js | 3 ++- app/controllers/sessions_controller.rb | 20 ++++++++++++++++++++ app/helpers/sessions_helper.rb | 15 +++++++++++++++ app/views/layouts/_header.html.erb | 19 ++++++++++++++++++- app/views/sessions/new.html.erb | 18 ++++++++++++++++++ config/routes.rb | 15 ++++++++++----- test/controllers/sessions_controller_test.rb | 8 ++++++++ test/integration/users_login_test.rb | 14 ++++++++++++++ 8 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/views/sessions/new.html.erb create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/integration/users_login_test.rb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fbecce2..a2a7003 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -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 \ No newline at end of file diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..dbdb886 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..f81bc8e --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -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 diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 8352757..aa77b92 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -5,7 +5,24 @@ diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb new file mode 100644 index 0000000..29df465 --- /dev/null +++ b/app/views/sessions/new.html.erb @@ -0,0 +1,18 @@ +<% provide(:title, "Log in") %> + +

Log in

+
+
+ <%= 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 %> + +

New user? <%= link_to "SIgn up now!", signup_path %>

+
+
diff --git a/config/routes.rb b/config/routes.rb index c8ac7c6..d22b657 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..811a08d --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,8 @@ +require "test_helper" + +class SessionsControllerTest < ActionDispatch::IntegrationTest + test "should get new" do + get login_path + assert_response :success + end +end diff --git a/test/integration/users_login_test.rb b/test/integration/users_login_test.rb new file mode 100644 index 0000000..8df72c1 --- /dev/null +++ b/test/integration/users_login_test.rb @@ -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