diff --git a/src/Gemfile b/src/Gemfile index 6b4f169..63fdfd7 100644 --- a/src/Gemfile +++ b/src/Gemfile @@ -12,3 +12,4 @@ gem "sqlite3" gem "slim" gem "sassc" gem "colorize" +gem "bcrypt" diff --git a/src/Gemfile.lock b/src/Gemfile.lock index a8bea08..06e5031 100644 --- a/src/Gemfile.lock +++ b/src/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + bcrypt (3.1.16) colorize (0.8.1) ffi (1.15.5) multi_json (1.15.0) @@ -36,6 +37,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + bcrypt colorize sassc sinatra diff --git a/src/app.rb b/src/app.rb index 02c3585..ac107d2 100755 --- a/src/app.rb +++ b/src/app.rb @@ -8,11 +8,14 @@ require "slim" require "sqlite3" require "sassc" require "colorize" +require "bcrypt" require_relative "debug.rb" require_relative "lib/database.rb" require_relative "func.rb" +require_relative "db_models.rb" + load_tables = [ "User", "Role" @@ -21,20 +24,27 @@ db = Database.new("main", load_tables) enable :sessions +def init_params(params={}) + g = Hash.new "" + g.merge(params) +end + + +# Routes get "/style.css" do sass :"stylesheets/style", style: :compressed end get "/" do - slim :index + slim :index, locals: {params: init_params} end get "/login" do - slim :"user/login" + slim :"user/login", locals: {params: init_params} end get "/register" do - slim :"user/register" + slim :"user/register", locals: {params: init_params} end # API stuff diff --git a/src/db_models.rb b/src/db_models.rb index e69de29..b173dc5 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -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 diff --git a/src/lib/database.rb b/src/lib/database.rb index 03e2ce5..545663c 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -4,10 +4,10 @@ class Table attr_reader :name attr_accessor :db - def initialize(db, name, sql_file) + def initialize(db, name) @db = db @name = name - @sql_file = sql_file + @sql_file = "sql/tables/#{name}.sql" end def create_table @@ -19,20 +19,24 @@ class Table 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) end - def insert(data, filter="") + private def insert(data, filter="") @db.insert(@name, data, filter) end - def update(data, filter="") + private def update(data, filter="") @db.update(@name, data, filter) end # 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 @db.update(@name, data, filter) else @@ -51,7 +55,7 @@ class Database # Database class @tables = [] # generate table objects tables_names.each do |name| - tbl = Table.new(self, name, "sql/tables/#{name}.sql") + tbl = Table.new(self, name) @tables << tbl end end @@ -96,7 +100,7 @@ class Database # Database class self.query(q, *data.values ) 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 self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values ) end diff --git a/src/sql/tables/User.sql b/src/sql/tables/User.sql index 403e2ff..9f5d807 100644 --- a/src/sql/tables/User.sql +++ b/src/sql/tables/User.sql @@ -3,7 +3,7 @@ CREATE TABLE "User" ( "email" TEXT NOT NULL UNIQUE, "pw_hash" TEXT NOT NULL UNIQUE, "name" TEXT NOT NULL DEFAULT 'Unknown', - "bio_text" TEXT, + "bio_text" TEXT NOT NULL DEFAULT 'No information given.', "balance" REAL NOT NULL DEFAULT 0, "avatar_url" TEXT NOT NULL DEFAULT '/avatars/default.png', "reputation" INTEGER NOT NULL DEFAULT 100, diff --git a/src/views/user/login.slim b/src/views/user/login.slim index c8c02fb..a99bdd4 100644 --- a/src/views/user/login.slim +++ b/src/views/user/login.slim @@ -1,7 +1,7 @@ .form-container form action="/user/login" method="post" h2 Log in - label.error_msg = session[:error_msg] + label.error_msg = params[:error] input type="text" name="email" placeholder="Email" input type="password" name="password" placeholder="Password" input type="submit" value="Log in"