Skip to content

Commit

Permalink
♻️ Rewrite authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Androz2091 committed Feb 18, 2021
1 parent e7b2268 commit 1d1fec2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
17 changes: 7 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ export default {
} else {
const startAt = Date.now();
fetchCommands(this.$store.state.clientID, this.$store.state.token.value, this.$store.state.proxyURL, this.$store.state.selectedGuildID).then((commands) => {
if (!commands) {
setTimeout(() => {
this.$store.commit('SET_COMMANDS', commands);
this.loading = false;
this.missingScope = true;
} else {
setTimeout(() => {
this.$store.commit('SET_COMMANDS', commands);
this.loading = false;
}, (Date.now() - startAt) + 100);
}
}).catch(() => {
}, (Date.now() - startAt) + 100);
}).catch((err) => {
console.log(err.statusCode);
this.loading = false;
this.$router.push('/settings');
if (err.statusCode === 403) this.missingScope = true;
else this.$router.push('/settings');
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios from 'axios';
const getURL = (clientID, guildID, commandID) => `applications/${clientID}/${guildID ? `guilds/${guildID}/` : ''}${commandID ? `commands/${commandID}` : 'commands'}`;

export function request (token, proxyURL, url, method, data) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
axios({
url: `${proxyURL}/https://discord.com/api/v8/${url}`,
method,
Expand All @@ -14,8 +14,8 @@ export function request (token, proxyURL, url, method, data) {
}
}).then((value) => {
resolve(value.data);
}).catch(() => {
resolve(null);
}).catch((err) => {
reject(err);
});
});
}
Expand All @@ -28,7 +28,7 @@ export function request (token, proxyURL, url, method, data) {
* @returns {Promise<Object>}
*/
export function getToken (clientID, clientSecret, proxyURL) {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const data = new URLSearchParams();
data.append('grant_type', 'client_credentials');
data.append('scope', 'applications.commands.update');
Expand All @@ -42,8 +42,8 @@ export function getToken (clientID, clientSecret, proxyURL) {
}
}).then((value) => {
resolve(value.data);
}).catch(() => {
resolve(null);
}).catch((err) => {
reject(err);
});
});
}
Expand Down
34 changes: 15 additions & 19 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,23 @@ export default {
proxyURL: this.proxyURL
});
getToken(this.clientID, this.clientSecret, this.proxyURL).then((tokenData) => {
if (!tokenData) {
this.invalidClientCredentials.add(`${this.clientID}${this.clientSecret}`);
this.loading = false;
} else {
this.$store.dispatch('saveToken', {
expiresAt: Date.now() + (tokenData.expires_in * 1000),
value: tokenData.access_token
this.$store.dispatch('saveToken', {
expiresAt: Date.now() + (tokenData.expires_in * 1000),
value: tokenData.access_token
});
fetchApplication(this.$store.state.clientID).then((application) => {
this.$toast.success(`Successfully logged in as ${application.username}!`, {
duration: 10000,
pauseOnHover: true
});
fetchApplication(this.$store.state.clientID).then((application) => {
this.$toast.success(`Successfully logged in as ${application.username}!`, {
duration: 10000,
pauseOnHover: true
});
this.$router.push('/');
});
this.loading = false;
this.$root.loadCommands();
}
}).catch(() => {
this.$router.push('/');
});
this.loading = false;
this.$root.loadCommands();
}).catch((err) => {
this.loading = false;
this.invalidProxyURLs.add(this.proxyURL);
if (!err.response) this.invalidProxyURLs.add(this.proxyURL);
else this.invalidClientCredentials.add(`${this.clientID}${this.clientSecret}`);
});
}
}
Expand Down

0 comments on commit 1d1fec2

Please # to comment.