Skip to content

Commit

Permalink
Set token and language headers in AJAX requests
Browse files Browse the repository at this point in the history
This change sets the 'Authorization' token header and
'X-Accept-Language' header in AJAX requests if they are not already
present. The token is retrieved from local storage, and the language is
obtained from the current user's settings. This ensures that
authenticated requests include the necessary authorization and language
information. No further issues are affected by this change.
  • Loading branch information
AnnatarHe committed Dec 11, 2023
1 parent 08a7ad5 commit 1aad92a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ schema: src/schema/schema.json
# schema:
# - "http://127.0.0.1:19654/api/v2/graphql":
# headers:
# Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJleHAiOjE2OTkyNzkyNDMsImlhdCI6MTY4MzcyNzI0MywiaXNzIjoiY2stc2VydmVyQCJ9.h3HiVk3idy5erOZb5jfTh-dwzI5ixZtmFwuz9Yy0J-o"
# Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJleHAiOjE3MTUwMTE3ODUsImlhdCI6MTY5OTQ1OTc4NSwiaXNzIjoiY2stc2VydmVyQCJ9.dEfHo4B6g3SJynurBbXmNQfds9NUuusYhuSonDFhkE8"
documents:
- "src/schema/**/*.graphql"
- "src/**/*.tsx"
Expand Down
15 changes: 9 additions & 6 deletions src/services/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ let token = getLocalToken()
// let token = localProfile?.token

export async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
if (token) {
options.headers = {
...(options.headers || {}),
'Authorization': `Bearer ${token}`,
'X-Accept-Language': getLanguage()
}
// set token if not exist
if (!(options.headers as Record<string, string>)['Authorization'] && token) {
(options.headers as Record<string, string>)['Authorization'] = `Bearer ${token}`
}

// set language if not exist
if (!(options.headers as Record<string, string>)['X-Accept-Language']) {
(options.headers as Record<string, string>)['X-Accept-Language'] = getLanguage()
}

options.credentials = 'include'
options.mode = 'cors'
if (!options.next) {
Expand Down

0 comments on commit 1aad92a

Please sign in to comment.