User: login progress

master
E. Almqvist 3 years ago
parent d363aa9e77
commit e0a927449d
  1. 4
      src/config.rb
  2. 19
      src/db_models.rb
  3. 86
      src/lib/database.rb
  4. 2
      src/misc/subtitles.txt

@ -16,3 +16,7 @@ REGISTER_ERRORS = {
email_fake: "Please use a valid email address" email_fake: "Please use a valid email address"
} }
LOGIN_ERRORS = {
fields: "Please fill all of the fields",
fail: "Wrong password and/or email"
}

@ -1,3 +1,4 @@
# User table model
class User < Table class User < Table
def initialize(db) def initialize(db)
super db, "User" super db, "User"
@ -6,16 +7,16 @@ class User < Table
# 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 find_by_id(id) def find_by_id(id)
resp = self.get("*", "id = ?", id) self.get("*", "id = ?", id)
end end
# Find user by email, same as above but for emails. # Find user by email, same as above but for emails.
# Also unique # Also unique
def find_by_email(email) def find_by_email(email)
resp = self.get("*", "email = ?", email) self.get("*", "email = ?", email)
end end
private def validate_credentials(email, name, password, password_confirm) private def validate_register_creds(email, name, password, password_confirm)
# Field check # Field check
check_all_fields = email != "" && name != "" && password != "" && password_confirm != "" check_all_fields = email != "" && name != "" && password != "" && password_confirm != ""
@ -43,7 +44,7 @@ class User < Table
# Register a new user # Register a new user
# Returns: success?, data # Returns: success?, data
def register(email, name, password, password_confirm) 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 if( check ) then
pw_hash = BCrypt::Password.create(password) pw_hash = BCrypt::Password.create(password)
@ -59,4 +60,14 @@ class User < Table
return check, errorstr return check, errorstr
end end
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 end

@ -1,48 +1,5 @@
DEFAULT_DB_PATH = "db/main.db" 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 class Database # Database class
attr_reader :name, :path attr_reader :name, :path
attr_accessor :tables attr_accessor :tables
@ -107,3 +64,46 @@ class Database # Database class
@tables[tablesym] @tables[tablesym]
end end
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

@ -1,6 +1,6 @@
V2FpdCwgbW9uZXkgaGFzIG5vIHZhbHVlPyEK V2FpdCwgbW9uZXkgaGFzIG5vIHZhbHVlPyEK
No such place like 127.0.0.1! 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... I would just like to interject for a moment...
68747470733a2f2f777963682e6465762f6369706865722e747874 68747470733a2f2f777963682e6465762f6369706865722e747874
CSRF? No, I do not think so. CSRF? No, I do not think so.

Loading…
Cancel
Save