180 lines
6.0 KiB
JavaScript
180 lines
6.0 KiB
JavaScript
// Generated by LiveScript 1.6.0
|
||
(function(){
|
||
var maquette, h, bulma, AuthWS, LoginForm;
|
||
maquette = require("maquette");
|
||
h = maquette.h;
|
||
bulma = require("./bulma.ls");
|
||
AuthWS = require("./authws.ls");
|
||
LoginForm = function(args){
|
||
var self, authWs;
|
||
args || (args = {});
|
||
self = {
|
||
onLogin: args.onLogin || function(){},
|
||
onError: args.onError || function(){},
|
||
scheduleRender: args.scheduleRender || function(){},
|
||
currentView: "login",
|
||
enableRegistration: args.enableRegistration || false,
|
||
registrating: false,
|
||
input: {
|
||
login: "",
|
||
password: "",
|
||
repeatPassword: ""
|
||
},
|
||
lockedInput: true,
|
||
error: void 8,
|
||
authwsUrl: args.authwsUrl || (location.protocol === 'https' ? 'wss' : 'ws') + '://' + location.hostname + ":9999/auth.JSON"
|
||
};
|
||
authWs = AuthWS(self.authwsUrl);
|
||
authWs.socket.onopen = function(){
|
||
console.log("socket is open");
|
||
self.lockedInput = false;
|
||
return self.scheduleRender();
|
||
};
|
||
authWs.userOnSocketError.push(function(){
|
||
self.error = "socket error";
|
||
return self.onError.apply(this, arguments);
|
||
});
|
||
authWs.addEventListener('token', function(message){
|
||
self.error = void 8;
|
||
self.token = message.token;
|
||
self.lockedInput = false;
|
||
if (self.user) {
|
||
return self.onLogin(self.user, self.token);
|
||
}
|
||
});
|
||
authWs.addEventListener('user', function(message){
|
||
self.error = void 8;
|
||
self.user = message.user;
|
||
if (self.token) {
|
||
return self.onLogin(self.user, self.token);
|
||
}
|
||
});
|
||
authWs.addEventListener('user-added', function(message){
|
||
var ref$, login, password;
|
||
ref$ = {
|
||
login: self.input.login,
|
||
password: self.input.password
|
||
}, login = ref$.login, password = ref$.password;
|
||
console.log("user added, duh");
|
||
self.user = message.user;
|
||
return authWs.getToken(login, password);
|
||
});
|
||
authWs.addEventListener('error', function(message){
|
||
if (message.reason === "user not found") {
|
||
return;
|
||
}
|
||
self.error = message.reason;
|
||
self.lockedInput = false;
|
||
return self.onError(message.reason);
|
||
});
|
||
self.render = function(){
|
||
if (self.error === "socket error") {
|
||
return h('div.notification.is-danger', [h('div.title.is-4', ["WebSocket error!"]), h('p', ["Cannot connect to authd."])]);
|
||
}
|
||
return h('form.form.login-form', {
|
||
key: self,
|
||
onsubmit: function(e){
|
||
var ref$, login, password;
|
||
ref$ = {
|
||
login: self.input.login,
|
||
password: self.input.password
|
||
}, login = ref$.login, password = ref$.password;
|
||
self.lockedInput = true;
|
||
if (self.registrating) {
|
||
authWs.register(login, password);
|
||
} else {
|
||
authWs.getToken(login, password);
|
||
authWs.getUserByCredentials(login, password);
|
||
}
|
||
return e.preventDefault();
|
||
}
|
||
}, [
|
||
h('div.field', {
|
||
key: 'login'
|
||
}, [
|
||
bulma.label("Login"), bulma.input({
|
||
type: "text",
|
||
id: "login",
|
||
name: "login",
|
||
classes: {
|
||
"is-danger": self.error === "invalid credentials"
|
||
},
|
||
disabled: self.lockedInput,
|
||
oninput: function(e){
|
||
return self.input.login = e.target.value;
|
||
}
|
||
})
|
||
]), h('div.field', {
|
||
key: 'password'
|
||
}, [
|
||
bulma.label("Password"), bulma.input({
|
||
type: "password",
|
||
id: "password",
|
||
name: "password",
|
||
classes: {
|
||
"is-danger": self.error === "invalid credentials"
|
||
},
|
||
oninput: function(e){
|
||
return self.input.password = e.target.value;
|
||
},
|
||
disabled: self.lockedInput
|
||
})
|
||
]), self.registrating ? h('div.field', {
|
||
key: 'password-repeat'
|
||
}, [
|
||
bulma.label("Password (reapeat)"), bulma.input({
|
||
type: 'password',
|
||
id: 'password-repeat',
|
||
name: 'password-repeat',
|
||
classes: {
|
||
"is-danger": self.input.password !== self.input.repeatPassword
|
||
},
|
||
disabled: self.lockedInput,
|
||
oninput: function(e){
|
||
return self.input.repeatPassword = e.target.value;
|
||
}
|
||
})
|
||
]) : void 8, self.error ? h('div.field', {
|
||
key: 'error-notification'
|
||
}, [h('div.notification.is-danger', [self.error])]) : void 8, self.registrating
|
||
? h('div.field.is-grouped', {
|
||
key: 'login-button'
|
||
}, [self.input.login === ""
|
||
? h('button.button.is-static.is-fullwidth', {
|
||
type: 'submit'
|
||
}, ["(empty login)"])
|
||
: self.input.password !== self.input.repeatPassword
|
||
? h('button.button.is-static.is-fullwidth', {
|
||
type: 'submit'
|
||
}, ["(passwords don’t match)"])
|
||
: self.input.password === ""
|
||
? h('button.button.is-static.is-fullwidth', {
|
||
type: 'submit'
|
||
}, ["(empty password)"])
|
||
: h('button.button.is-success.is-fullwidth', {
|
||
type: 'submit'
|
||
}, ["Register!"])])
|
||
: h('div.field.is-grouped', {
|
||
key: 'login-button'
|
||
}, [h('button.button.is-fullwidth.is-success', {
|
||
type: 'submit'
|
||
}, ["Log in!"])]), h('div.field.level', {
|
||
key: 'extra-buttons'
|
||
}, [self.enableRegistration ? h('div.level-right', [self.registrating
|
||
? h('a.link', {
|
||
onclick: function(e){
|
||
return self.registrating = false;
|
||
}
|
||
}, ["Log in"])
|
||
: h('a.link', {
|
||
onclick: function(e){
|
||
return self.registrating = true;
|
||
}
|
||
}, ["Create account!"])]) : void 8])
|
||
]);
|
||
};
|
||
return self;
|
||
};
|
||
module.exports = LoginForm;
|
||
}).call(this);
|