Skip to content

Commit d7374fd

Browse files
committed
Add "session" layout
As per Sean's suggested changes, I have removed the "connexion" and "reset_password" routes to use the default Rails conventions. I have also fixed indendation and added some tests for login and password reset.
1 parent 0251abc commit d7374fd

File tree

9 files changed

+195
-109
lines changed

9 files changed

+195
-109
lines changed

app/controllers/passwords_controller.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class PasswordsController < ApplicationController
22
allow_unauthenticated_access
33
before_action :set_user_by_token, only: %i[ edit update ]
4-
layout "mazer"
4+
layout "session"
5+
56
def new
67
end
78

89
def create
910
if user = User.find_by(email: params[:email])
1011
PasswordsMailer.reset(user).deliver_later
1112
end
12-
1313
redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
1414
end
1515

@@ -25,9 +25,10 @@ def update
2525
end
2626

2727
private
28-
def set_user_by_token
29-
@user = User.find_by_password_reset_token!(params[:token])
30-
rescue ActiveSupport::MessageVerifier::InvalidSignature
31-
redirect_to new_password_path, alert: "Password reset link is invalid or has expired."
32-
end
28+
29+
def set_user_by_token
30+
@user = User.find_by_password_reset_token!(params[:token])
31+
rescue ActiveSupport::MessageVerifier::InvalidSignature
32+
redirect_to new_password_path, alert: "Password reset link is invalid or has expired."
33+
end
3334
end
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class SessionsController < ApplicationController
22
allow_unauthenticated_access only: %i[ new create ]
33
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
4-
layout "mazer"
4+
layout "session"
5+
56
def new
67
end
78

@@ -10,12 +11,12 @@ def create
1011
start_new_session_for user
1112
redirect_to after_authentication_url
1213
else
13-
redirect_to connexion_path, alert: "Try another email address or password."
14+
redirect_to new_session_path, alert: "Try another email address or password."
1415
end
1516
end
1617

1718
def destroy
1819
terminate_session
19-
redirect_to connexion_path
20+
redirect_to new_session_path
2021
end
2122
end

app/views/layouts/mazer.html.erb

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,47 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title><%= content_for(:title) || "Skillrx" %></title>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title><%= content_for(:title) || "Skillrx" %></title>
77

8-
<meta name="viewport" content="width=device-width,initial-scale=1">
9-
<meta name="apple-mobile-web-app-capable" content="yes">
10-
<meta name="mobile-web-app-capable" content="yes">
11-
<%= csrf_meta_tags %>
12-
<%= csp_meta_tag %>
8+
<meta name="viewport" content="width=device-width,initial-scale=1">
9+
<meta name="apple-mobile-web-app-capable" content="yes">
10+
<meta name="mobile-web-app-capable" content="yes">
11+
<%= csrf_meta_tags %>
12+
<%= csp_meta_tag %>
1313

14-
<%= yield :head %>
14+
<%= yield :head %>
1515

16-
<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
17-
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>
16+
<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
17+
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>
1818

1919

20-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app.css">
21-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app-dark.css">
22-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/iconly.css">
20+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app.css">
21+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app-dark.css">
22+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/iconly.css">
2323

24-
<%# Includes all stylesheet files in app/assets/stylesheets %>
25-
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
26-
<%= javascript_importmap_tags %>
24+
<%# Includes all stylesheet files in app/assets/stylesheets %>
25+
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
26+
<%= javascript_importmap_tags %>
2727
</head>
2828

2929
<body>
30-
<!-- Only for connexion and reset password not display sidebar -->
31-
<% unless current_page?('/connexion') || current_page?('/reset_password') %>
32-
<%= render "layouts/sidebar" %>
30+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/initTheme.js"></script>
3331

34-
<% end %>
35-
<%= yield %>
32+
<!-- Start content here -->
33+
<%= render "layouts/sidebar" %>
3634

37-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/initTheme.js"></script>
38-
<!-- Start content here -->
35+
<%= yield %>
36+
<!-- End content -->
3937

40-
<%#= render "layouts/sidebar" %>
38+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/components/dark.js"></script>
39+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/perfect-scrollbar/perfect-scrollbar.min.js"></script>
4140

42-
<!-- End content -->
43-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/components/dark.js"></script>
44-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/perfect-scrollbar/perfect-scrollbar.min.js"></script>
41+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/js/app.js"></script>
4542

46-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/js/app.js"></script>
47-
48-
<!-- Need: Apexcharts -->
49-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/apexcharts/apexcharts.min.js"></script>
50-
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/pages/dashboard.js"></script>
43+
<!-- Need: Apexcharts -->
44+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/apexcharts/apexcharts.min.js"></script>
45+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/pages/dashboard.js"></script>
5146
</body>
5247
</html>

