DB Models: User register

master
E. Almqvist 3 years ago
parent 9d6376b6b2
commit 556c8e62fa
  1. 1
      src/Gemfile
  2. 2
      src/Gemfile.lock
  3. 16
      src/app.rb
  4. 39
      src/db_models.rb
  5. 20
      src/lib/database.rb
  6. 2
      src/sql/tables/User.sql
  7. 2
      src/views/user/login.slim

@ -12,3 +12,4 @@ gem "sqlite3"
gem "slim" gem "slim"
gem "sassc" gem "sassc"
gem "colorize" gem "colorize"
gem "bcrypt"

@ -1,6 +1,7 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
bcrypt (3.1.16)
colorize (0.8.1) colorize (0.8.1)
ffi (1.15.5) ffi (1.15.5)
multi_json (1.15.0) multi_json (1.15.0)
@ -36,6 +37,7 @@ PLATFORMS
x86_64-linux x86_64-linux
DEPENDENCIES DEPENDENCIES
bcrypt
colorize colorize
sassc sassc
sinatra sinatra

@ -8,11 +8,14 @@ require "slim"
require "sqlite3" require "sqlite3"
require "sassc" require "sassc"
require "colorize" require "colorize"
require "bcrypt"
require_relative "debug.rb" require_relative "debug.rb"
require_relative "lib/database.rb" require_relative "lib/database.rb"
require_relative "func.rb" require_relative "func.rb"
require_relative "db_models.rb"
load_tables = [ load_tables = [
"User", "User",
"Role" "Role"
@ -21,20 +24,27 @@ db = Database.new("main", load_tables)
enable :sessions enable :sessions
def init_params(params={})
g = Hash.new ""
g.merge(params)
end
# Routes
get "/style.css" do get "/style.css" do
sass :"stylesheets/style", style: :compressed sass :"stylesheets/style", style: :compressed
end end
get "/" do get "/" do
slim :index slim :index, locals: {params: init_params}
end end
get "/login" do get "/login" do
slim :"user/login" slim :"user/login", locals: {params: init_params}
end end
get "/register" do get "/register" do
slim :"user/register" slim :"user/register", locals: {params: init_params}
end end
# API stuff # API stuff

@ -0,0 +1,39 @@
class User < Table
def initialize(db)
super db, "User"
end
# Find user by ID, returns multiple results if multiple IDs exist
# (which wont happen since IDs are unique)
def find_by_id(id)
self.get("*", "id = #{id}")
end
# Find user by email, same as above but for emails.
# Also unique
def find_by_email(email)
self.get("*", "email = #{email}")
end
# Register a new user
# Returns: success?, data
def register(email, name, password, password_confirm)
if( self.find_by_email(email).length > 0 ) then
# Email taken
return false, "Email already in use!"
else
if( password == password_confirm ) then
pw_hash = BCrypt::Password.create(password)
data = { # payload
name: name,
email: email,
pw_hash: pw_hash
}
self.insert(@name, data) # insert into the db
else
return false, "Password mismatch!"
end
end
end
end

@ -4,10 +4,10 @@ class Table
attr_reader :name attr_reader :name
attr_accessor :db attr_accessor :db
def initialize(db, name, sql_file) def initialize(db, name)
@db = db @db = db
@name = name @name = name
@sql_file = sql_file @sql_file = "sql/tables/#{name}.sql"
end end
def create_table def create_table
@ -19,20 +19,24 @@ class Table
end end
end end
def get(attr, filter="") # these methods are private because they
# are intended to be accessed through a
# "Table Model".
# See "db_models.rb"
private def get(attr, filter="")
@db.get(@name, attr, filter) @db.get(@name, attr, filter)
end end
def insert(data, filter="") private def insert(data, filter="")
@db.insert(@name, data, filter) @db.insert(@name, data, filter)
end end
def update(data, filter="") private def update(data, filter="")
@db.update(@name, data, filter) @db.update(@name, data, filter)
end end
# sets or updates a specific field in the table # sets or updates a specific field in the table
def set(attr, data, filter="") # slower but more lazy private def set(attr, data, filter="") # slower but more lazy
if @db.get(@name, attr, filter).length > 0 then if @db.get(@name, attr, filter).length > 0 then
@db.update(@name, data, filter) @db.update(@name, data, filter)
else else
@ -51,7 +55,7 @@ class Database # Database class
@tables = [] @tables = []
# generate table objects # generate table objects
tables_names.each do |name| tables_names.each do |name|
tbl = Table.new(self, name, "sql/tables/#{name}.sql") tbl = Table.new(self, name)
@tables << tbl @tables << tbl
end end
end end
@ -96,7 +100,7 @@ class Database # Database class
self.query(q, *data.values ) self.query(q, *data.values )
end end
def insert(table, data, filter="") # Inserts new data into the table def insert(table, data) # Inserts new data into the table
entstr, valstr = gen_insert_query data.keys entstr, valstr = gen_insert_query data.keys
self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values ) self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values )
end end

@ -3,7 +3,7 @@ CREATE TABLE "User" (
"email" TEXT NOT NULL UNIQUE, "email" TEXT NOT NULL UNIQUE,
"pw_hash" TEXT NOT NULL UNIQUE, "pw_hash" TEXT NOT NULL UNIQUE,
"name" TEXT NOT NULL DEFAULT 'Unknown', "name" TEXT NOT NULL DEFAULT 'Unknown',
"bio_text" TEXT, "bio_text" TEXT NOT NULL DEFAULT 'No information given.',
"balance" REAL NOT NULL DEFAULT 0, "balance" REAL NOT NULL DEFAULT 0,
"avatar_url" TEXT NOT NULL DEFAULT '/avatars/default.png', "avatar_url" TEXT NOT NULL DEFAULT '/avatars/default.png',
"reputation" INTEGER NOT NULL DEFAULT 100, "reputation" INTEGER NOT NULL DEFAULT 100,

@ -1,7 +1,7 @@
.form-container .form-container
form action="/user/login" method="post" form action="/user/login" method="post"
h2 Log in h2 Log in
label.error_msg = session[:error_msg] label.error_msg = params[:error]
input type="text" name="email" placeholder="Email" input type="text" name="email" placeholder="Email"
input type="password" name="password" placeholder="Password" input type="password" name="password" placeholder="Password"
input type="submit" value="Log in" input type="submit" value="Log in"

Loading…
Cancel
Save