Skip to content

Commit 3caf1f7

Browse files
committed
Allow tokens to be used for login, fixes #159
1 parent 76ab439 commit 3caf1f7

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/Client/Client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ export default class Client extends EventEmitter {
7979
this.internal.userAgent = userAgent;
8080
}
8181

82+
// def loginWithToken
83+
loginWithToken(token, email = null, password = null, callback = (/*err, token*/) => {}) {
84+
if (typeof email === "function") {
85+
// email is the callback
86+
callback = email;
87+
email = null;
88+
password = null;
89+
}
90+
91+
return this.internal.loginWithToken(token, email, password)
92+
.then(dataCallback(callback), errorCallback(callback));
93+
}
94+
8295
// def login
8396
login(email, password, callback = (/*err, token*/) => { }) {
8497
return this.internal.login(email, password)

src/Client/InternalClient.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ export default class InternalClient {
143143

144144
if(this.client.options.revive && !forced){
145145
this.setup();
146-
this.login(this.email, this.password);
146+
147+
// Check whether the email is set (if not, only a token has been used for login)
148+
if(this.email) {
149+
this.login(this.email, this.password);
150+
} else {
151+
this.loginWithToken(this.token);
152+
}
147153
}
148154

149155
this.client.emit("disconnected");
@@ -296,6 +302,21 @@ export default class InternalClient {
296302
});
297303
}
298304

305+
// def loginWithToken
306+
// email and password are optional
307+
loginWithToken(token, email, password) {
308+
this.state = ConnectionState.LOGGED_IN;
309+
this.token = token;
310+
this.email = email;
311+
this.password = password;
312+
313+
return this.getGateway()
314+
.then(url => {
315+
this.createWS(url);
316+
return token;
317+
});
318+
}
319+
299320
// def login
300321
login(email, password) {
301322
var client = this.client;
@@ -310,18 +331,7 @@ export default class InternalClient {
310331
var tk = this.tokenCacher.getToken(email, password);
311332
if( tk ){
312333
this.client.emit("debug", "bypassed direct API login, used cached token");
313-
this.state = ConnectionState.LOGGED_IN;
314-
this.token = tk;
315-
this.email = email;
316-
this.password = password;
317-
318-
return this.getGateway()
319-
.then(url => {
320-
this.createWS(url);
321-
return tk;
322-
});
323-
324-
return Promise.resolve(tk);
334+
return loginWithToken(tk, email, password);
325335
}
326336
}
327337

@@ -339,16 +349,7 @@ export default class InternalClient {
339349
this.client.emit("debug", "direct API login, cached token was unavailable");
340350
var token = res.token;
341351
this.tokenCacher.setToken(email, password, token);
342-
this.state = ConnectionState.LOGGED_IN;
343-
this.token = token;
344-
this.email = email;
345-
this.password = password;
346-
347-
return this.getGateway()
348-
.then(url => {
349-
this.createWS(url);
350-
return token;
351-
});
352+
loginWithToken(token, email, password);
352353
}, error => {
353354
this.websocket = null;
354355
throw error;
@@ -955,6 +956,9 @@ export default class InternalClient {
955956

956957
//def updateDetails
957958
updateDetails(data) {
959+
if(!email) {
960+
throw new Error("Can't use updateDetails because only a token has been used for login!");
961+
}
958962
return this.apiRequest("patch", Endpoints.ME, true, {
959963
avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar,
960964
email: data.email || this.email,

0 commit comments

Comments
 (0)