Skip to content

Commit 2d0b82c

Browse files
Merge pull request #16 from calebowens/finish-login-and-register-controllers
Fix loging in and registering
2 parents 04f54b0 + 4bda9d4 commit 2d0b82c

File tree

6 files changed

+37
-38
lines changed

6 files changed

+37
-38
lines changed

app/common/has_errors.rb

-8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,4 @@ def template
1818
def errors_for(attribute)
1919
Errors.new(messages: errors.full_messages_for(attribute))
2020
end
21-
22-
def validations_passed?
23-
errors.blank?
24-
end
25-
26-
def validations_failed?
27-
errors.any?
28-
end
2921
end

app/features/authentication/pages/login_controller.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ class FormObject
1212
validates_presence_of :email
1313
validates_presence_of :password
1414

15+
validate :ensure_user_exists # Must run last for security
16+
1517
def user
1618
@user ||= User.from_login_details(email:, password:)
1719
end
1820

1921
def ensure_user_exists
22+
return unless errors.blank?
23+
2024
if user.nil?
21-
errors.add :email, :no_user, message: "The email and password provided did not match our records"
25+
errors.add :email, :no_user, message: "and password provided did not match our records"
2226
end
2327
end
2428
end
@@ -58,11 +62,7 @@ def view
5862
def submit
5963
ViewContext.form_object = FormObject.new(user_params)
6064

61-
if ViewContext.form_object.validations_passed?
62-
ViewContext.form_object.ensure_user_exists
63-
end
64-
65-
if ViewContext.form_object.validations_passed?
65+
if ViewContext.form_object.valid?
6666
ViewContext.form_object.user.add_to_session(session)
6767

6868
redirect_to dashboard_home_path, status: :see_other

app/features/authentication/pages/register_controller.rb

+29-15
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,50 @@ class FormObject
99

1010
attr_accessor :email, :password, :confirm_password
1111

12-
validates :email, presence: true
12+
validates :email, presence: true, email_format: { message: "formatted incorrectly" }
1313
validates :password, presence: true, length: {minimum: 8, maximum: 64}
14-
validates :confirm_password, presence: true, comparison: {equal_to: -> { password }}
14+
validates :confirm_password, presence: true
15+
validate :ensure_confirm_password_equals_password
16+
17+
validate :ensure_no_user_with_details # Must run last for security
18+
19+
def ensure_no_user_with_details
20+
return unless errors.blank?
21+
22+
if User.exists_with_email?(email)
23+
errors.add :email, :email_used, message: "is already taken"
24+
end
25+
end
26+
27+
def ensure_confirm_password_equals_password
28+
if confirm_password != password
29+
errors.add :confirm_password, :not_password, message: "must match password"
30+
end
31+
end
1532
end
1633

17-
class View
34+
class View < ApplicationView
1835
def template
1936
h1 { "Create a user" }
2037

2138
render Form
2239
end
2340
end
2441

25-
class Form
42+
class Form < ApplicationView
2643
def template
27-
form_with(url: authentication_register_path, id: "register") do |f|
44+
form_with(model: ViewContext.form_object, url: authentication_register_path, id: "register") do |f|
2845
f.label :email, "Email"
29-
f.text_field :email, value: @submission[:email]
30-
render @submission.errors_for(:email)
46+
f.text_field :email
47+
render ViewContext.form_object.errors_for(:email)
3148

3249
f.label :password, "Password"
3350
f.password_field :password
34-
render @submission.errors_for(:password)
51+
render ViewContext.form_object.errors_for(:password)
3552

3653
f.label :confirm_password, "Confirm Password"
3754
f.password_field :confirm_password
38-
render @submission.errors_for(:confirm_password)
55+
render ViewContext.form_object.errors_for(:confirm_password)
3956

4057
f.submit "Register"
4158
end
@@ -53,11 +70,8 @@ def view
5370
def submit
5471
ViewContext.form_object = FormObject.new(user_params)
5572

56-
if ViewContext.form_object.validations_passed?
57-
end
58-
59-
if ViewContext.form_object.validations_passed?
60-
user = User.create!(email: submission[:email], password: submission[:password])
73+
if ViewContext.form_object.valid?
74+
user = User.create!(email: ViewContext.form_object.password, password: ViewContext.form_object.password)
6175

6276
user.add_to_session session
6377

@@ -72,7 +86,7 @@ def submit
7286
end
7387

7488
def user_params
75-
params.permit(
89+
params.require(:authentication_pages_register_controller_form_object).permit(
7690
:email,
7791
:password,
7892
:confirm_password

app/javascript/application.js

-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
import "@hotwired/turbo-rails"
33
import Alpine from "alpinejs"
44
import * as Routes from "routes"
5-
import tippy from 'tippy.js'
6-
import "./othertest.js"
7-
8-
tippy('#tippy', {
9-
content: 'My tooltip!'
10-
});
115

126
Alpine.data("notificationController", (initialNotices = []) => ({
137
notices: [],

app/javascript/othertest.js

-1
This file was deleted.

config/routes.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
get "/login", to: "login#view"
1919
post "/login", to: "login#submit"
2020

21-
get "/register", to: "register#handle"
22-
post "/register", to: "register_submit#handle"
21+
get "/register", to: "register#view"
22+
post "/register", to: "register#submit"
2323
delete "/logout", to: "logout#handle"
2424
end
2525
end

0 commit comments

Comments
 (0)