Verify token is invalidated and forget it

This commit is contained in:
Midgard 2020-03-26 17:57:42 +01:00
parent 4438a027ec
commit 013ae64b88
Signed by: midgard
GPG Key ID: 511C112F1331BBB4
2 changed files with 31 additions and 4 deletions

View File

@ -1,9 +1,9 @@
const ajax = (function() { "use strict";
class AjaxError extends Error {
constructor (message, response, ...rest) {
constructor (message, xhr, ...rest) {
super(message, ...rest);
this.response = response;
this.xhr = xhr;
}
}
class NetworkError extends AjaxError {}
@ -61,7 +61,7 @@ function xhrParseJsonResponse(xhr) {
if (xhr.status === 0) {
throw new NetworkError("Failed to connect to server. Developer console may have more information", xhr);
} else {
throw new NotOkError(xhr.statusText, xhr);
throw new NotOkError(`${xhr.status} ${xhr.statusText}`, xhr);
}
}

29
main.js
View File

@ -93,7 +93,24 @@ class MattermostClient {
throw Error("No token stored");
}
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;
}
@ -105,6 +122,16 @@ class MattermostClient {
const response = await this.api.get("/users/me", stored.token);
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;
}
}