User: Fixed register errors

master
E. Almqvist 3 years ago
parent 6db13f193e
commit 8bd577f663
  1. 18
      src/app.rb
  2. 6
      src/config.rb
  3. 35
      src/db_models.rb
  4. 4
      src/func.rb
  5. 5
      src/views/stylesheets/style.sass
  6. 2
      src/views/user/register.slim

@ -10,6 +10,7 @@ require "sassc"
require "colorize" require "colorize"
require "bcrypt" require "bcrypt"
require_relative "config.rb"
require_relative "debug.rb" require_relative "debug.rb"
require_relative "lib/database.rb" require_relative "lib/database.rb"
require_relative "func.rb" require_relative "func.rb"
@ -27,15 +28,17 @@ get "/style.css" do
end end
get "/" do get "/" do
slim :index, locals: {info: init_info} serve :index
end end
get "/login" do get "/login" do
slim :"user/login", locals: {info: init_info} serve :"user/login"
end end
get "/register" do get "/register" do
slim :"user/register", locals: {info: init_info} info = session[:error_msg] != nil ? {error_msg: session[:error_msg]} : {}
session[:error_msg] = nil
serve :"user/register", info
end end
# API stuff # API stuff
@ -48,12 +51,13 @@ post "/user" do
password = params[:password] password = params[:password]
password_confirm = params[:password_confirm] password_confirm = params[:password_confirm]
status, info = user.register(email, name, password, password_confirm) status, ret = user.register(email, name, password, password_confirm)
Console::debug "STATUS: #{status}", info Console::debug "STATUS: #{status}", ret
if !status then # if something went wrong then return to 0 if !status then # if something went wrong then return to 0
redirect "/register", locals: {info: init_info(info)} session[:error_msg] = ret
redirect "/register"
else # if everything went right then continue else # if everything went right then continue
redirect "/login", locals: {info: init_info(info)} redirect "/login"
end end
end end

@ -5,12 +5,14 @@ MIN_NAME_LEN = 2
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/i
REGISTER_ERRORS = { REGISTER_ERRORS = {
fields: "Please fill all of the fields",
pass_len: "Password length must be at least #{MIN_PASSWORD_LEN}", pass_len: "Password length must be at least #{MIN_PASSWORD_LEN}",
pass_notequals: "Password mismatch", pass_notequals: "Password mismatch",
name_len: "Name length must be at least #{MIN_NAME_LEN}", name_len: "Name length must be at least #{MIN_NAME_LEN}",
email_dupe: "Email already in use", email_dupe: "Email is already in use",
email_fake: "Use a real email" email_fake: "Please use a valid email address"
} }

@ -16,6 +16,9 @@ class User < Table
end end
private def validate_credentials(email, name, password, password_confirm) private def validate_credentials(email, name, password, password_confirm)
# Field check
check_all_fields = email != "" && name != "" && password != "" && password_confirm != ""
# Check email # Check email
check_email_dupe = self.find_by_email(email).length <= 0 check_email_dupe = self.find_by_email(email).length <= 0
check_email_valid = email.match(EMAIL_REGEX) != nil check_email_valid = email.match(EMAIL_REGEX) != nil
@ -26,18 +29,33 @@ class User < Table
# Password # Password
check_pass_equals = password == password_confirm check_pass_equals = password == password_confirm
check_pass_len = password.length >= MIN_PASSWORD_LEN check_pass_len = password.length >= MIN_PASSWORD_LEN
# This code is really ugly
# TODO: refactor
if not check_all_fields then
return false, REGISTER_ERRORS[:fields]
elsif not check_email_dupe then
return false, REGISTER_ERRORS[:email_dupe]
elsif not check_email_valid then
return false, REGISTER_ERRORS[:email_fake]
elsif not check_name_len then
return false, REGISTER_ERRORS[:name_len]
elsif not check_pass_equals then
return false, REGISTER_ERRORS[:pass_notequals]
elsif not check_pass_len then
return false, REGISTER_ERRORS[:pass_len]
else
return true, ""
end
end end
# Register a new user # Register a new user
# Returns: success?, data # Returns: success?, data
# TODO: input checks & ERRORS! # TODO: input checks & ERRORS!
def register(email, name, password, password_confirm) def register(email, name, password, password_confirm)
check_email = self.find_by_email(email) check, errorstr = self.validate_credentials(email, name, password, password_confirm)
if( check_email.length > 0 ) then
# Email taken if( check ) then
return false, {error_msg: "Email already in use!"}
else
if( password == password_confirm ) then
pw_hash = BCrypt::Password.create(password) pw_hash = BCrypt::Password.create(password)
data = { # payload data = { # payload
name: name, name: name,
@ -46,10 +64,9 @@ class User < Table
} }
resp = self.insert(data) # insert into the db resp = self.insert(data) # insert into the db
return true, resp return check, resp
else else
return false, {error_msg: "Password mismatch!"} return check, errorstr
end
end end
end end
end end

@ -9,3 +9,7 @@ def init_info(info={})
return info return info
end end
def serve(template, info={})
slim( template, locals: {info: init_info(info)} )
end

@ -5,7 +5,7 @@ $bg_clr: #272a34
$bg_alt_clr: #232730 $bg_alt_clr: #232730
$fg_clr: #fff $fg_clr: #fff
$fg_alt_clr: #eee $fg_alt_clr: #eee
$fg_error: #f00 $fg_error: #f77
$shadow_clr: #4b5263 $shadow_clr: #4b5263
@ -91,7 +91,7 @@ a.button:hover
label.error_msg label.error_msg
color: $fg_error color: $fg_error
font-size: 1.2rem font-size: 1rem
font-weight: bold font-weight: bold
.form-container .form-container
@ -135,6 +135,5 @@ label.error_msg
opacity: .8 opacity: .8
color: $special_btn_clr color: $special_btn_clr
input:not(:last-child) input:not(:last-child)
margin-bottom: 1rem margin-bottom: 1rem

@ -1,7 +1,7 @@
.form-container .form-container
form action="/user" method="post" form action="/user" method="post"
h2 Register Account h2 Register Account
label.error_msg = info label.error_msg = info[:error_msg]
input type="text" name="email" placeholder="Email" input type="text" name="email" placeholder="Email"
input type="text" name="name" placeholder="Name" input type="text" name="name" placeholder="Name"
input type="password" name="password" placeholder="Password" input type="password" name="password" placeholder="Password"

Loading…
Cancel
Save