Allow tokens to be used for login, fixes #159

This commit is contained in:
meew0
2016-01-24 14:33:39 +01:00
parent 76ab439033
commit 3caf1f74cc
2 changed files with 40 additions and 23 deletions

View File

@@ -79,6 +79,19 @@ export default class Client extends EventEmitter {
this.internal.userAgent = userAgent;
}
// def loginWithToken
loginWithToken(token, email = null, password = null, callback = (/*err, token*/) => {}) {
if (typeof email === "function") {
// email is the callback
callback = email;
email = null;
password = null;
}
return this.internal.loginWithToken(token, email, password)
.then(dataCallback(callback), errorCallback(callback));
}
// def login
login(email, password, callback = (/*err, token*/) => { }) {
return this.internal.login(email, password)

View File

@@ -143,7 +143,13 @@ export default class InternalClient {
if(this.client.options.revive && !forced){
this.setup();
this.login(this.email, this.password);
// Check whether the email is set (if not, only a token has been used for login)
if(this.email) {
this.login(this.email, this.password);
} else {
this.loginWithToken(this.token);
}
}
this.client.emit("disconnected");
@@ -296,6 +302,21 @@ export default class InternalClient {
});
}
// def loginWithToken
// email and password are optional
loginWithToken(token, email, password) {
this.state = ConnectionState.LOGGED_IN;
this.token = token;
this.email = email;
this.password = password;
return this.getGateway()
.then(url => {
this.createWS(url);
return token;
});
}
// def login
login(email, password) {
var client = this.client;
@@ -310,18 +331,7 @@ export default class InternalClient {
var tk = this.tokenCacher.getToken(email, password);
if( tk ){
this.client.emit("debug", "bypassed direct API login, used cached token");
this.state = ConnectionState.LOGGED_IN;
this.token = tk;
this.email = email;
this.password = password;
return this.getGateway()
.then(url => {
this.createWS(url);
return tk;
});
return Promise.resolve(tk);
return loginWithToken(tk, email, password);
}
}
@@ -339,16 +349,7 @@ export default class InternalClient {
this.client.emit("debug", "direct API login, cached token was unavailable");
var token = res.token;
this.tokenCacher.setToken(email, password, token);
this.state = ConnectionState.LOGGED_IN;
this.token = token;
this.email = email;
this.password = password;
return this.getGateway()
.then(url => {
this.createWS(url);
return token;
});
loginWithToken(token, email, password);
}, error => {
this.websocket = null;
throw error;
@@ -955,6 +956,9 @@ export default class InternalClient {
//def updateDetails
updateDetails(data) {
if(!email) {
throw new Error("Can't use updateDetails because only a token has been used for login!");
}
return this.apiRequest("patch", Endpoints.ME, true, {
avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar,
email: data.email || this.email,