|
|
@ -1,5 +1,17 @@ |
|
|
|
# User table model |
|
|
|
# User table model |
|
|
|
class User < Entity |
|
|
|
class User < EntityModel |
|
|
|
|
|
|
|
attr_reader :email, :name, :bio_text, :balance, :avatar_url, :reputation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(user_info) |
|
|
|
|
|
|
|
super user_info |
|
|
|
|
|
|
|
@email = user_info["email"] |
|
|
|
|
|
|
|
@name = user_info["name"] |
|
|
|
|
|
|
|
@bio_text = user_info["bio_text"] |
|
|
|
|
|
|
|
@balance = user_info["balance"] |
|
|
|
|
|
|
|
@avatar_url = user_info["avatar_url"] |
|
|
|
|
|
|
|
@reputation = user_info["reputation"] |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Find user by ID, returns multiple results if multiple IDs exist |
|
|
|
# Find user by ID, returns multiple results if multiple IDs exist |
|
|
|
# (which wont happen since IDs are unique) |
|
|
|
# (which wont happen since IDs are unique) |
|
|
|
def self.find_by_id(id) |
|
|
|
def self.find_by_id(id) |
|
|
@ -37,12 +49,16 @@ class User < Entity |
|
|
|
return true, "" |
|
|
|
return true, "" |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.validate_password(pw_hash, password) |
|
|
|
|
|
|
|
BCrypt::Password.new(pw_hash) == password |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Register a new user |
|
|
|
# Register a new user |
|
|
|
# Returns: success?, data |
|
|
|
# Returns: success?, data |
|
|
|
def self.register(email, name, password, password_confirm) |
|
|
|
def self.register(email, name, password, password_confirm) |
|
|
|
check, errorstr = self.validate_register_creds(email, name, password, password_confirm) |
|
|
|
check, errorstr = self.validate_register_creds(email, name, password, password_confirm) |
|
|
|
|
|
|
|
|
|
|
|
if( check ) then |
|
|
|
if check then |
|
|
|
pw_hash = BCrypt::Password.create(password) |
|
|
|
pw_hash = BCrypt::Password.create(password) |
|
|
|
data = { # payload |
|
|
|
data = { # payload |
|
|
|
name: name, |
|
|
|
name: name, |
|
|
@ -57,13 +73,18 @@ class User < Entity |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Log in user |
|
|
|
# Log in user |
|
|
|
# Returns: success?, auth token |
|
|
|
# Returns: success?, user info |
|
|
|
def self.login(email, password) |
|
|
|
def self.login(email, password) |
|
|
|
user_query = self.find_by_email email # get the user info |
|
|
|
user_query = self.find_by_email email # get the user info |
|
|
|
|
|
|
|
|
|
|
|
if user_query.length >= 1 then |
|
|
|
return false, LOGIN_ERRORS[:fail] unless user_query.length >= 1 # Verify that a user was found |
|
|
|
user_info = user_query.first |
|
|
|
|
|
|
|
end |
|
|
|
user_info = user_query.first |
|
|
|
|
|
|
|
pw_check = self.validate_password user_info["pw_hash"], password |
|
|
|
|
|
|
|
return false, LOGIN_ERRORS[:fail] unless pw_check # Verify password |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true, user_info |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|