bulma = require "./bulma.ls" h = require 'maquette' .h module.exports = { create-socket: (socket-url) -> self = {} self.token = "" request-types = { "get-token": 0 "add-user": 1 "get-user": 2 "get-user-by-credentials": 3 "mod-user": 4 # TODO: code these messages "register": 5 "get-extra": 6 "set-extra": 7 "update-password": 8 "list-users": 9 } response-types = { "error": 0 "token": 1 "user": 2 "user-added": 3 "user-edited": 4 "extra": 5 "extra-updated": 6 "users-list": 7 } # TODO: naming convention # users can record functions to run on events self.user-on-socket-error = [] self.user-on-socket-close = [] self.callbacks = {} for key, value of response-types self.callbacks[value] = [] # self.user-on-message = [] self.add-event-listener = (type, callback) -> # console.log "authd: add event listener", type, callback, response-types[type] console.log response-types type = response-types[type] self.callbacks[type] ++= [callback] self.open-socket = -> console.log "Opening socket to #{socket-url}" self.socket := new WebSocket socket-url self.socket.onerror = (event) -> for f in self.user-on-socket-error f event self.socket.close! self.socket.onclose = (event) -> for f in self.user-on-socket-close f event self.socket.onmessage = (event) -> message = JSON.parse(event.data) console.log "authd message received: ", message for f in self.callbacks[message.mtype] f JSON.parse(message.payload) self.reopen = -> self.socket.close! self.open-socket! self.open-socket! self.send = (type, opts) -> console.log JSON.stringify { mtype: type, payload: opts } self.socket.send JSON.stringify { mtype: type, payload: opts } self.get-token = (login, password) -> self.send request-types[\get-token], JSON.stringify { login: login password: password } self.get-user-by-credentials = (login, password) -> self.send request-types[\get-user-by-credentials], JSON.stringify { login: login password: password } self.login = (login, password) -> self.get-token login, password self.get-user-by-credentials login, password self.get-user = (uid) -> self.send request-types[\get-user], JSON.stringify { uid: uid } 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" } # TODO: authd overhaul #self.add-user = (login, password) -> # self.send request-types[\add-user], JSON.stringify { # login: login # password: password # } # TODO: authd overhaul #self.mod-user = (uid) -> # self.send request-types[\mod-user], JSON.stringify { # uid: uid # } self login-page: (model) -> # XXX: container = marge # XXX: box = dessiner un contour 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) -> # not to refresh the page since it's a form button e.prevent-default! model.authd-ws.get-token model.login, model.password } [ "Connexion" ] ] ] ] }