1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-12-25 00:48:51 -05:00

Add apps oauth login, handle connection errors

This commit is contained in:
Lance Stout 2013-09-12 21:28:59 -07:00
parent baf25a6bc8
commit 53950d9320
11 changed files with 87 additions and 13 deletions

View File

@ -50,6 +50,7 @@ module.exports = {
self.api.connect(); self.api.connect();
self.api.once('session:started', function () { self.api.once('session:started', function () {
app.hasConnected = true;
cb(); cb();
}); });
}, },

View File

@ -80,6 +80,9 @@ module.exports = function (client, app) {
client.on('disconnected', function () { client.on('disconnected', function () {
me.connected = false; me.connected = false;
if (!app.hasConnected) {
window.location = '/login';
}
}); });
client.on('auth:failed', function () { client.on('auth:failed', function () {

View File

@ -4979,19 +4979,31 @@ WSConnection.prototype.connect = function (opts) {
self.parser = new DOMParser(); self.parser = new DOMParser();
self.serializer = new XMLSerializer(); self.serializer = new XMLSerializer();
try {
self.conn = new WebSocket(opts.wsURL, 'xmpp'); self.conn = new WebSocket(opts.wsURL, 'xmpp');
self.conn.onerror = function (e) {
self.conn.onopen = function () { e.preventDefault();
self.emit('connected', self); console.log(e);
self.emit('disconnected', self);
return false;
}; };
self.conn.onclose = function () { self.conn.onclose = function () {
self.emit('disconnected', self); self.emit('disconnected', self);
}; };
self.conn.onopen = function () {
self.emit('connected', self);
};
self.conn.onmessage = function (wsMsg) { self.conn.onmessage = function (wsMsg) {
self.emit('raw:incoming', wsMsg.data); self.emit('raw:incoming', wsMsg.data);
}; };
} catch (e) {
console.log('Caught exception');
return self.emit('disconnected', self);
}
}; };
WSConnection.prototype.disconnect = function () { WSConnection.prototype.disconnect = function () {

View File

@ -1,10 +1,16 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>OTalk</title>
<link rel="stylesheet" type="text/css" href="#{cssFileName}" /> <link rel="stylesheet" type="text/css" href="#{cssFileName}" />
<script src="#{jsFileName}"></script> <script src="#{jsFileName}"></script>
</head> </head>
<body> <body class="aux">
<p>Connecting</p> <header>
<img id="logo" src="/logo.png" alt="OTalk" />
</header>
<section id="loginbox" class="content">
<h2>Connecting...</h2>
</section>
</body> </body>
</html> </html>

View File

@ -16,6 +16,7 @@ module.exports = HumanView.extend({
}, },
render: function () { render: function () {
$('head').append(templates.head()); $('head').append(templates.head());
$('body').removeClass('aux');
this.renderAndBind(); this.renderAndBind();
this.renderCollection(me.contacts, ContactListItem, this.$('#contactList')); this.renderCollection(me.contacts, ContactListItem, this.$('#contactList'));
return this; return this;

30
public/oauthLogin.js Normal file
View File

@ -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';
}
});
}
});

View File

@ -40,6 +40,7 @@ nav.main {
margin: 0px; margin: 0px;
text-align: center; text-align: center;
box-sizing: border-box; box-sizing: border-box;
-moz-box-sizing: border-box;
position: fixed; position: fixed;
bottom: 0px; bottom: 0px;
left: 0px; left: 0px;
@ -64,6 +65,7 @@ nav.main a {
font-size: 12px; font-size: 12px;
cursor: pointer; cursor: pointer;
box-sizing: border-box; box-sizing: border-box;
-moz-box-sizing: border-box;
} }
#contactList { #contactList {
@ -186,6 +188,7 @@ nav.main a {
#conversation { #conversation {
background: #ecf0f2; background: #ecf0f2;
box-sizing: border-box; box-sizing: border-box;
-moz-box-sizing: border-box;
bottom: 0; bottom: 0;
margin: 0; margin: 0;
padding-bottom: 30px; padding-bottom: 30px;
@ -363,12 +366,13 @@ nav.main a {
border: 1px solid #eeeeee; border: 1px solid #eeeeee;
color: #2e2d2d; color: #2e2d2d;
box-sizing: border-box; box-sizing: border-box;
-moz-box-sizing: border-box;
} }
#loginbox input:focus { #loginbox input:focus {
border: 1px solid #a7d9eb; border: 1px solid #a7d9eb;
outline: 0px; outline: 0px;
} }
#loginbox button { #loginbox button, .andyetLogin {
text-decoration: none; text-decoration: none;
text-align: center; text-align: center;
border-radius: 3px; border-radius: 3px;
@ -385,6 +389,11 @@ nav.main a {
padding-bottom: 10px; padding-bottom: 10px;
border-bottom: 1px solid #f8f8f8 border-bottom: 1px solid #f8f8f8
} }
.andyetLogin {
display: block;
float: right;
font-size: 14px;
}
.aux header { .aux header {
margin-top: 10%; margin-top: 10%;
text-align: center; text-align: center;

View File

@ -48,6 +48,12 @@ app.get('/login', function (req, res) {
app.get('/logout', function (req, res) { app.get('/logout', function (req, res) {
res.render('logout'); 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 // serves app on every other url
app.get('*', clientApp.html()); app.get('*', clientApp.html());

View File

@ -13,4 +13,5 @@ html
block content block content
script(src='/zepto.js') script(src='/zepto.js')
script(src='//static.andyet.com/tag.js')
block scripts block scripts

View File

@ -2,6 +2,7 @@ extends layout
block content block content
section#loginbox.content section#loginbox.content
a.andyetLogin(href="/oauth/login") have an &amp;yet account?
h2 Log in h2 Log in
form form
.fieldContainer .fieldContainer

4
views/oauthLogin.jade Normal file
View File

@ -0,0 +1,4 @@
extends layout
block scripts
script(src="/oauthLogin.js")