|
|
@ -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,30 +29,44 @@ 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!"} |
|
|
|
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 |
|
|
|
else |
|
|
|
if( password == password_confirm ) then |
|
|
|
return check, errorstr |
|
|
|
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 |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|