Skip to content

Commit

Permalink
Fix for auth change
Browse files Browse the repository at this point in the history
  • Loading branch information
LucHeart committed Oct 6, 2023
1 parent 9700c9f commit 1e4904c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
12 changes: 8 additions & 4 deletions src/js/ApiCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ class ApiCall {
return await axios({
method: method,
url: config.apiUrl + path,
data: data
data: data,
headers: {
OpenShockSession: localStorage.getItem("token")
}
});
} catch (err) {
toastr.error(utils.getError(err), "API interaction failed");
if(err.response !== undefined && err.response.status === 401) {
localStorage.removeItem("token");
router.push('/account/login');
utils.setLogin("");
return undefined;
}

throw err;
}
}

async makeCall(method, path, data, headers) {
async makeCallHeaders(method, path, data, headers) {
headers.OpenShockSession = localStorage.getItem("token");
try {
return await axios({
method: method,
Expand All @@ -37,8 +41,8 @@ class ApiCall {
} catch (err) {
toastr.error(utils.getError(err), "API interaction failed");
if(err.response !== undefined && err.response.status === 401) {
localStorage.removeItem("token");
router.push('/account/login');
utils.setLogin("");
return undefined;
}

Expand Down
11 changes: 9 additions & 2 deletions src/js/SlWs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import storeF from '@/store'
import router from '@/router'
import * as signalR from '@microsoft/signalr'


const connection = new signalR.HubConnectionBuilder()
.withUrl(config.apiUrl + "1/hubs/user")
.withUrl(config.apiUrl + "1/hubs/user?session=" + localStorage.getItem("token"))
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect([0, 1000, 2000, 5000, 10000, 10000, 15000, 30000, 60000])
.build();
Expand Down Expand Up @@ -45,7 +47,12 @@ setInterval(() => {
}
}, 200);

connection.start().catch((err) => toastr.error(err, "User Hub"));
connection.start().catch((err) => {
if(err.message && err.message.includes(`Status code '401'`)) {
localStorage.removeItem("token");
router.push('/account/login');
} else toastr.error(err, "User Hub");
});

global.ws = ws;
global.userHubConnection = connection;
23 changes: 15 additions & 8 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ let utils = {
}
return "";
},
setLogin() {
this.setCookie("loggedIn", "true", 7);
},
authExists() {
let user = this.getCookie("loggedIn");
return user === "true";
isLoggedIn() {
const expiresOnRaw = localStorage.getItem("token_validUntil");
if(expiresOnRaw === null) return false;
const tokenExists = localStorage.getItem("token") !== null;
if(!tokenExists) return false;

const asDate = new Date(expiresOnRaw);
if(isNaN(asDate)) return false;

return asDate > Date.now();
},
isDarkMode() {
const cook = this.getCookie("settings_dark");
Expand All @@ -30,7 +34,7 @@ let utils = {
return true;
},
setDarkMode(dark) {
this.setCookie("settings_dark", dark, 3652);
localStorage.setItem("dark", dark);
},
setCookie(cname, cvalue, exdays) {
const d = new Date();
Expand All @@ -56,7 +60,10 @@ let utils = {
try {
const res = await axios({
method: "GET",
url: config.apiUrl + "1/users/self"
url: config.apiUrl + "1/users/self",
headers: {
OpenShockSession: localStorage.getItem("token")
}
});

return res.status === 200;
Expand Down
5 changes: 3 additions & 2 deletions src/views/Login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ export default {
method: 'POST',
url: config.apiUrl + '1/account/login',
data: {
username: this.username,
email: this.username,
password: this.password
}
});
localStorage.setItem("token", res.data.data.sessionToken);
localStorage.setItem("token_validUntil", res.data.data.validUntil);
this.successful = true;
utils.setLogin();
setTimeout(() => {
const returnUrl = this.$store.state.returnUrl;
if(returnUrl !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/views/Root.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
methods: {
redirect() {
console.log("Redirecting...");
if(utils.authExists()) {
if(utils.isLoggedIn()) {
this.$router.push('/dashboard/');
} else {
this.$router.push('/account/');
Expand Down
5 changes: 4 additions & 1 deletion src/views/dashboard/DashboardRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ export default {
},
methods: {
async getSelf() {
this.loading = true;
try {
await this.$store.dispatch('getSelf');
this.success = true;
} catch (_) { }
} catch (err) {
console.error(err);
}
this.loading = false;
Expand Down

0 comments on commit 1e4904c

Please sign in to comment.