44 lines
1,003 B
CoffeeScript
44 lines
1,003 B
CoffeeScript
|
# libraries
|
||
|
bcrypt = (require 'bcrypt')
|
||
|
sqlite3 = (require 'sqlite3').verbose()
|
||
|
|
||
|
FFTCGDB =
|
||
|
open: (filename) ->
|
||
|
@filename = filename
|
||
|
|
||
|
@db = new sqlite3.Database @filename, (err) ->
|
||
|
if err
|
||
|
console.error err.message
|
||
|
console.log 'Connected to', @filename
|
||
|
|
||
|
@db.run """
|
||
|
CREATE TABLE IF NOT EXISTS users (
|
||
|
nick text NOT NULL,
|
||
|
pwd text NOT NULL,
|
||
|
socket text
|
||
|
);
|
||
|
"""
|
||
|
|
||
|
close: ->
|
||
|
db.close (err) ->
|
||
|
if err
|
||
|
console.error err.message
|
||
|
console.log 'Closed', @filename
|
||
|
|
||
|
adduser: (name, password) ->
|
||
|
saltRounds = 13
|
||
|
that = @
|
||
|
|
||
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||
|
that.db.run 'INSERT INTO users (nick, pwd) VALUES (\'' + name + '\', \'' + hash + '\');', (err, result) ->
|
||
|
if err
|
||
|
console.error err.message
|
||
|
console.log 'Result:', result
|
||
|
|
||
|
module.exports = FFTCGDB
|
||
|
|
||
|
# Handle termination
|
||
|
process.on 'SIGINT', ->
|
||
|
console.log '[FFTCG-DB] shutting down'
|
||
|
FFTCGDB.close()
|