Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved: handling of token in the response(#14) #18

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ import { StatusCodes } from "http-status-codes";
import router from "@/router"

axios.interceptors.request.use((config: any) => {
// TODO: pass csrf token
const token = store.getters["user/getUserToken"];
if (token) {
config.headers.Authorization = "Bearer " + token;
config.headers["api_key"] = token;
config.headers["Content-Type"] = "application/json";
}

return config;
});

// TODO: need to update this as per the changes in the Moqui response format, if required.
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data

// TODO: explore more on a secure way to store the csrf token
// Cannot store it in cookies or localStorage as its not safe
// https://stackoverflow.com/questions/67062876/is-it-secure-to-store-a-csrf-token-value-in-the-dom
// https://stackoverflow.com/questions/62289684/what-is-the-correct-way-for-a-client-to-store-a-csrf-token
const csrfToken = response.headers["x-csrf-token"]
const meta = document.createElement("meta")
meta.name = "csrf"
meta.content = csrfToken
document.getElementsByTagName("head")[0].appendChild(meta)

document.cookie = `x-csrf-token=${csrfToken}`

return response;
}, function (error) {
// TODO Handle it in a better way
Expand Down Expand Up @@ -66,7 +81,8 @@ const api = async (customConfig: any) => {
url: customConfig.url,
method: customConfig.method,
data: customConfig.data,
params: customConfig.params
params: customConfig.params,
// withCredentials: true
}

const baseURL = store.getters["user/getInstanceUrl"];
Expand Down
1 change: 0 additions & 1 deletion src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
return resp.data;
} else if (hasError(resp)) {
showToast(translate("Sorry, your username or password is incorrect. Please try again."));
console.error("error", resp.data._ERROR_MESSAGE_);

Check warning on line 26 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

Unexpected console statement

Check warning on line 26 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

Unexpected console statement
return Promise.reject(new Error(resp.data._ERROR_MESSAGE_));
}
} else {
showToast(translate("Something went wrong"));
console.error("error", resp.data._ERROR_MESSAGE_);

Check warning on line 31 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

Unexpected console statement

Check warning on line 31 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

Unexpected console statement
return Promise.reject(new Error(resp.data._ERROR_MESSAGE_));
}
} catch (err: any) {
showToast(translate("Something went wrong"));
console.error("error", err);

Check warning on line 36 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

Unexpected console statement

Check warning on line 36 in src/store/modules/user/actions.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

Unexpected console statement
return Promise.reject(new Error(err))
}
// return resp
},

/**
Expand Down
4 changes: 2 additions & 2 deletions src/views/BrokeringRuns.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
{{ "Description" }}
</ion-item>
<ion-item>
{{ "<Frequency>" }}
{{ "<Runtime>" }}
<ion-label>{{ "<Frequency>" }}</ion-label>
<ion-label slot="end">{{ "<Runtime>" }}</ion-label>
</ion-item>
<ion-item>
{{ "Created at <time>" }}
Expand Down
4 changes: 2 additions & 2 deletions src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ion-page>
<ion-content>
<div class="flex">
<form class="login-container" @keyup.enter="login(form)" @submit.prevent="login(form)">
<form class="login-container" @keyup.enter="login(form)" @submit.prevent>
<Logo />

<ion-item lines="full">
Expand Down Expand Up @@ -73,7 +73,7 @@ export default defineComponent({
this.password = ""
this.$router.push("/")
}
})
}).catch(err => err)
}
},
setup() {
Expand Down
Loading