diff --git a/src/config.rb b/src/config.rb index 605e293..2ef4ce1 100644 --- a/src/config.rb +++ b/src/config.rb @@ -16,3 +16,7 @@ REGISTER_ERRORS = { email_fake: "Please use a valid email address" } +LOGIN_ERRORS = { + fields: "Please fill all of the fields", + fail: "Wrong password and/or email" +} diff --git a/src/db_models.rb b/src/db_models.rb index fc020ea..536d011 100644 --- a/src/db_models.rb +++ b/src/db_models.rb @@ -1,3 +1,4 @@ +# User table model class User < Table def initialize(db) super db, "User" @@ -6,16 +7,16 @@ class User < Table # Find user by ID, returns multiple results if multiple IDs exist # (which wont happen since IDs are unique) def find_by_id(id) - resp = self.get("*", "id = ?", id) + self.get("*", "id = ?", id) end # Find user by email, same as above but for emails. # Also unique def find_by_email(email) - resp = self.get("*", "email = ?", email) + self.get("*", "email = ?", email) end - private def validate_credentials(email, name, password, password_confirm) + private def validate_register_creds(email, name, password, password_confirm) # Field check check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" @@ -43,7 +44,7 @@ class User < Table # Register a new user # Returns: success?, data def register(email, name, password, password_confirm) - check, errorstr = self.validate_credentials(email, name, password, password_confirm) + check, errorstr = self.validate_register_creds(email, name, password, password_confirm) if( check ) then pw_hash = BCrypt::Password.create(password) @@ -59,4 +60,14 @@ class User < Table return check, errorstr end end + + # Log in user + # Returns: success?, auth token + def login(email, password) + user_query = self.find_by_email email # get the user info + + if user_query.length >= 1 then + user_info = user_query.first + end + end end diff --git a/src/lib/database.rb b/src/lib/database.rb index d8bf015..ad8117d 100644 --- a/src/lib/database.rb +++ b/src/lib/database.rb @@ -1,48 +1,5 @@ DEFAULT_DB_PATH = "db/main.db" -class Table - attr_reader :name - attr_accessor :db - - def initialize(db, name) - @db = db - @name = name - @sql_file = "sql/tables/#{name}.sql" - - begin - q = File.read @sql_file # get SQL script - @db.query q # run query - rescue Errno::ENOENT => err - error "#{err}" - end - end - - # these methods are private because they - # are intended to be accessed through a - # "Table Model". - # See "db_models.rb" - private def get(attr, filter="", *args) - @db.get(@name, attr, filter, *args) - end - - private def insert(data) - @db.insert(@name, data) - end - - private def update(data, filter="") - @db.update(@name, data, filter) - end - - # sets or updates a specific field in the table - 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 - @db.insert(@name, data, filter) - end - end -end - class Database # Database class attr_reader :name, :path attr_accessor :tables @@ -107,3 +64,46 @@ class Database # Database class @tables[tablesym] end end + +class Table + attr_reader :name + attr_accessor :db + + def initialize(db, name) + @db = db + @name = name + @sql_file = "sql/tables/#{name}.sql" + + begin + q = File.read @sql_file # get SQL script + @db.query q # run query + rescue Errno::ENOENT => err + error "#{err}" + end + end + + # these methods are private because they + # are intended to be accessed through a + # "Table Model". + # See "db_models.rb" + private def get(attr, filter="", *args) + @db.get(@name, attr, filter, *args) + end + + private def insert(data) + @db.insert(@name, data) + end + + private def update(data, filter="") + @db.update(@name, data, filter) + end + + # sets or updates a specific field in the table + 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 + @db.insert(@name, data, filter) + end + end +end diff --git a/src/misc/subtitles.txt b/src/misc/subtitles.txt index ace544a..6276333 100644 --- a/src/misc/subtitles.txt +++ b/src/misc/subtitles.txt @@ -1,6 +1,6 @@ V2FpdCwgbW9uZXkgaGFzIG5vIHZhbHVlPyEK No such place like 127.0.0.1! -Fourty-two? What do you mean? Tell me! +Fourty-two? That's not much for an answer... I would just like to interject for a moment... 68747470733a2f2f777963682e6465762f6369706865722e747874 CSRF? No, I do not think so.