app/views/layouts/session.html.erb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title><%= content_for(:title) || "Skillrx" %></title>
7+
8+
<meta name="viewport" content="width=device-width,initial-scale=1">
9+
<meta name="apple-mobile-web-app-capable" content="yes">
10+
<meta name="mobile-web-app-capable" content="yes">
11+
<%= csrf_meta_tags %>
12+
<%= csp_meta_tag %>
13+
14+
<%= yield :head %>
15+
16+
<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
17+
<%#= tag.link rel: "manifest", href: pwa_manifest_path(format: :json) %>
18+
19+
20+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app.css">
21+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/app-dark.css">
22+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/css/iconly.css">
23+
24+
<%# Includes all stylesheet files in app/assets/stylesheets %>
25+
<%= stylesheet_link_tag :app, "data-turbo-track": "reload" %>
26+
<%= javascript_importmap_tags %>
27+
</head>
28+
29+
<body>
30+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/initTheme.js"></script>
31+
32+
<!-- Start content here -->
33+
<%= yield %>
34+
<!-- End content -->
35+
36+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/components/dark.js"></script>
37+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/perfect-scrollbar/perfect-scrollbar.min.js"></script>
38+
39+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/compiled/js/app.js"></script>
40+
41+
<!-- Need: Apexcharts -->
42+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/extensions/apexcharts/apexcharts.min.js"></script>
43+
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/pages/dashboard.js"></script>
44+
</body>
45+
</html>

app/views/passwords/new.html.erb

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
<%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
22

3-
<div class="row h-100">
4-
<div class="col-lg-5 col-12">
5-
<div id="auth-left">
6-
<div class="auth-logo">
7-
8-
</div>
9-
<h1 class="auth-title">Forgot Password</h1>
10-
<p class="auth-subtitle mb-5">Input your email and we will send you reset password link.</p>
3+
<div class="row h-100 p-4">
4+
<div class="col-lg-5 col-12">
5+
<div id="auth-left">
6+
<div class="auth-logo">
117

12-
<%= form_with url: passwords_path do |form| %>
13-
<div class="form-group position-relative has-icon-left mb-4">
14-
<%= form.email_field :email, required: true, class:'form-control form-control-xl', autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email] %><br>
8+
</div>
9+
<h1 class="auth-title">Forgot Password</h1>
10+
<p class="auth-subtitle mb-5">Input your email and we will send you reset password link.</p>
1511

16-
<div class="form-control-icon">
17-
<i class="bi bi-envelope"></i>
18-
</div>
19-
</div>
20-
21-
<%= form.submit "Email reset instructions", class:'btn btn-primary btn-block btn-lg shadow-lg mt-5'%>
22-
<%end%>
23-
</form>
24-
<div class="text-center mt-5 text-lg fs-4">
25-
<p class='text-gray-600'>Remember your account? <%= link_to "Login",connexion_path, class:'font-bold'%>
26-
</p>
27-
</div>
12+
<%= form_with url: passwords_path do |form| %>
13+
<div class="form-group position-relative has-icon-left mb-4">
14+
<%= form.email_field :email, required: true, class:"form-control form-control-xl", autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email] %><br>
15+
<div class="form-control-icon">
16+
<i class="bi bi-envelope"></i>
17+
</div>
2818
</div>
29-
</div>
30-
<div class="col-lg-7 d-none d-lg-block">
31-
<div id="auth-right">
3219

33-
</div>
20+
<%= form.submit "Email reset instructions", class:"btn btn-primary btn-block btn-lg shadow-lg mt-5" %>
21+
<% end %>
22+
23+
<div class="text-center mt-5 text-lg fs-4">
24+
<p class="text-gray-600">Remember your account? <%= link_to "Login", new_session_path, class:"font-bold" %></p>
25+
</div>
26+
</div>
27+
</div>
28+
<div class="col-lg-7 d-none d-lg-block">
29+
<div id="auth-right">
3430
</div>
35-
</div>
31+
</div>
32+
</div>

