From 6db13f193e4e5c13784d005b0ae50eedb73bae1b Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 10 Feb 2022 13:02:16 +0100 Subject: [PATCH] Config & register progress --- src/app.rb | 1 + src/config.rb | 16 ++++++++++++++++ src/db_models.rb | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/config.rb diff --git a/src/app.rb b/src/app.rb index 2f31c81..99049a2 100755 --- a/src/app.rb +++ b/src/app.rb @@ -49,6 +49,7 @@ post "/user" do password_confirm = params[:password_confirm] status, info = user.register(email, name, password, password_confirm) + Console::debug "STATUS: #{status}", info if !status then # if something went wrong then return to 0 redirect "/register", locals: {info: init_info(info)} else # if everything went right then continue diff --git a/src/config.rb b/src/config.rb new file mode 100644 index 0000000..1038c1c --- /dev/null +++ b/src/config.rb @@ -0,0 +1,16 @@ +# Register stuff +MIN_PASSWORD_LEN = 8 +MIN_NAME_LEN = 2 + +EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i + +REGISTER_ERRORS = { + 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" +} + diff --git a/src/db_models.rb b/src/db_models.rb index 72b5664..21c6739 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -15,6 +15,19 @@ class User < Table resp = self.get("*", "email = ?", email) end + private def validate_credentials(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 + + # Name + check_name_len = name.length >= MIN_NAME_LEN + + # Password + check_pass_equals = password == password_confirm + check_pass_len = password.length >= MIN_PASSWORD_LEN + end + # Register a new user # Returns: success?, data # TODO: input checks & ERRORS! @@ -33,7 +46,7 @@ class User < Table } resp = self.insert(data) # insert into the db - return true, {resp: resp} + return true, resp else return false, {error_msg: "Password mismatch!"} end