Implement login/logout
Added API calls and UI events to handle logging in and logging out
+var baseURL = '';
+
enyo.kind({
name: "blerg.API",
+ loggedIn: false,
+ username: "",
create: function() {
this.inherited(arguments);
+ if (enyo.getCookie('auth') && enyo.getCookie('username')) {
+ this.loggedIn = true;
+ this.username = enyo.getCookie('username');
+ // Defer the signal until everything's initialized
+ setTimeout(function() {
+ this.bubble('onLoginSuccessful', {username: this.username});
+ }.bind(this), 0);
+ }
+ },
+ login: function(username, password) {
+ var req = new enyo.Ajax({
+ url: baseURL + '/login',
+ method: 'POST'
+ });
+ req.response(function(inSender, inResponse) {
+ if (inResponse.status == 'success') {
+ this.loggedIn = true;
+ this.username = username;
+ enyo.setCookie('username', username);
+ this.bubble('onLoginSuccessful', {username: username});
+ } else {
+ enyo.setCookie('username', '', {"Max-Age": 0});
+ this.bubble('onLoginFailed');
+ }
+ }.bind(this));
+ req.go({
+ username: username,
+ password: password
+ });
+ },
+ logout: function() {
+ var req = new enyo.Ajax({
+ url: baseURL + '/logout',
+ method: 'POST'
+ });
+ req.response(function(inSender, inResponse) {
+ this.loggedIn = false;
+ enyo.setCookie('auth', '', {"Max-Age": 0});
+ this.bubble('onLogoutSuccessful');
+ }.bind(this));
+ req.go({
+ username: this.username
+ });
+ enyo.setCookie('username', '', {"Max-Age": 0});
+ },
+ requestFeedStatus: function() {
+ if (!this.loggedIn)
+ throw new Error('Cannot request feed status when not logged in');
+ // TODO
}
});
handlers: {
onStartSignup: "showSignupDialog",
onTryLogin: "tryLogin",
+ onTryLogout: "tryLogout",
onSetTitle: "setTitle"
},
components: [
{classes: "blerg-header", components: [
- {kind: "blerg.Title"},
- {kind: "blerg.Controls"},
+ {name: "title", kind: "blerg.Title"},
+ {name: "controls", kind: "blerg.Controls"},
{style: "clear: both"},
{name: "post", kind: "blerg.Post", showing: false},
{name: "help", kind: "blerg.Help"}
]},
{name: "main", kind: "blerg.Main"},
{name: "signupDialog", kind: "blerg.SignupDialog"},
- {name: "passwdDialog", kind: "blerg.PasswdDialog"}
+ {name: "passwdDialog", kind: "blerg.PasswdDialog"},
+ {name: "api", kind: "blerg.API",
+ onLoginSuccessful: "loginSuccessful",
+ onLoginFailed: "loginFailed",
+ onLogoutSuccessful: "logout"}
],
urlmap: [
['search', /^\?post\/([^/]+)\/(.+)/, "blerg.ExternalURLPost"],
this.$.title.hideControls();
},
tryLogin: function(inSender, inEvent) {
+ this.$.api.login(inEvent.username, inEvent.password);
+ },
+ tryLogout: function(inSender, inEvent) {
+ this.$.api.logout();
+ },
+ loginSuccessful: function(inSender, inEvent) {
+ this.$.api.requestFeedStatus();
+ this.feedStatusUpdateInterval = setInterval(function() {
+ this.$.api.requestFeedStatus();
+ }.bind(this), 900000);
+ this.waterfall('onLogin', inEvent);
+ },
+ loginFailed: function(inSender, inEvent) {
+ alert('Login failed');
+ this.logout();
+ },
+ logout: function(inSender, inEvent) {
+ clearInterval(this.feedStatusUpdateInterval);
+ this.waterfall('onLogout');
}
});
kind: "Control",
style: "float: right",
classes: "blerg-controls",
+ username: null,
published: {
loggedIn: false
},
+ handlers: {
+ onLogin: "login",
+ onLogout: "logout"
+ },
components: [
{name: "loggedOutControls", components: [
- {tag: "form", onsubmit: "doLogin", classes: "login", components: [
+ {tag: "form", onsubmit: "loginClicked", classes: "login", components: [
{kind: "onyx.Groupbox", components: [
{kind: "onyx.InputDecorator", components: [
{name: "username", kind: "onyx.Input", placeholder: "Username"}
{name: "password", kind: "onyx.Input", placeholder: "Password", type: "password"}
]},
]},
- {kind: "onyx.Button", content: "Login", onclick: "doLogin"}
+ {kind: "onyx.Button", content: "Login", onclick: "loginClicked"}
]}
]},
{name: "loggedInControls", showing: false, components: [
- {name: "greeting"},
- {kind: "onyx.Toolbar", components: [
- {kind: "onyx.Button", content: "Write", onClick: "writeClicked"},
- {kind: "onyx.Button", content: "Hearsay", onClick: "chatterClicked"},
- {kind: "onyx.Button", content: "Stalking", onClick: "feedClicked"}
+ {name: "greeting", components: [
+ {noDom: true, content: "Hello, "},
+ {name: "userlink", tag: "a"},
+ {noDom: true, content: "."}
+ ]},
+ {classes: "onyx-toolbar-inline", components: [
+ {kind: "onyx.Button", content: "Write", onclick: "writeClicked"},
+ {kind: "onyx.Button", content: "Hearsay", onclick: "chatterClicked"},
+ {kind: "onyx.Button", content: "Stalking", onclick: "feedClicked"},
+ {kind: "onyx.Button", content: "Logout", onclick: "logoutClicked"}
]},
{components: [
{name: "rssButton", showing: false, kind: "blerg.Link", components: [
this.$.loggedInControls.hide();
}
},
- doLogin: function() {
+ loginClicked: function(inSender, inEvent) {
this.bubble('onTryLogin', {
username: this.$.username.getValue(),
password: this.$.password.getValue()
});
+ inEvent.preventDefault();
+ return true;
+ },
+ logoutClicked: function() {
+ this.bubble('onTryLogout');
+ },
+ login: function(inSender, inEvent) {
+ this.$.password.setValue('');
+ this.setLoggedIn(true);
+ this.$.userlink.setAttribute('href', '/#' + inEvent.username);
+ this.$.userlink.setContent('@' + inEvent.username);
+ this.username = inEvent.username;
+ },
+ logout: function(inSender, inEvent) {
+ this.setLoggedIn(false);
}
});