2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00

Merge pull request #71 from Amialc/popupchange

Changed popup to redirect mode, updated dependencies
This commit is contained in:
Nathan Totten 2016-02-09 10:18:10 -08:00
commit 4712148963
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,67 +1,68 @@
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
button.btn Login button.btn Login
div#chat div#chat
ul#messages ul#messages
form(action="") form(action="")
input(id="m" autocomplete="off") input(id="m" autocomplete="off")
button.btn Send button.btn Send
script. script.
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}');
$('#chat').hide(); var hash = lock.parseHash();
$('#login button').click(function(e){ $('#chat').hide();
e.preventDefault(); $('#login button').click(function (e) {
lock.show(function(err, profile, token) { e.preventDefault();
if (err) { lock.show();
//Error callback });
alert('There was an error');
alert(err);
} else {
console.log('connected and authenticated');
//Success callback
userToken = token;
//Save the JWT token if (hash) {
localStorage.setItem('userToken', token); 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();
});
}
}
//Save the profile function openChat() {
userProfile = profile; var socket = io();
socket.on('connect', function () {
openChat(); socket.on('authenticated', function () {
} //Do
}) $('#login').hide();
}); $('#chat').show();
$('form').submit(function (event) {
function openChat(){ socket.emit('chat message', $('#m').val());
var socket = io(); $('#m').val('');
socket.on('connect', function () { return false;
socket.on('authenticated', function () { });
//Do socket.on('chat message', function (msg) {
$('#login').hide(); console.log("msg");
$('#chat').show(); $('#messages').append($('<li>').text(msg));
$('form').submit(function(event){ });
socket.emit('chat message', $('#m').val()); }).emit('authenticate', {token: userToken}); // send the jwt
$('#m').val(''); })
return false; };
});
socket.on('chat message', function(msg){
console.log("msg");
$('#messages').append($('<li>').text(msg));
});
})
.emit('authenticate', {token: userToken}); // send the jwt
})
};