mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +01:00
Allow tokens to be used for login, fixes #159
This commit is contained in:
@@ -79,6 +79,19 @@ export default class Client extends EventEmitter {
|
|||||||
this.internal.userAgent = userAgent;
|
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
|
// def login
|
||||||
login(email, password, callback = (/*err, token*/) => { }) {
|
login(email, password, callback = (/*err, token*/) => { }) {
|
||||||
return this.internal.login(email, password)
|
return this.internal.login(email, password)
|
||||||
|
|||||||
@@ -143,7 +143,13 @@ export default class InternalClient {
|
|||||||
|
|
||||||
if(this.client.options.revive && !forced){
|
if(this.client.options.revive && !forced){
|
||||||
this.setup();
|
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");
|
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
|
// def login
|
||||||
login(email, password) {
|
login(email, password) {
|
||||||
var client = this.client;
|
var client = this.client;
|
||||||
@@ -310,18 +331,7 @@ export default class InternalClient {
|
|||||||
var tk = this.tokenCacher.getToken(email, password);
|
var tk = this.tokenCacher.getToken(email, password);
|
||||||
if( tk ){
|
if( tk ){
|
||||||
this.client.emit("debug", "bypassed direct API login, used cached token");
|
this.client.emit("debug", "bypassed direct API login, used cached token");
|
||||||
this.state = ConnectionState.LOGGED_IN;
|
return loginWithToken(tk, email, password);
|
||||||
this.token = tk;
|
|
||||||
this.email = email;
|
|
||||||
this.password = password;
|
|
||||||
|
|
||||||
return this.getGateway()
|
|
||||||
.then(url => {
|
|
||||||
this.createWS(url);
|
|
||||||
return tk;
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.resolve(tk);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,16 +349,7 @@ export default class InternalClient {
|
|||||||
this.client.emit("debug", "direct API login, cached token was unavailable");
|
this.client.emit("debug", "direct API login, cached token was unavailable");
|
||||||
var token = res.token;
|
var token = res.token;
|
||||||
this.tokenCacher.setToken(email, password, token);
|
this.tokenCacher.setToken(email, password, token);
|
||||||
this.state = ConnectionState.LOGGED_IN;
|
loginWithToken(token, email, password);
|
||||||
this.token = token;
|
|
||||||
this.email = email;
|
|
||||||
this.password = password;
|
|
||||||
|
|
||||||
return this.getGateway()
|
|
||||||
.then(url => {
|
|
||||||
this.createWS(url);
|
|
||||||
return token;
|
|
||||||
});
|
|
||||||
}, error => {
|
}, error => {
|
||||||
this.websocket = null;
|
this.websocket = null;
|
||||||
throw error;
|
throw error;
|
||||||
@@ -955,6 +956,9 @@ export default class InternalClient {
|
|||||||
|
|
||||||
//def updateDetails
|
//def updateDetails
|
||||||
updateDetails(data) {
|
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, {
|
return this.apiRequest("patch", Endpoints.ME, true, {
|
||||||
avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar,
|
avatar: this.resolver.resolveToBase64(data.avatar) || this.user.avatar,
|
||||||
email: data.email || this.email,
|
email: data.email || this.email,
|
||||||
|
|||||||
Reference in New Issue
Block a user