basic register/login functionality. prepared session storage.
This commit is contained in:
parent
3711e8354e
commit
abc5d75e48
6 changed files with 134 additions and 52 deletions
|
@ -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 = ->
|
||||
new Promise (resolve, reject) ->
|
||||
@db.close (err) ->
|
||||
if err
|
||||
console.error err.message
|
||||
console.log 'Closed', @filename
|
||||
resolve "[FFTCGDB] Error closing: '#{err.message}'"
|
||||
else
|
||||
reject "[FFTCGDB] Closed '#{@filename}'"
|
||||
|
||||
FFTCGDB::register = (login, password) ->
|
||||
that = @
|
||||
|
@ -34,13 +35,19 @@ 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
|
||||
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
|
||||
|
||||
|
@ -48,11 +55,23 @@ 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()
|
||||
|
|
62
inc/socket.coffee
Normal file
62
inc/socket.coffee
Normal file
|
@ -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
|
|
@ -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()
|
||||
|
|
|
@ -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 ->
|
||||
|
|
24
src/index/localStorage.coffee
Normal file
24
src/index/localStorage.coffee
Normal file
|
@ -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"
|
|
@ -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;
|
||||
|
|
Reference in a new issue