app/views/sessions/new.html.erb

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
<%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
2-
<%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
3-
<div class="row h-100 px-3">
4-
<div class="col-lg-5 col-12">
5-
<div id="auth-left">
6-
<div class="auth-logo">
2+
<%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
3+
<div class="row h-100 p-4">
4+
<div class="col-lg-5 col-12">
5+
<div id="auth-left">
6+
<div class="auth-logo">
7+
</div>
8+
<h1 class="auth-title">Log in</h1>
9+
<p class="auth-subtitle mb-5">Log in with your data that you entered during registration.</p>
10+
<%= form_with url: session_path do |form| %>
11+
<div class="form-group position-relative has-icon-left mb-4">
12+
<%= form.email_field :email, required: true, class:"form-control form-control-xl", autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email] %><br>
13+
<div class="form-control-icon">
14+
<i class="bi bi-person"></i>
715
</div>
8-
<h1 class="auth-title">Log in.</h1>
9-
<p class="auth-subtitle mb-5">Log in with your data that you entered during registration.</p>
10-
<%= form_with url: session_path do |form| %>
11-
<div class="form-group position-relative has-icon-left mb-4">
12-
<%= form.email_field :email, required: true, class:'form-control form-control-xl', autofocus: true, autocomplete: "username", placeholder: "Enter your email address", value: params[:email] %><br>
13-
<div class="form-control-icon">
14-
<i class="bi bi-person"></i>
15-
</div>
16-
</div>
17-
<div class="form-group position-relative has-icon-left mb-4">
18-
<%= form.password_field :password, required: true, class:'form-control form-control-xl', autocomplete: "current-password", placeholder: "Enter your password", maxlength: 72 %><br>
19-
<div class="form-control-icon">
20-
<i class="bi bi-shield-lock"></i>
21-
</div>
22-
</div>
23-
<%= form.submit "Sign in", class:'btn btn-primary btn-block btn-lg shadow-lg mt-5'%>
24-
<% end %>
25-
<div class="text-center mt-5 text-lg fs-4">
26-
<p><%= link_to "Forgot password?",reset_password_path, class:'font-bold'%></p>
27-
</div>
16+
</div>
17+
<div class="form-group position-relative has-icon-left mb-4">
18+
<%= form.password_field :password, required: true, class:"form-control form-control-xl", autocomplete: "current-password", placeholder: "Enter your password", maxlength: 72 %><br>
19+
<div class="form-control-icon">
20+
<i class="bi bi-shield-lock"></i>
2821
</div>
22+
</div>
23+
<%= form.submit "Sign in", class:"btn btn-primary btn-block btn-lg shadow-lg mt-5" %>
24+
<% end %>
25+
<div class="text-center mt-5 text-lg fs-4">
26+
<p><%= link_to "Forgot password?", new_password_path, class:"font-bold" %></p>
2927
</div>
30-
</div>
28+
</div>
29+
</div>

config/routes.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
Rails.application.routes.draw do
2-
resources :providers
3-
resources :regions
42
root "home#index"
53
get "home/index", as: :home
6-
resource :session, except: [ :new ]
7-
resources :passwords, except: [ :new ], param: :token
8-
resource :registration, only: %i[new create]
9-
resources :regions
10-
resources :providers
114
resources :languages, only: %i[index show new create edit update]
5+
resources :passwords, param: :token
6+
resources :providers
7+
resources :regions
8+
resource :registration, only: %i[new create]
9+
resource :session
10+
resources :users
1211

1312
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
1413
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
1514
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
16-
get "home/index", as: :home
1715
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
18-
get "connexion", to: "sessions#new"
19-
get "reset_password", to: "passwords#new"
2016
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
2117
# Can be used by load balancers and uptime monitors to verify that the app is live.
2218
get "up" => "rails/health#show", as: :rails_health_check
2319

2420
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
2521
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
2622
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
27-
resources :users
2823
# Defines the root path route ("/")
29-
root "home#index"
3024
end

spec/support/capybara.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "capybara/rails"
2+
require "capybara/rspec"
3+
require "selenium/webdriver"
4+
5+
Capybara.register_driver :selenium do |app|
6+
Capybara::Selenium::Driver.new(app, browser: :chrome)
7+
end
8+
9+
RSpec.configure do |config|
10+
config.before(:each, type: :system) do
11+
driven_by :rack_test
12+
end
13+
14+
config.before(:each, :debug, type: :system) do
15+
driven_by :selenium_chrome
16+
end
17+
end

spec/system/login_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "User Login", type: :system do
4+
let!(:user) { create(:user) }
5+
6+
context "with correct email and password" do
7+
it "logs in the user" do
8+
visit new_session_path
9+
fill_in "email", with: user.email
10+
fill_in "password", with: user.password
11+
click_button("Sign in")
12+
13+
# will probably need to be amended once we have a page for non-Admin users
14+
expect(page).to have_text("Administration")
15+
end
16+
end
17+
18+
context "with incorrect email or password" do
19+
it "shows error message" do
20+
visit new_session_path
21+
fill_in "email", with: "[email protected]"
22+
fill_in "password", with: user.password
23+
click_button("Sign in")
24+
25+
expect(page).to have_text("Try another email address or password.")
26+
end
27+
end
28+
29+
context "forgotten password" do
30+
it "allows users to reset password" do
31+
visit new_session_path
32+
click_on("Forgot password?")
33+
expect(page).to have_text("Input your email and we will send you reset password link")
34+
expect(page).to have_link("Login", href: new_session_path)
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)