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

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

@ -3,12 +3,22 @@ def get_random_subtitle
subtitles.sample.chomp
end
def init_info(info={})
def init_info(*infos)
g = Hash.new ""
info = g.merge(info)
info = g.merge(*infos)
return info
end
def user
session[:userid] && User.find_by_id(session[:userid])
end
# Serve templates
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

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

@ -12,7 +12,7 @@ html lang="en"
h2 = get_random_subtitle
nav
ul
- unless session[:user]
- unless user
li
a.button#register href="/register" target="_self"
| Register
@ -22,7 +22,7 @@ html lang="en"
- else
li
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
==yield

Loading…
Cancel
Save