todo-webclient/client/authd.ls

182 lines
4.0 KiB
Plaintext
Raw Normal View History

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-10 05:59:52 +01:00
self.token = ""
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-10 05:59:52 +01:00
# TODO: code these messages
"register": 5
"get-extra": 6
"set-extra": 7
"update-password": 8
"list-users": 9
2019-12-11 00:10:58 +01:00
"set-permissions": 10
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-10 05:59:52 +01:00
"extra": 5
"extra-updated": 6
"users-list": 7
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) ->
# 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
}
2019-12-10 05:59:52 +01:00
self.list-users = ->
self.send request-types[\list-users], JSON.stringify {
token: self.token
# FIXME: this will be removed once the authd program will accept
# any list-users requests from any user
# which is the only logical choice since any user can create projects and assign people to it
key: "nico-nico-nii"
}
2019-12-11 00:10:58 +01:00
self.set-permissions = (list-id, user-id, permission) ->
self.send request-types[\set-permissions], JSON.stringify {
token: self.token
list: list-id
uid: user-id
permission: permission
}
2019-12-03 03:47:27 +01:00
# 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" ]
]
]
]
}