Refactor & form input validation

master
E. Almqvist 3 years ago
parent 3272edd65b
commit e78e37a49b
  1. 8
      src/app.rb
  2. 1
      src/config.rb
  3. 6
      src/const.rb
  4. 2
      src/db_models.rb
  5. BIN
      src/public/avatars/4.png
  6. 6
      src/views/stylesheets/style.sass
  7. 2
      src/views/user/register.slim
  8. 6
      src/views/user/settings.slim

@ -110,8 +110,8 @@ post "/register" do
end
post "/login" do
email = params[:email]
password = params[:password]
email = params[:email].strip
password = params[:password].strip
status, ret = User.login(email, password)
if !status then # ret = error message
@ -130,8 +130,8 @@ end
post "/user/update" do
data = {
name: params["displayname"],
bio_text: params["bio"]
name: params["displayname"].chomp,
bio_text: params["bio"].chomp
}
if params[:image] then

@ -11,6 +11,7 @@ REGISTER_ERRORS = {
pass_notequals: "Password mismatch",
name_len: "Name length must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN}",
name_desc: "May only contain alphabetical characters and must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN} characters long",
email_dupe: "Email is already in use",
email_fake: "Please use a valid email address"

@ -20,12 +20,14 @@ AVATAR_SIZE = 1024 # width & height
MIN_PASSWORD_LEN = 8
MIN_NAME_LEN = 2
MAX_NAME_LEN = 32
MAX_NAME_LEN = 28
MIN_BIO_LEN = 0
MAX_BIO_LEN = 128
EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/
NAME_REGEX_STR = "[a-zA-Z-_0-9 ]{#{MIN_NAME_LEN},#{MAX_NAME_LEN}}"
BIO_REGEX_STR = "{#{MIN_BIO_LEN},#{MAX_BIO_LEN}}"
# Routes that needs auth

@ -58,11 +58,9 @@ class User < EntityModel
return false, SETTINGS_ERRORS[:bio_len] unless data[:bio_text].length.between?(MIN_BIO_LEN, MAX_BIO_LEN)
# Filter unchanged data
p data
data.keys.each do |k|
data.delete(k) if @data[k.to_s] == data[k]
end
p data
User.update(data, "id = ?", @id) unless data.length < 1
return true, nil
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

@ -270,12 +270,12 @@ ul.button-container
#userinfo
margin-bottom: 1.75rem
text-align: center
min-width: 250px
width: 250px
h2
margin: 1rem 0 0 0
padding: 0
text-align: center
font-size: 1.5rem
font-size: 100%
font-weight: bold
border-bottom: none
h3
@ -342,7 +342,7 @@ ul.button-container
color: $red_clr
form
background-color: $bg_alt_clr
border: $border_size solid $border_clr
//border: $border_size solid $border_clr
input[type=text]
padding: 0 .2rem

@ -3,7 +3,7 @@
h2 Register Account
label.error_msg = error_msg
input type="text" name="email" placeholder="Email"
input type="text" name="name" placeholder="Name"
input type="text" placeholder="Name" oninput="this.reportValidity()" title="#{REGISTER_ERRORS[:name_desc]}" maxlength="#{MAX_NAME_LEN}" name="name" pattern="#{NAME_REGEX_STR}"
input type="password" name="password" placeholder="Password"
input type="password" name="password_confirm" placeholder="Confirm Password"
input type="submit" value="Register"

@ -7,9 +7,9 @@
label Change avatar
input type="file" name="image" accept="image"
label Display name (min 2 chars)
input type="text" name="displayname" value="#{session_user.name}"
label Display name
input type="text" oninput="this.reportValidity()" title="May only contain alphabetical characters and must be between #{MIN_NAME_LEN} and #{MAX_NAME_LEN} characters long" maxlength="#{MAX_NAME_LEN}" name="displayname" value="#{session_user.name}" pattern="#{NAME_REGEX_STR}"
label Biography
textarea name="bio" cols="20" rows="5" placeholder="Tell us about yourself!" value="#{session_user.bio_text}" = session_user.bio_text
textarea name="bio" cols="20" rows="5" title="Content length must be between #{MIN_BIO_LEN} and #{MAX_BIO_LEN} characters" pattern="#{BIO_REGEX_STR}" maxlength="#{MAX_BIO_LEN}" placeholder="Tell us about yourself!" value="#{session_user.bio_text}" = session_user.bio_text
input type="submit" value="Update"

Loading…
Cancel
Save