Fix token validation

This commit is contained in:
Midgard 2020-03-25 22:17:24 +01:00
parent 87808a3ffe
commit 5fd8101224
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 10 additions and 4 deletions

View file

@ -12,8 +12,8 @@
<tr><th>Username</th><td><input type="text" id="username"/></td></tr> <tr><th>Username</th><td><input type="text" id="username"/></td></tr>
<tr><th>Password</th><td><input type="password" id="password"/></td></tr> <tr><th>Password</th><td><input type="password" id="password"/></td></tr>
</table> </table>
<input type="button" onclick="this.disabled = true; this.value = 'Validating token'; validateToken(); return false" id="validate" value="Validate token"/> </input>(ignores username and password) <br/> <input type="button" onclick="this.disabled = true; this.value = 'Logging in...'; logIn(); return false" value="Log in"/><br/>
<input type="button" onclick="this.disabled = true; this.value = 'Logging in...'; logIn(); return false" value="Log in"/> <input type="button" onclick="this.disabled = true; this.value = 'Validating token'; validateToken(); return false" id="validate" value="Validate token"/> </input>(ignores username and password)
</form> </form>
<script type="text/javascript" src="xhr.js"></script> <script type="text/javascript" src="xhr.js"></script>
<script type="text/javascript" src="main.js"></script> <script type="text/javascript" src="main.js"></script>

10
main.js
View file

@ -30,7 +30,7 @@ class MattermostApi {
async validateToken(token) { async validateToken(token) {
const response = await ajax.getJson(`${this.endpoint}/users/me`, { const response = await ajax.getJson(`${this.endpoint}/users/me`, {
headers: { headers: {
"Token": `Authorization: Bearer ${token}` "Authorization": `Bearer ${token}`
} }
}); });
if (!response.ok) { if (!response.ok) {
@ -85,8 +85,14 @@ function logIn() {
function validateToken() { function validateToken() {
let endpoint = normalizedEndpoint(byId("server").value); let endpoint = normalizedEndpoint(byId("server").value);
let cred = getCredentials(endpoint);
if (!cred || !cred.token) {
byId("validate").value = "No token, log in first";
byId("validate").disabled = false;
}
let api = new MattermostApi(endpoint); let api = new MattermostApi(endpoint);
api.validateToken(getCredentials(endpoint)) api.validateToken(cred.token)
.then(() => { .then(() => {
byId("validate").value = "Validation succeeded"; byId("validate").value = "Validation succeeded";
byId("validate").disabled = false; byId("validate").disabled = false;