2015-12-01 21:06:55 +01:00
|
|
|
extends layout
|
|
|
|
|
|
|
|
block content
|
2016-07-21 17:14:59 +02:00
|
|
|
script(src="https://cdn.auth0.com/js/lock/10.0/lock.min.js")
|
2016-01-07 15:44:05 +01:00
|
|
|
script(src="/socket.io/socket.io.js")
|
2016-06-08 21:13:53 +02:00
|
|
|
script(src="http://code.jquery.com/jquery-1.12.4.js")
|
2015-12-01 21:06:55 +01:00
|
|
|
|
|
|
|
|
2016-01-07 15:44:05 +01:00
|
|
|
div#login
|
|
|
|
button.btn Login
|
|
|
|
div#chat
|
|
|
|
ul#messages
|
2016-07-11 22:07:28 +02:00
|
|
|
form(id="chatForm" action="")
|
2016-01-07 15:44:05 +01:00
|
|
|
input(id="m" autocomplete="off")
|
|
|
|
button.btn Send
|
|
|
|
|
|
|
|
script.
|
|
|
|
var userProfile;
|
|
|
|
var userToken;
|
|
|
|
var lock = new Auth0Lock('#{env.AUTH0_CLIENT_ID}', '#{env.AUTH0_DOMAIN}');
|
2016-07-11 22:07:28 +02:00
|
|
|
var id_token = localStorage.getItem('id_token');
|
2016-01-07 15:44:05 +01:00
|
|
|
$('#chat').hide();
|
|
|
|
$('#login button').click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
lock.show();
|
|
|
|
});
|
|
|
|
|
2016-07-11 22:07:28 +02:00
|
|
|
lock.on('authenticated', function(authResult) {
|
|
|
|
lock.getProfile(authResult.idToken, function(error, profile) {
|
|
|
|
if (error) {
|
|
|
|
// Handle error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('connected and authenticated');
|
|
|
|
localStorage.setItem('id_token', authResult.idToken);
|
|
|
|
localStorage.setItem('profile', JSON.stringify(profile));
|
|
|
|
userProfile = profile;
|
|
|
|
id_token = authResult.idToken;
|
|
|
|
openChat();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (id_token) {
|
|
|
|
lock.getProfile(id_token, function (err, profile) {
|
|
|
|
if (err) {
|
|
|
|
return alert('There was an error getting the profile: ' + err.message);
|
|
|
|
}
|
|
|
|
userProfile = profile;
|
|
|
|
openChat();
|
|
|
|
});
|
2016-01-07 15:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function openChat() {
|
|
|
|
var socket = io();
|
2016-04-16 09:00:05 +02:00
|
|
|
socket
|
|
|
|
.on('connect', function (msg) {
|
|
|
|
console.log("connected");
|
2016-07-11 22:07:28 +02:00
|
|
|
socket.emit('authenticate', {token: id_token}); // send the jwt
|
2016-04-16 09:00:05 +02:00
|
|
|
})
|
|
|
|
.on('authenticated', function () {
|
2016-01-07 15:44:05 +01:00
|
|
|
//Do
|
|
|
|
$('#login').hide();
|
|
|
|
$('#chat').show();
|
|
|
|
$('form').submit(function (event) {
|
|
|
|
socket.emit('chat message', $('#m').val());
|
|
|
|
$('#m').val('');
|
|
|
|
return false;
|
|
|
|
});
|
2016-04-16 09:00:05 +02:00
|
|
|
})
|
|
|
|
.on('unauthorized', function(msg){
|
|
|
|
console.log("unauthorized: " + JSON.stringify(msg.data));
|
|
|
|
throw new Error(msg.data.type);
|
|
|
|
})
|
|
|
|
.on('chat message', function (msg) {
|
|
|
|
console.log("msg");
|
|
|
|
$('#messages').append($('<li>').text(msg));
|
|
|
|
});
|
2016-01-07 15:44:05 +01:00
|
|
|
};
|