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 """
|
@db.run """
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
login text NOT NULL,
|
login text NOT NULL COLLATE NOCASE,
|
||||||
nickname text,
|
pwdhash text NOT NULL,
|
||||||
pwd text NOT NULL,
|
session text,
|
||||||
socket text,
|
|
||||||
UNIQUE(login)
|
UNIQUE(login)
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
console.log 'Connected to', @filename
|
console.log "[FFTCGDB] Connected to '#{@filename}'"
|
||||||
return
|
return
|
||||||
|
|
||||||
FFTCGDB::close = ->
|
FFTCGDB::close = ->
|
||||||
@db.close (err) ->
|
new Promise (resolve, reject) ->
|
||||||
if err
|
@db.close (err) ->
|
||||||
console.error err.message
|
if err
|
||||||
console.log 'Closed', @filename
|
resolve "[FFTCGDB] Error closing: '#{err.message}'"
|
||||||
|
else
|
||||||
|
reject "[FFTCGDB] Closed '#{@filename}'"
|
||||||
|
|
||||||
FFTCGDB::register = (login, password) ->
|
FFTCGDB::register = (login, password) ->
|
||||||
that = @
|
that = @
|
||||||
|
@ -34,25 +35,43 @@ FFTCGDB::register = (login, password) ->
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
new Promise (resolve, reject) ->
|
||||||
# validate username
|
# validate username
|
||||||
login = login.toLowerCase()
|
|
||||||
# hash password
|
# hash password
|
||||||
bcrypt.hash password, saltRounds, (err, hash) ->
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||||||
reject 'bcrypt fail' if err
|
reject 'hash' if err
|
||||||
# try creating row in users table
|
# try creating row in users table
|
||||||
that.db.run "INSERT INTO users (login, pwd) VALUES ('#{login}', '#{hash}');", (err, result) ->
|
that.db.run "INSERT INTO users (login, pwdhash) VALUES ('#{login}', '#{hash}');", (err) ->
|
||||||
reject 'sqlite fail' if err
|
if err
|
||||||
# registration successful
|
if err.code == 'SQLITE_CONSTRAINT'
|
||||||
resolve login
|
reject 'existence'
|
||||||
|
else
|
||||||
|
reject 'db'
|
||||||
|
|
||||||
|
else
|
||||||
|
# registration successful
|
||||||
|
resolve login
|
||||||
|
|
||||||
FFTCGDB::login = (login, password) ->
|
FFTCGDB::login = (login, password) ->
|
||||||
that = @
|
that = @
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
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
|
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')
|
helmet = (require 'helmet')
|
||||||
http = (require 'http')
|
http = (require 'http')
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
socketio = (require 'socket.io')
|
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
FFTCGDB = (require './inc/fftcgdb')
|
FFTCGSOCKET = (require './inc/socket')
|
||||||
|
|
||||||
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
# express + socket framework
|
||||||
|
|
||||||
# express + socket.io framework
|
|
||||||
app = express()
|
app = express()
|
||||||
web = http.Server app
|
web = http.Server app
|
||||||
io = socketio web
|
socket = new FFTCGSOCKET(web, path.resolve(__dirname, 'fftcg.db'))
|
||||||
|
|
||||||
app.use helmet()
|
app.use helmet()
|
||||||
|
|
||||||
|
@ -25,36 +22,12 @@ app.set 'view engine', 'pug'
|
||||||
app.get '/:template.html', (req, res) ->
|
app.get '/:template.html', (req, res) ->
|
||||||
res.render (req.params.template + '.pug')
|
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
|
# Create server
|
||||||
web.listen 3000, ->
|
web.listen 3000, ->
|
||||||
console.log '[FFTCG] Listening on port 3000 ...'
|
console.log '[FFTCG] Listening on port 3000 ...'
|
||||||
|
|
||||||
# Handle termination
|
# Handle termination
|
||||||
process.on 'SIGINT', ->
|
process.on 'SIGINT', ->
|
||||||
|
socket.close()
|
||||||
console.log '[FFTCG] shutting down after SIGINT'
|
console.log '[FFTCG] shutting down after SIGINT'
|
||||||
process.exit()
|
process.exit()
|
||||||
|
|
|
@ -12,6 +12,10 @@ $ ->
|
||||||
# init Socket.IO
|
# init Socket.IO
|
||||||
socket = io()
|
socket = io()
|
||||||
|
|
||||||
|
require './index/localStorage.coffee'
|
||||||
|
|
||||||
|
socket.on 'id', (session) ->
|
||||||
|
console.log session
|
||||||
|
|
||||||
# login form
|
# login form
|
||||||
$('form[name="login"]').submit ->
|
$('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 {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
background-color: #aaa;
|
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-x: center;
|
||||||
background-position-y: center;
|
background-position-y: center;
|
||||||
background-size: cover;
|
background-size: cover;
|
||||||
|
|
Reference in a new issue