2019-11-20 19:22:51 +01:00
|
|
|
bulma = require "./bulma.ls"
|
|
|
|
h = require 'maquette' .h
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2019-12-03 05:29:09 +01:00
|
|
|
create-socket: (socket-url) ->
|
2019-11-22 18:42:46 +01:00
|
|
|
self = {}
|
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
request-types = {
|
2019-12-03 05:29:09 +01:00
|
|
|
"get-token": 0
|
|
|
|
"add-user": 1
|
|
|
|
"get-user": 2
|
|
|
|
"get-user-by-credentials": 3
|
|
|
|
"mod-user": 4
|
2019-12-03 03:47:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response-types = {
|
2019-12-03 05:29:09 +01:00
|
|
|
"error": 0
|
|
|
|
"token": 1
|
|
|
|
"user": 2
|
|
|
|
"user-added": 3
|
|
|
|
"user-edited": 4
|
2019-12-03 03:47:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: naming convention
|
2019-11-22 18:42:46 +01:00
|
|
|
# users can record functions to run on events
|
2019-12-03 03:47:27 +01:00
|
|
|
self.user-on-socket-error = []
|
2019-12-03 05:29:09 +01:00
|
|
|
self.user-on-socket-close = []
|
2019-12-03 03:47:27 +01:00
|
|
|
|
|
|
|
self.callbacks = {}
|
2019-12-03 05:29:09 +01:00
|
|
|
for key, value of response-types
|
|
|
|
self.callbacks[value] = []
|
2019-12-03 03:47:27 +01:00
|
|
|
|
|
|
|
# self.user-on-message = []
|
|
|
|
|
|
|
|
self.add-event-listener = (type, callback) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
# console.log "authd: add event listener", type, callback, response-types[type]
|
2019-12-03 05:29:09 +01:00
|
|
|
console.log response-types
|
2019-12-03 03:47:27 +01:00
|
|
|
type = response-types[type]
|
|
|
|
|
|
|
|
self.callbacks[type] ++= [callback]
|
2019-11-22 18:42:46 +01:00
|
|
|
|
|
|
|
self.open-socket = ->
|
|
|
|
console.log "Opening socket to #{socket-url}"
|
|
|
|
self.socket := new WebSocket socket-url
|
|
|
|
|
|
|
|
self.socket.onerror = (event) ->
|
2019-12-03 03:47:27 +01:00
|
|
|
for f in self.user-on-socket-error
|
2019-11-22 18:42:46 +01:00
|
|
|
f event
|
|
|
|
self.socket.close!
|
|
|
|
|
|
|
|
self.socket.onclose = (event) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
for f in self.user-on-socket-close
|
2019-11-22 18:42:46 +01:00
|
|
|
f event
|
|
|
|
|
|
|
|
self.socket.onmessage = (event) ->
|
2019-12-03 03:47:27 +01:00
|
|
|
message = JSON.parse(event.data)
|
2019-12-03 05:29:09 +01:00
|
|
|
console.log "authd message received: ", message
|
2019-11-22 18:42:46 +01:00
|
|
|
|
2019-12-03 05:29:09 +01:00
|
|
|
for f in self.callbacks[message.mtype]
|
2019-12-03 03:47:27 +01:00
|
|
|
f JSON.parse(message.payload)
|
2019-11-22 18:42:46 +01:00
|
|
|
|
|
|
|
self.reopen = ->
|
|
|
|
self.socket.close!
|
|
|
|
self.open-socket!
|
|
|
|
|
|
|
|
self.open-socket!
|
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
self.send = (type, opts) ->
|
|
|
|
console.log JSON.stringify { mtype: type, payload: opts }
|
|
|
|
self.socket.send JSON.stringify { mtype: type, payload: opts }
|
2019-11-22 18:42:46 +01:00
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
self.get-token = (login, password) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
self.send request-types[\get-token], JSON.stringify {
|
2019-11-22 18:42:46 +01:00
|
|
|
login: login
|
|
|
|
password: password
|
|
|
|
}
|
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
self.get-user-by-credentials = (login, password) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
self.send request-types[\get-user-by-credentials], JSON.stringify {
|
2019-12-03 03:47:27 +01:00
|
|
|
login: login
|
|
|
|
password: password
|
|
|
|
}
|
2019-11-22 18:42:46 +01:00
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
self.login = (login, password) ->
|
|
|
|
self.get-token login, password
|
|
|
|
self.get-user-by-credentials login, password
|
2019-11-20 19:22:51 +01:00
|
|
|
|
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
self.get-user = (uid) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
self.send request-types[\get-user], JSON.stringify {
|
2019-12-03 03:47:27 +01:00
|
|
|
uid: uid
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: authd overhaul
|
|
|
|
#self.add-user = (login, password) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
# self.send request-types[\add-user], JSON.stringify {
|
2019-12-03 03:47:27 +01:00
|
|
|
# login: login
|
|
|
|
# password: password
|
|
|
|
# }
|
|
|
|
|
|
|
|
# TODO: authd overhaul
|
|
|
|
#self.mod-user = (uid) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
# self.send request-types[\mod-user], JSON.stringify {
|
2019-12-03 03:47:27 +01:00
|
|
|
# uid: uid
|
|
|
|
# }
|
|
|
|
|
|
|
|
self
|
2019-11-20 19:22:51 +01:00
|
|
|
|
2019-11-22 18:42:46 +01:00
|
|
|
login-page: (model) ->
|
2019-12-03 03:47:27 +01:00
|
|
|
# XXX: container = marge
|
|
|
|
# XXX: box = dessiner un contour
|
2019-11-20 19:22:51 +01:00
|
|
|
h \div.container [
|
|
|
|
h \div.box [
|
|
|
|
if model.login-error
|
|
|
|
h \div.notification.is-danger [
|
|
|
|
model.login-error
|
|
|
|
]
|
|
|
|
h \form [
|
|
|
|
bulma.field [
|
|
|
|
bulma.label "Login"
|
|
|
|
bulma.input {
|
|
|
|
oninput: (e) ->
|
|
|
|
model.login = e.target.value
|
|
|
|
name: \login
|
|
|
|
id: \login-input
|
|
|
|
}
|
|
|
|
]
|
|
|
|
bulma.field [
|
|
|
|
bulma.label "Password"
|
|
|
|
bulma.input {
|
|
|
|
oninput: (e) ->
|
|
|
|
model.password = e.target.value
|
|
|
|
name: \password
|
|
|
|
type: \password
|
|
|
|
id: \password-input
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
h \button.button.is-fullwidth.is-primary {
|
|
|
|
onclick: (e) ->
|
2019-12-03 03:47:27 +01:00
|
|
|
# not to refresh the page since it's a form button
|
2019-11-20 19:22:51 +01:00
|
|
|
e.prevent-default!
|
|
|
|
|
2019-12-03 03:47:27 +01:00
|
|
|
model.authd-ws.get-token model.login, model.password
|
2019-11-20 19:22:51 +01:00
|
|
|
} [ "Connexion" ]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|