diff --git a/inc/fftcgdb.coffee b/inc/fftcgdb.coffee index 76b6422..de5c422 100644 --- a/inc/fftcgdb.coffee +++ b/inc/fftcgdb.coffee @@ -11,21 +11,22 @@ FFTCGDB = (filename) -> @db.run """ CREATE TABLE IF NOT EXISTS users ( - login text NOT NULL, - nickname text, - pwd text NOT NULL, - socket text, + login text NOT NULL COLLATE NOCASE, + pwdhash text NOT NULL, + session text, UNIQUE(login) ); """ - console.log 'Connected to', @filename + console.log "[FFTCGDB] Connected to '#{@filename}'" return FFTCGDB::close = -> - @db.close (err) -> - if err - console.error err.message - console.log 'Closed', @filename + new Promise (resolve, reject) -> + @db.close (err) -> + if err + resolve "[FFTCGDB] Error closing: '#{err.message}'" + else + reject "[FFTCGDB] Closed '#{@filename}'" FFTCGDB::register = (login, password) -> that = @ @@ -34,25 +35,43 @@ FFTCGDB::register = (login, password) -> new Promise (resolve, reject) -> # validate username - login = login.toLowerCase() + # hash password bcrypt.hash password, saltRounds, (err, hash) -> - reject 'bcrypt fail' if err + reject 'hash' 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 + that.db.run "INSERT INTO users (login, pwdhash) VALUES ('#{login}', '#{hash}');", (err) -> + if err + if err.code == 'SQLITE_CONSTRAINT' + reject 'existence' + else + reject 'db' + + else + # registration successful + resolve login FFTCGDB::login = (login, password) -> that = @ new Promise (resolve, reject) -> + # validate username + # get users table row + that.db.all "SELECT rowid, pwdhash FROM users WHERE login = '#{login}';", (err, rows) -> + if err + reject 'db' + + else if rows.length == 0 + reject 'existence' + + else + row = rows[0] + bcrypt.compare password, row.pwdhash, (err, res) -> + reject 'hash' if err + if res == true + resolve row.rowid + else + reject 'login' module.exports = FFTCGDB - -# Handle termination -process.on 'SIGINT', -> - console.log '[FFTCG-DB] shutting down' - FFTCGDB.close() diff --git a/inc/socket.coffee b/inc/socket.coffee new file mode 100644 index 0000000..c38b177 --- /dev/null +++ b/inc/socket.coffee @@ -0,0 +1,62 @@ +# node libraries +socketio = (require 'socket.io') +path = (require 'path') + +# my libraries +FFTCGDB = (require './fftcgdb') + +FFTCGSOCKET = (http, dbfile) -> + that = @ + @io = socketio http + @db = new FFTCGDB dbfile + + @io.on 'connection', (socket) -> + that.__connection socket + + return + +FFTCGSOCKET::__connection = (socket) -> + that = @ + + console.log 'a user connected' + + console.log socket.handshake.headers['cookie'] + socket.emit 'id', socket.handshake.headers['cookie'] + + socket.on 'disconnect', -> + console.log 'a user disconnected' + + socket.on 'register', (login, password) -> + that.__register login, password + + socket.on 'login', (login, password) -> + that.__login login, password + +FFTCGSOCKET::__login = (login, password) -> + console.log '__login:', login, password + @db.login login, password + .then (login) -> + console.log 'Login OK "%s"', login + .catch (err) -> + console.error 'error: "%s"', err + +FFTCGSOCKET::__register = (login, password) -> + console.log '__register:', login, password + @db.register login, password + .then (login) -> + console.log 'registered "%s"', login + .catch (err) -> + console.error 'error: "%s"', err + +FFTCGSOCKET::close = -> + console.log '[FFTCGSOCKET] shutting down' + if @db + @db.close() + .then (msg) -> + console.log msg + .catch (err) -> + console.error err + + + +module.exports = FFTCGSOCKET diff --git a/server.coffee b/server.coffee index dc3765d..df7c9af 100644 --- a/server.coffee +++ b/server.coffee @@ -3,17 +3,14 @@ express = (require 'express') helmet = (require 'helmet') http = (require 'http') path = (require 'path') -socketio = (require 'socket.io') # my libraries -FFTCGDB = (require './inc/fftcgdb') +FFTCGSOCKET = (require './inc/socket') -fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db') - -# express + socket.io framework +# express + socket framework app = express() web = http.Server app -io = socketio web +socket = new FFTCGSOCKET(web, path.resolve(__dirname, 'fftcg.db')) app.use helmet() @@ -25,36 +22,12 @@ app.set 'view engine', 'pug' app.get '/:template.html', (req, res) -> res.render (req.params.template + '.pug') -# Server logic -io.on 'connection', (socket) -> - - console.log 'a user connected' - - socket.on 'disconnect', -> - console.log 'a user disconnected' - return - - socket.on 'register', (login, password) -> - console.log 'message:', login, password - fftcgdb.register login, password - .then (login) -> - console.log 'registered "%s"', login - .catch (err) -> - console.error 'error: "%s"', err - - socket.on 'login', (uname, password) -> - console.log 'message:', uname, password - fftcgdb.login uname, password - .then (login) -> - console.log 'Login OK "%s"', login - .catch (err) -> - console.error 'error: "%s"', err - # Create server web.listen 3000, -> console.log '[FFTCG] Listening on port 3000 ...' # Handle termination process.on 'SIGINT', -> + socket.close() console.log '[FFTCG] shutting down after SIGINT' process.exit() diff --git a/src/index.coffee b/src/index.coffee index b7cdaf8..1aa5aa5 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -12,6 +12,10 @@ $ -> # init Socket.IO socket = io() + require './index/localStorage.coffee' + + socket.on 'id', (session) -> + console.log session # login form $('form[name="login"]').submit -> diff --git a/src/index/localStorage.coffee b/src/index/localStorage.coffee new file mode 100644 index 0000000..13a9c60 --- /dev/null +++ b/src/index/localStorage.coffee @@ -0,0 +1,24 @@ +window.storageAvailable = (type) -> + try + storage = window[type] + x = '__storage_test__' + storage.setItem x, x + storage.removeItem x + true + + catch e + e instanceof DOMException and + # everything except Firefox + (e.code == 22 or + # Firefox + e.code == 1014 or + # test name field too, because code might not be present + # everything except Firefox + e.name == 'QuotaExceededError' or + # Firefox + e.name == 'NS_ERROR_DOM_QUOTA_REACHED') and + # acknowledge QuotaExceededError only if there's something already stored + storage.length != 0 + +if storageAvailable 'sessionStorage' + console.log "yay" diff --git a/src/style/custom.scss b/src/style/custom.scss index d2c0047..d7cc7e1 100644 --- a/src/style/custom.scss +++ b/src/style/custom.scss @@ -13,7 +13,7 @@ html, body { body { margin: 0; background-color: #aaa; - background-image: url(//gameranx.com/wp-content/uploads/2016/02/Final-Fantasy-XV-4K-Wallpaper.jpg); + // background-image: url(//gameranx.com/wp-content/uploads/2016/02/Final-Fantasy-XV-4K-Wallpaper.jpg); background-position-x: center; background-position-y: center; background-size: cover;