Skip to content

Instantly share code, notes, and snippets.

@chchrist
Last active February 29, 2016 17:38
Show Gist options
  • Save chchrist/1b7d2cd2c6ed03b0b185 to your computer and use it in GitHub Desktop.
Save chchrist/1b7d2cd2c6ed03b0b185 to your computer and use it in GitHub Desktop.
cometd postpone handshake
'use strict';
import $ from 'jquery';
class AuthenticatorExt {
constructor(options) {
this.tokenUrl = options.tokenUrl;
this.memberId = options.memberId;
this.isPendingHanshake = false;
}
registered(name, cometd) {
this.cometd = cometd;
}
incoming(message) {
if (message.channel === '/meta/handshake' && this.isPendingHanshake) {
this.isPendingHanshake = false;
}
}
outgoing(message) {
if (message.channel === '/meta/handshake' && !this.isPendingHanshake) {
this.isPendingHanshake = true;
this._getToken().then(token => {
this.cometd.handshake({
ext: {
authentication: {
memberId: this.memberId,
token: token,
}
}
})
}).catch(res => {
//TODO something meaningful
})
return false;
}
}
_getToken() {
return new Promise((resolve, reject) => {
$.ajax({
url: this.tokenUrl,
xhrFields: {
withCredentials: true
}
}).then(token => {
resolve(token);
}).fail(e => {
reject(e);
});
});
}
}
export default AuthenticatorExt;
@sbordet
Copy link

sbordet commented Feb 29, 2016

Line 18: perhaps you should check also for message.successful ?

Line 38: you must return null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment