User: Login!

master
E. Almqvist 3 years ago
parent 49caae9154
commit 74de922987
  1. 20
      src/app.rb
  2. 31
      src/db_models.rb
  3. 2
      src/debug.rb
  4. 9
      src/lib/database.rb
  5. 7
      src/views/layout.slim
  6. 10
      src/views/stylesheets/style.sass
  7. 2
      src/views/user/login.slim
  8. 2
      src/views/user/register.slim

@ -31,6 +31,8 @@ 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
@ -41,14 +43,14 @@ get "/register" do
end end
# API stuff # API stuff
post "/user" do post "/register" do
email = params[:email] email = params[:email]
name = params[:name] name = params[:name]
password = params[:password] password = params[:password]
password_confirm = params[:password_confirm] password_confirm = params[:password_confirm]
status, ret = User.register(email, name, password, password_confirm) status, ret = User.register(email, name, password, password_confirm)
Console::debug "STATUS: #{status}", ret Console.debug "/register STATUS: #{status}", ret
if !status then # if something went wrong then return to 0 if !status then # if something went wrong then return to 0
session[:error_msg] = ret session[:error_msg] = ret
redirect "/register" redirect "/register"
@ -57,8 +59,18 @@ post "/user" do
end end
end end
post "/user/login" do post "/login" do
# login user email = params[:email]
password = params[:password]
status, ret = User.login(email, password)
Console.debug "/login STATUS: #{status}", ret
if !status then
session[:error_msg] = ret
redirect "/login"
else
session[:user] = User.new(ret)
redirect "/" redirect "/"
end
end end

@ -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 user_info = user_query.first
end 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

@ -1,4 +1,4 @@
module Console class Console
def self.log(str, indent=4, *args) def self.log(str, indent=4, *args)
puts "#{str}" puts "#{str}"

@ -4,9 +4,12 @@ def db
dbbuf dbbuf
end end
class Entity class EntityModel
attr_reader :name, :path attr_accessor :data
attr_accessor :tables
def initialize(data)
@data = data
end
# Creates the table # Creates the table
def self.init_table def self.init_table

@ -12,7 +12,7 @@ html lang="en"
h2 = get_random_subtitle h2 = get_random_subtitle
nav nav
ul ul
- unless session[:auth_token] - unless session[:user]
li li
a.button#register href="/register" target="_self" a.button#register href="/register" target="_self"
| Register | Register
@ -21,8 +21,9 @@ html lang="en"
| Log in | Log in
- else - else
li li
a.button#profile href="/profile" target="_self" label = session[:user].name
| Profile a href="/profile" target="_self"
img.avatar src=session[:user].avatar_url alt="Your avatar"
.content-container .content-container
==yield ==yield

@ -102,6 +102,16 @@ label.error_msg
font-size: 1rem font-size: 1rem
font-weight: bold font-weight: bold
img.avatar
width: 3.4rem
height: 3.4rem
border-radius: 50%
border: 2px solid $shadow_clr
transition: border .2s
img.avatar:hover
border: 2px solid $special_btn_clr
.form-container .form-container
display: flex display: flex
justify-content: center justify-content: center

@ -1,5 +1,5 @@
.form-container .form-container
form action="/user/login" method="post" form action="/login" method="post"
h2 Log in h2 Log in
label.error_msg = info[:error_msg] label.error_msg = info[:error_msg]
input type="text" name="email" placeholder="Email" input type="text" name="email" placeholder="Email"

@ -1,5 +1,5 @@
.form-container .form-container
form action="/user" method="post" form action="/register" method="post"
h2 Register Account h2 Register Account
label.error_msg = info[:error_msg] label.error_msg = info[:error_msg]
input type="text" name="email" placeholder="Email" input type="text" name="email" placeholder="Email"

Loading…
Cancel
Save