diff --git a/src/app.rb b/src/app.rb index 99049a2..c52bd60 100755 --- a/src/app.rb +++ b/src/app.rb @@ -10,6 +10,7 @@ require "sassc" require "colorize" require "bcrypt" +require_relative "config.rb" require_relative "debug.rb" require_relative "lib/database.rb" require_relative "func.rb" @@ -27,15 +28,17 @@ get "/style.css" do end get "/" do - slim :index, locals: {info: init_info} + serve :index end get "/login" do - slim :"user/login", locals: {info: init_info} + serve :"user/login" end 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 # API stuff @@ -48,12 +51,13 @@ post "/user" do password = params[:password] password_confirm = params[:password_confirm] - status, info = user.register(email, name, password, password_confirm) - Console::debug "STATUS: #{status}", info + status, ret = user.register(email, name, password, password_confirm) + Console::debug "STATUS: #{status}", ret 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 - redirect "/login", locals: {info: init_info(info)} + redirect "/login" end end diff --git a/src/config.rb b/src/config.rb index 1038c1c..605e293 100644 --- a/src/config.rb +++ b/src/config.rb @@ -5,12 +5,14 @@ MIN_NAME_LEN = 2 EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i REGISTER_ERRORS = { + fields: "Please fill all of the fields", + pass_len: "Password length must be at least #{MIN_PASSWORD_LEN}", pass_notequals: "Password mismatch", name_len: "Name length must be at least #{MIN_NAME_LEN}", - email_dupe: "Email already in use", - email_fake: "Use a real email" + email_dupe: "Email is already in use", + email_fake: "Please use a valid email address" } diff --git a/src/db_models.rb b/src/db_models.rb index 21c6739..62cdc7b 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -16,6 +16,9 @@ class User < Table end private def validate_credentials(email, name, password, password_confirm) + # Field check + check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" + # Check email check_email_dupe = self.find_by_email(email).length <= 0 check_email_valid = email.match(EMAIL_REGEX) != nil @@ -26,30 +29,44 @@ class User < Table # Password check_pass_equals = password == password_confirm 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 # Register a new user # Returns: success?, data # TODO: input checks & ERRORS! def register(email, name, password, password_confirm) - check_email = self.find_by_email(email) - if( check_email.length > 0 ) then - # Email taken - return false, {error_msg: "Email already in use!"} + check, errorstr = self.validate_credentials(email, name, password, password_confirm) + + if( check ) then + pw_hash = BCrypt::Password.create(password) + data = { # payload + name: name, + email: email, + pw_hash: pw_hash + } + + resp = self.insert(data) # insert into the db + return check, resp else - if( password == password_confirm ) then - pw_hash = BCrypt::Password.create(password) - data = { # payload - name: name, - email: email, - pw_hash: pw_hash - } - - resp = self.insert(data) # insert into the db - return true, resp - else - return false, {error_msg: "Password mismatch!"} - end + return check, errorstr end end end diff --git a/src/func.rb b/src/func.rb index 5798ec2..219cfb0 100644 --- a/src/func.rb +++ b/src/func.rb @@ -9,3 +9,7 @@ def init_info(info={}) return info end +def serve(template, info={}) + slim( template, locals: {info: init_info(info)} ) +end + diff --git a/src/views/stylesheets/style.sass b/src/views/stylesheets/style.sass index ddaa64e..9ca6eed 100644 --- a/src/views/stylesheets/style.sass +++ b/src/views/stylesheets/style.sass @@ -5,7 +5,7 @@ $bg_clr: #272a34 $bg_alt_clr: #232730 $fg_clr: #fff $fg_alt_clr: #eee -$fg_error: #f00 +$fg_error: #f77 $shadow_clr: #4b5263 @@ -91,7 +91,7 @@ a.button:hover label.error_msg color: $fg_error - font-size: 1.2rem + font-size: 1rem font-weight: bold .form-container @@ -135,6 +135,5 @@ label.error_msg opacity: .8 color: $special_btn_clr - input:not(:last-child) margin-bottom: 1rem diff --git a/src/views/user/register.slim b/src/views/user/register.slim index df6e6fe..6e1ef53 100644 --- a/src/views/user/register.slim +++ b/src/views/user/register.slim @@ -1,7 +1,7 @@ .form-container form action="/user" method="post" h2 Register Account - label.error_msg = info + label.error_msg = info[:error_msg] input type="text" name="email" placeholder="Email" input type="text" name="name" placeholder="Name" input type="password" name="password" placeholder="Password"