Skip to content

Commit

Permalink
feat ✨: User authentication token clearing
Browse files Browse the repository at this point in the history
- Functionality for clearing the user's authentication token has been added.

- The code implements a mechanism to handle unauthorized access attempts and redirect the user to the login page.
  • Loading branch information
tmeftah committed Aug 25, 2024
1 parent 31983fb commit 9d7d417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions frontend/src/stores/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export const useAuthStore = defineStore("auth", {
},

actions: {
clearToken() {
this.token = null;
localStorage.removeItem("token"); // Remove token from storage
},

async login(email, password) {
const formData = new URLSearchParams();
formData.append("grant_type", "password");
Expand Down
26 changes: 25 additions & 1 deletion frontend/src/stores/main-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export const useMainStore = defineStore("main", {
},
});

if (response.status === 401) {
authStore.clearToken();
window.location.href = "/login"; // Or use Vue Router
return;
}

if (!response.ok) {
// Extract error message if available
const errorData = await response.json().catch(() => ({})); // Parsing might fail, default to empty object
Expand Down Expand Up @@ -115,6 +121,14 @@ export const useMainStore = defineStore("main", {
readChunk(reader, this.solution);
})
.catch((error) => {
//
if (error.status === 401) {
authStore.clearToken();
window.location.href = "/login"; // Or use Vue Router
return;
}

//
this.loading = false;
console.error("Error fetching the data:", error);
});
Expand All @@ -132,6 +146,12 @@ export const useMainStore = defineStore("main", {
},
});

if (response.status === 401) {
authStore.clearToken();
window.location.href = "/login"; // Or use Vue Router
return;
}

if (!response.ok) {
// Extract error message if available
const errorData = await response.json().catch(() => ({})); // Parsing might fail, default to empty object
Expand Down Expand Up @@ -180,7 +200,10 @@ export const useMainStore = defineStore("main", {
url: `${baseUrl}/documents/upload`,
method: "POST",
headers: [
{ name: "Authorization", value: `Bearer ${authStore.token}` },
{
name: "Authorization",
value: `Bearer ${authStore.token}`,
},
],
});
}, 2000);
Expand All @@ -203,6 +226,7 @@ export const useMainStore = defineStore("main", {
},

async upload_failed() {
// FIXME: chech if token is not valid anymore
Notify.create({
color: "negative",
position: "bottom",
Expand Down

0 comments on commit 9d7d417

Please sign in to comment.