Changed popup to redirect mode, updated dependencies

This commit is contained in:
Vlad 2016-01-07 16:44:05 +02:00
parent c31764aac3
commit 46c212363f
2 changed files with 63 additions and 62 deletions

View File

@ -7,9 +7,9 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^1.2.0", "dotenv": "^1.2.0",
"express": "^4.10.2", "express": "^4.13.3",
"jade": "^1.11.0", "jade": "^1.11.0",
"socket.io": "^1.3.6", "socket.io": "^1.3.7",
"socketio-jwt": "^4.3.1" "socketio-jwt": "^4.3.3"
} }
} }

View File

@ -1,9 +1,9 @@
extends layout extends layout
block content block content
script(src="https://cdn.auth0.com/js/lock-7.12.js") script(src="https://cdn.auth0.com/js/lock-8.1.1.js")
script(src="/socket.io/socket.io.js") script(src="/socket.io/socket.io.js")
script(src="http://code.jquery.com/jquery-1.11.1.js") script(src="http://code.jquery.com/jquery-1.11.3.js")
div#login div#login
@ -18,48 +18,49 @@ block content
var userProfile; var userProfile;
var userToken; var userToken;
var lock = new Auth0Lock('#{env.AUTH0_CLIENT_ID}', '#{env.AUTH0_DOMAIN}'); var lock = new Auth0Lock('#{env.AUTH0_CLIENT_ID}', '#{env.AUTH0_DOMAIN}');
var hash = lock.parseHash();
$('#chat').hide(); $('#chat').hide();
$('#login button').click(function(e){ $('#login button').click(function (e) {
e.preventDefault(); e.preventDefault();
lock.show(function(err, profile, token) { lock.show();
if (err) {
//Error callback
alert('There was an error');
alert(err);
} else {
console.log('connected and authenticated');
//Success callback
userToken = token;
//Save the JWT token
localStorage.setItem('userToken', token);
//Save the profile
userProfile = profile;
openChat();
}
})
}); });
function openChat(){ if (hash) {
if (hash.error) {
console.log("There was an error logging in", hash.error);
}
else {
lock.getProfile(hash.id_token, function (err, profile) {
if (err) {
console.log('Cannot get user', err);
return;
}
console.log('connected and authenticated');
userProfile = profile;
localStorage.setItem('userToken', hash.id_token);
userToken = hash.id_token;
openChat();
});
}
}
function openChat() {
var socket = io(); var socket = io();
socket.on('connect', function () { socket.on('connect', function () {
socket.on('authenticated', function () { socket.on('authenticated', function () {
//Do //Do
$('#login').hide(); $('#login').hide();
$('#chat').show(); $('#chat').show();
$('form').submit(function(event){ $('form').submit(function (event) {
socket.emit('chat message', $('#m').val()); socket.emit('chat message', $('#m').val());
$('#m').val(''); $('#m').val('');
return false; return false;
}); });
socket.on('chat message', function(msg){ socket.on('chat message', function (msg) {
console.log("msg"); console.log("msg");
$('#messages').append($('<li>').text(msg)); $('#messages').append($('<li>').text(msg));
}); });
}) }).emit('authenticate', {token: userToken}); // send the jwt
.emit('authenticate', {token: userToken}); // send the jwt
}) })
}; };