diff --git a/clientapp/app.js b/clientapp/app.js index e182f13..cae061a 100644 --- a/clientapp/app.js +++ b/clientapp/app.js @@ -50,6 +50,7 @@ module.exports = { self.api.connect(); self.api.once('session:started', function () { + app.hasConnected = true; cb(); }); }, diff --git a/clientapp/helpers/xmppEventHandlers.js b/clientapp/helpers/xmppEventHandlers.js index fa13c4e..1bc9d3c 100644 --- a/clientapp/helpers/xmppEventHandlers.js +++ b/clientapp/helpers/xmppEventHandlers.js @@ -80,6 +80,9 @@ module.exports = function (client, app) { client.on('disconnected', function () { me.connected = false; + if (!app.hasConnected) { + window.location = '/login'; + } }); client.on('auth:failed', function () { diff --git a/clientapp/libraries/stanza.io.js b/clientapp/libraries/stanza.io.js index 0a40395..008ea47 100644 --- a/clientapp/libraries/stanza.io.js +++ b/clientapp/libraries/stanza.io.js @@ -4979,19 +4979,31 @@ WSConnection.prototype.connect = function (opts) { self.parser = new DOMParser(); self.serializer = new XMLSerializer(); - self.conn = new WebSocket(opts.wsURL, 'xmpp'); + try { + self.conn = new WebSocket(opts.wsURL, 'xmpp'); + self.conn.onerror = function (e) { + e.preventDefault(); + console.log(e); + self.emit('disconnected', self); + return false; + }; - self.conn.onopen = function () { - self.emit('connected', self); - }; + self.conn.onclose = function () { + self.emit('disconnected', self); + }; - self.conn.onclose = function () { - self.emit('disconnected', self); - }; + self.conn.onopen = function () { + self.emit('connected', self); + }; - self.conn.onmessage = function (wsMsg) { - self.emit('raw:incoming', wsMsg.data); - }; + self.conn.onmessage = function (wsMsg) { + self.emit('raw:incoming', wsMsg.data); + }; + } catch (e) { + console.log('Caught exception'); + return self.emit('disconnected', self); + + } }; WSConnection.prototype.disconnect = function () { diff --git a/clientapp/template.html b/clientapp/template.html index c319eb9..a8848a0 100644 --- a/clientapp/template.html +++ b/clientapp/template.html @@ -1,10 +1,16 @@ + OTalk - -

Connecting

+ +
+ +
+
+

Connecting...

+
diff --git a/clientapp/views/main.js b/clientapp/views/main.js index c768275..a67e3ef 100644 --- a/clientapp/views/main.js +++ b/clientapp/views/main.js @@ -16,6 +16,7 @@ module.exports = HumanView.extend({ }, render: function () { $('head').append(templates.head()); + $('body').removeClass('aux'); this.renderAndBind(); this.renderCollection(me.contacts, ContactListItem, this.$('#contactList')); return this; diff --git a/public/oauthLogin.js b/public/oauthLogin.js new file mode 100644 index 0000000..422d2ef --- /dev/null +++ b/public/oauthLogin.js @@ -0,0 +1,30 @@ +var parts = window.location.hash.slice(1).split('&'); + +parts.forEach(function (value) { + if (value.substr(0, 12) === "access_token") { + var token = value.substr(13); + $.ajax({ + type: 'get', + url: 'https://api.andbang.com/me', + dataType: 'json', + headers: { + 'Authorization': 'Bearer ' + token + }, + success: function (user) { + localStorage.config = JSON.stringify({ + jid: user.username.toLowerCase() + "@otalk.im", + server: "otalk.im", + wsURL: "wss://otalk.im/xmpp-websocket", + credentials: { + username: user.username.toLowerCase(), + password: token + } + }); + window.location = '/'; + }, + error: function () { + window.location = '/logout'; + } + }); + } +}); diff --git a/public/style.css b/public/style.css index 6c5c380..05ffaf2 100644 --- a/public/style.css +++ b/public/style.css @@ -40,6 +40,7 @@ nav.main { margin: 0px; text-align: center; box-sizing: border-box; + -moz-box-sizing: border-box; position: fixed; bottom: 0px; left: 0px; @@ -64,6 +65,7 @@ nav.main a { font-size: 12px; cursor: pointer; box-sizing: border-box; + -moz-box-sizing: border-box; } #contactList { @@ -186,6 +188,7 @@ nav.main a { #conversation { background: #ecf0f2; box-sizing: border-box; + -moz-box-sizing: border-box; bottom: 0; margin: 0; padding-bottom: 30px; @@ -363,12 +366,13 @@ nav.main a { border: 1px solid #eeeeee; color: #2e2d2d; box-sizing: border-box; + -moz-box-sizing: border-box; } #loginbox input:focus { border: 1px solid #a7d9eb; outline: 0px; } -#loginbox button { +#loginbox button, .andyetLogin { text-decoration: none; text-align: center; border-radius: 3px; @@ -385,6 +389,11 @@ nav.main a { padding-bottom: 10px; border-bottom: 1px solid #f8f8f8 } +.andyetLogin { + display: block; + float: right; + font-size: 14px; +} .aux header { margin-top: 10%; text-align: center; diff --git a/server.js b/server.js index ec8657b..7b60755 100644 --- a/server.js +++ b/server.js @@ -48,6 +48,12 @@ app.get('/login', function (req, res) { app.get('/logout', function (req, res) { res.render('logout'); }); +app.get('/oauth/login', function (req, res) { + res.redirect('https://apps.andyet.com/oauth/authorize?client_id=' + config.andyetAuth.id + '&response_type=token'); +}); +app.get('/oauth/callback', function (req, res) { + res.render('oauthLogin'); +}); // serves app on every other url app.get('*', clientApp.html()); diff --git a/views/layout.jade b/views/layout.jade index c419189..02eea87 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -13,4 +13,5 @@ html block content script(src='/zepto.js') + script(src='//static.andyet.com/tag.js') block scripts diff --git a/views/login.jade b/views/login.jade index e72db46..ca6fe05 100644 --- a/views/login.jade +++ b/views/login.jade @@ -2,6 +2,7 @@ extends layout block content section#loginbox.content + a.andyetLogin(href="/oauth/login") have an &yet account? h2 Log in form .fieldContainer diff --git a/views/oauthLogin.jade b/views/oauthLogin.jade new file mode 100644 index 0000000..80aedb6 --- /dev/null +++ b/views/oauthLogin.jade @@ -0,0 +1,4 @@ +extends layout + +block scripts + script(src="/oauthLogin.js")