User: session refactor

master
E. Almqvist 3 years ago
parent 30ac747373
commit 25bc1e9201
  1. 12
      src/app.rb
  2. 34
      src/db_models.rb
  3. 16
      src/func.rb
  4. 3
      src/lib/database.rb
  5. 4
      src/views/layout.slim

@ -32,15 +32,11 @@ get "/" do
end end
get "/login" do get "/login" do
info = session[:error_msg] != nil ? {error_msg: session[:error_msg]} : {}
session[:error_msg] = nil
serve :"user/login" serve :"user/login"
end end
get "/register" do get "/register" do
info = session[:error_msg] != nil ? {error_msg: session[:error_msg]} : {} serve :"user/register"
session[:error_msg] = nil
serve :"user/register", info
end end
# API stuff # API stuff
@ -66,11 +62,11 @@ post "/login" do
status, ret = User.login(email, password) status, ret = User.login(email, password)
Console.debug "/login STATUS: #{status}", ret Console.debug "/login STATUS: #{status}", ret
if !status then if !status then # ret = error message
session[:error_msg] = ret session[:error_msg] = ret
redirect "/login" redirect "/login"
else else # ret = userid
session[:user] = User.new ret session[:userid] = ret
redirect "/" redirect "/"
end end
end end

@ -1,6 +1,6 @@
# User table model # User table model
class User < EntityModel class User < EntityModel
attr_reader :email, :name, :bio_text, :balance, :avatar_url, :reputation attr_reader :email, :name, :bio_text, :balance, :avatar_url, :reputation, :pw_hash
def initialize(user_info) def initialize(user_info)
super user_info super user_info
@ -10,10 +10,11 @@ class User < EntityModel
@balance = user_info["balance"] @balance = user_info["balance"]
@avatar_url = user_info["avatar_url"] @avatar_url = user_info["avatar_url"]
@reputation = user_info["reputation"] @reputation = user_info["reputation"]
@pw_hash = user_info["pw_hash"]
end end
def self.get_avatar def avatar
gravatar = Gravatar.src @email gravatar = nil # Gravatar.src @email
if gravatar then if gravatar then
return gravatar return gravatar
else else
@ -21,16 +22,16 @@ class User < EntityModel
end end
end end
# Find user by ID, returns multiple results if multiple IDs exist # Find user by ID, returns a user object
# (which wont happen since IDs are unique)
def self.find_by_id(id) def self.find_by_id(id)
self.get("*", "id = ?", id) data = self.get("*", "id = ?", id).first
data && User.new(data)
end end
# Find user by email, same as above but for emails. # Find user by email, same as above but for emails.
# Also unique
def self.find_by_email(email) def self.find_by_email(email)
self.get("*", "email = ?", email) data = self.get("*", "email = ?", email).first
data && User.new(data)
end end
def self.validate_register_creds(email, name, password, password_confirm) def self.validate_register_creds(email, name, password, password_confirm)
@ -38,7 +39,7 @@ class User < EntityModel
check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" 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)
check_email_valid = email.match(EMAIL_REGEX) != nil check_email_valid = email.match(EMAIL_REGEX) != nil
# Name # Name
@ -68,14 +69,14 @@ class User < EntityModel
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,
email: email, email: email,
pw_hash: pw_hash pw_hash: pw_hash
} }
resp = self.insert(data) # insert into the db resp = self.insert data # insert into the db
return check, resp return check, resp
else else
return check, errorstr return check, errorstr
@ -84,16 +85,15 @@ class User < EntityModel
# Log in user # Log in user
# Returns: success?, user info # Returns: success?, user id
def self.login(email, password) def self.login(email, password)
user_query = self.find_by_email email # get the user info user = self.find_by_email email # get the user info
return false, LOGIN_ERRORS[:fail] unless user_query.length >= 1 # Verify that a user was found return false, LOGIN_ERRORS[:fail] unless user # Verify that the user exists
user_info = user_query.first pw_check = self.validate_password(user.pw_hash, password)
pw_check = self.validate_password user_info["pw_hash"], password
return false, LOGIN_ERRORS[:fail] unless pw_check # Verify password return false, LOGIN_ERRORS[:fail] unless pw_check # Verify password
return true, user_info return true, user.id
end end
end end

@ -3,12 +3,22 @@ def get_random_subtitle
subtitles.sample.chomp subtitles.sample.chomp
end end
def init_info(info={}) def init_info(*infos)
g = Hash.new "" g = Hash.new ""
info = g.merge(info) info = g.merge(*infos)
return info return info
end end
def user
session[:userid] && User.find_by_id(session[:userid])
end
# Serve templates
def serve(template, info={}) def serve(template, info={})
slim( template, locals: {info: init_info(info)} ) # Insert the error info (if it exists)
error_info = session[:error_msg] != nil ? {error_msg: session[:error_msg]} : {}
session[:error_msg] = nil
# Serve the slim template
slim(template, locals: {info: init_info(info, error_info), user: user})
end end

@ -5,9 +5,10 @@ def db
end end
class EntityModel class EntityModel
attr_accessor :data attr_reader :id, :data
def initialize(data) def initialize(data)
@id = data["id"]
@data = data @data = data
end end

@ -12,7 +12,7 @@ html lang="en"
h2 = get_random_subtitle h2 = get_random_subtitle
nav nav
ul ul
- unless session[:user] - unless user
li li
a.button#register href="/register" target="_self" a.button#register href="/register" target="_self"
| Register | Register
@ -22,7 +22,7 @@ html lang="en"
- else - else
li li
a href="/profile" target="_self" a href="/profile" target="_self"
img.avatar src="#{session[:user].get_avatar}" alt="Your avatar" img.avatar src="#{user.avatar}" alt="Your avatar"
.content-container .content-container
==yield ==yield

Loading…
Cancel
Save