This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/inc/fftcgdb.coffee

59 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2018-12-07 09:38:46 +00:00
# libraries
bcrypt = (require 'bcrypt')
sqlite3 = (require 'sqlite3').verbose()
2018-12-14 06:03:03 +00:00
FFTCGDB = (filename) ->
@filename = filename
@db = new sqlite3.Database @filename, (err) ->
if err
console.error err.message
@db.run """
CREATE TABLE IF NOT EXISTS users (
login text NOT NULL,
nickname text,
pwd text NOT NULL,
socket text,
UNIQUE(login)
);
"""
console.log 'Connected to', @filename
return
FFTCGDB::close = ->
@db.close (err) ->
if err
console.error err.message
console.log 'Closed', @filename
FFTCGDB::register = (login, password) ->
that = @
# bruteforce countermeasure
saltRounds = 13
new Promise (resolve, reject) ->
# validate username
login = login.toLowerCase()
# hash password
2018-12-07 09:38:46 +00:00
bcrypt.hash password, saltRounds, (err, hash) ->
2018-12-14 06:03:03 +00:00
reject 'bcrypt fail' if err
# try creating row in users table
that.db.run "INSERT INTO users (login, pwd) VALUES ('#{login}', '#{hash}');", (err, result) ->
reject 'sqlite fail' if err
# registration successful
resolve login
FFTCGDB::login = (login, password) ->
that = @
new Promise (resolve, reject) ->
2018-12-07 09:38:46 +00:00
module.exports = FFTCGDB
# Handle termination
process.on 'SIGINT', ->
console.log '[FFTCG-DB] shutting down'
FFTCGDB.close()