Store signin data in DB and use random codes

This commit is contained in:
2025-06-22 09:06:33 +03:00
parent 708ca49a35
commit ccbb317189
5 changed files with 95 additions and 82 deletions

View File

@@ -8,6 +8,7 @@ ALPHANUM = [*"0".."9", *"A".."Z", *"a".."z", "-", "_"].freeze
module Players
db_file = File.expand_path("infinsweeper.db")
DB = Sequel.connect("sqlite:///#{db_file}", single_threaded: false)
DB.run("PRAGMA foreign_keys = ON;")
def self.list
DB["select * from Players"].all
@@ -26,19 +27,7 @@ module Players
digest = XXhash.xxh32(pass, 1234)
path = File.expand_path("db.json")
json = File.exist?(path) ? JSON.parse(File.read(path)) : {}
json["account_num"] ||= 0
account_num = json["account_num"]
json["account_num"] += 1
File.write(path, JSON.pretty_generate(json))
account_num = XXhash.xxh64(account_num, 1234)
code = ""
while account_num.positive?
code << ALPHANUM[account_num % 64]
account_num /= 64
end
code = code.reverse.rjust(12, "0")
code = Array.new(24) { ALPHANUM.sample }.join
DB[
"insert into Players (email, digest, username, activation_code) values (?, ?, ?, ?)",
@@ -64,20 +53,8 @@ module Players
def self.pass_req(email)
return unless self[email]
path = File.expand_path("db.json")
json = File.exist?(path) ? JSON.parse(File.read(path)) : {}
json["pass_num"] ||= 0
pass_num = json["pass_num"]
json["pass_num"] += 1
File.write(path, JSON.pretty_generate(json))
pass_num = XXhash.xxh64(pass_num, 1234)
code = ""
while pass_num.positive?
code << ALPHANUM[pass_num % 64]
pass_num /= 64
end
code = code.reverse.rjust(12, "0")
code = Array.new(24) { ALPHANUM.sample }.join
DB["update Players set new_pass_code = ? where email = ?", code, email].update
send_email(:pass_req, email, code)
@@ -108,9 +85,14 @@ module Players
end
Thread.new do
sleep 60 * 60
unverified.each do |player|
rm_player(player[:email]) if player[:created_at] + 24 * 60 * 60 < Time.now
loop do
unverified.each do |player|
rm_player(player[:email]) if player[:created_at] + 24 * 60 * 60 < Time.now
end
rescue StandardError => e
File.write("log/main.log", "Thread error: #{e.message}\n", mode: "a")
ensure
sleep 60 * 60
end
end
end