sessions!

This commit is contained in:
Jörn-Michael Miehe 2018-12-15 21:09:01 +01:00
parent 8d5a31a5dc
commit d7cdb08fb6
5 changed files with 45 additions and 48 deletions

View file

@ -5,10 +5,14 @@ path = (require 'path')
# my libraries # my libraries
FFTCGDB = (require './fftcgdb') FFTCGDB = (require './fftcgdb')
FFTCGSOCKET = (http, dbfile) -> FFTCGSOCKET = (http, dbfile, session) ->
that = @ that = @
# create server socket, open fftcg.db
# create server socket
@io = socketio http @io = socketio http
@io.use session
# open fftcg db
@db = new FFTCGDB dbfile @db = new FFTCGDB dbfile
# on new connection # on new connection
@ -20,16 +24,13 @@ FFTCGSOCKET = (http, dbfile) ->
FFTCGSOCKET::__connection = (socket) -> FFTCGSOCKET::__connection = (socket) ->
that = @ that = @
# offer server socket id @session = socket.handshake.session
socket.emit 'serverid', socket.id console.log "session '#{@session.id}' connected"
console.log "is user '#{@session.userID}'" if @session.userID
# accept client side id
socket.on 'clientid', (clientid) ->
@clientid = clientid
console.log "user '#{@clientid}' connected"
socket.on 'disconnect', -> socket.on 'disconnect', ->
console.log "user '#{@clientid}' disconnected" console.log "session '#{that.session.id}' disconnected"
console.log "is user '#{that.session.userID}'" if that.session.userID
socket.on 'register', (login, password) -> socket.on 'register', (login, password) ->
that.__register login, password that.__register login, password
@ -38,9 +39,13 @@ FFTCGSOCKET::__connection = (socket) ->
that.__login login, password that.__login login, password
FFTCGSOCKET::__login = (login, password) -> FFTCGSOCKET::__login = (login, password) ->
that = @
console.log '__login:', login, password console.log '__login:', login, password
@db.login login, password @db.login login, password
.then (login) -> .then (login) ->
that.session.userID = login
that.session.save()
console.log 'Login OK "%s"', login console.log 'Login OK "%s"', login
.catch (err) -> .catch (err) ->
console.error 'error: "%s"', err console.error 'error: "%s"', err

View file

@ -43,6 +43,8 @@
"bcrypt": "^3.0.2", "bcrypt": "^3.0.2",
"coffeescript": "^2.3.2", "coffeescript": "^2.3.2",
"express": "^4.16.4", "express": "^4.16.4",
"express-session": "^1.15.6",
"express-socket.io-session": "^1.3.5",
"helmet": "^3.15.0", "helmet": "^3.15.0",
"pug": "^2.0.3", "pug": "^2.0.3",
"socket.io": "^2.2.0", "socket.io": "^2.2.0",

View file

@ -1,5 +1,7 @@
# node libraries # node libraries
express = (require 'express') express = (require 'express')
expressSession = (require 'express-session')
sharedSession = (require 'express-socket.io-session')
helmet = (require 'helmet') helmet = (require 'helmet')
http = (require 'http') http = (require 'http')
path = (require 'path') path = (require 'path')
@ -7,13 +9,32 @@ path = (require 'path')
# my libraries # my libraries
FFTCGSOCKET = (require './inc/fftcgsocket') FFTCGSOCKET = (require './inc/fftcgsocket')
# express + socket framework # express framework
app = express() app = express()
app.use helmet() app.use helmet()
# sessions
session =
secret: 'keyboard cat'
cookie:
httpOnly: true
sameSite: 'strict'
proxy: true
resave: true
saveUninitialized: true
if app.get 'env' == 'production'
app.set 'trust proxy', 1
session.cookie.secure = true
sessionMiddleware = expressSession session
app.use sessionMiddleware
# Static content # Static content
app.use express.static path.resolve(__dirname, 'public_html') app.use express.static path.resolve(__dirname, 'public_html')
# Templates # Templates
app.set 'view engine', 'pug' app.set 'view engine', 'pug'
app.get '/:template.html', (req, res) -> app.get '/:template.html', (req, res) ->
@ -24,7 +45,12 @@ web = http.Server app
web.listen 3000, -> web.listen 3000, ->
console.log '[FFTCG] Listening on port 3000 ...' console.log '[FFTCG] Listening on port 3000 ...'
socket = new FFTCGSOCKET web, path.resolve(__dirname, 'fftcg.db') # socket.io
socket = new FFTCGSOCKET(
web,
path.resolve(__dirname, 'fftcg.db'),
sharedSession sessionMiddleware
)
# Handle termination # Handle termination
process.on 'SIGINT', -> process.on 'SIGINT', ->

View file

@ -11,21 +11,6 @@ $ ->
# init Socket.IO # init Socket.IO
socket = io() socket = io()
console.log socket
require './index/localStorage.coffee'
socket.on 'id', (socketid) ->
if storageAvailable 'localStorage'
myid = localStorage.getItem 'myid'
if not myid
localStorage.setItem 'myid', socketid
myid = socketid
console.log "id is '#{myid}'"
socket.emit 'id', myid
# login form # login form
$('form[name="login"]').submit -> $('form[name="login"]').submit ->

View file

@ -1,21 +0,0 @@
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