Verify token is invalidated and forget it
This commit is contained in:
parent
4438a027ec
commit
013ae64b88
2 changed files with 31 additions and 4 deletions
6
ajax.js
6
ajax.js
|
@ -1,9 +1,9 @@
|
||||||
const ajax = (function() { "use strict";
|
const ajax = (function() { "use strict";
|
||||||
|
|
||||||
class AjaxError extends Error {
|
class AjaxError extends Error {
|
||||||
constructor (message, response, ...rest) {
|
constructor (message, xhr, ...rest) {
|
||||||
super(message, ...rest);
|
super(message, ...rest);
|
||||||
this.response = response;
|
this.xhr = xhr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class NetworkError extends AjaxError {}
|
class NetworkError extends AjaxError {}
|
||||||
|
@ -61,7 +61,7 @@ function xhrParseJsonResponse(xhr) {
|
||||||
if (xhr.status === 0) {
|
if (xhr.status === 0) {
|
||||||
throw new NetworkError("Failed to connect to server. Developer console may have more information", xhr);
|
throw new NetworkError("Failed to connect to server. Developer console may have more information", xhr);
|
||||||
} else {
|
} else {
|
||||||
throw new NotOkError(xhr.statusText, xhr);
|
throw new NotOkError(`${xhr.status} ${xhr.statusText}`, xhr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
29
main.js
29
main.js
|
@ -93,7 +93,24 @@ class MattermostClient {
|
||||||
throw Error("No token stored");
|
throw Error("No token stored");
|
||||||
}
|
}
|
||||||
const response = await this.api.post("/users/logout", undefined, stored.token);
|
const response = await this.api.post("/users/logout", undefined, stored.token);
|
||||||
//this.storage.clear(this.api.id);
|
|
||||||
|
// Verify that the token is now invalidated
|
||||||
|
let tokenWorks;
|
||||||
|
try {
|
||||||
|
const meResponse = await this.usersMe();
|
||||||
|
tokenWorks = true;
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof ajax.NotOkError && e.xhr.status == 401) {
|
||||||
|
tokenWorks = false;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tokenWorks) {
|
||||||
|
throw new Error("Failed to log out: token still works after trying to log out");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.storage.clear(this.api.id);
|
||||||
return response.responseJson;
|
return response.responseJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +122,16 @@ class MattermostClient {
|
||||||
const response = await this.api.get("/users/me", stored.token);
|
const response = await this.api.get("/users/me", stored.token);
|
||||||
return response.responseJson;
|
return response.responseJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async myTeams() {
|
||||||
|
const response = await this.api.get("/users/me/teams", stored.token);
|
||||||
|
return response.responseJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
async myChannels(team_id) {
|
||||||
|
const response = await this.api.get(`/users/me/teams/${team_id}/channels`, stored.token);
|
||||||
|
return response.responseJson;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue