Skip to content
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
29 changes: 12 additions & 17 deletions packages/web-pkg/src/composables/authContext/useRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useClientService } from '../clientService'
import type { Router, RouteLocationNormalizedLoaded } from 'vue-router'
import type { Method, AxiosRequestConfig, AxiosResponse } from 'axios'
import { ClientService } from '../../services'
import { Auth, ClientService } from '../../services'
import { AuthStore, useAuthStore } from '../piniaStores'

interface RequestOptions {
Expand All @@ -24,25 +24,20 @@ export function useRequest(options: RequestOptions = {}): RequestResult {
url: string,
config: AxiosRequestConfig = {}
): Promise<AxiosResponse> => {
const httpClient = authStore.accessToken
? clientService.httpAuthenticated
: clientService.httpUnAuthenticated

config.headers = config.headers || {}

if (authStore.publicLinkContextReady) {
if (authStore.publicLinkPassword) {
config.headers.Authorization =
'Basic ' +
Buffer.from(['public', authStore.publicLinkPassword].join(':')).toString('base64')
}
if (authStore.publicLinkToken) {
config.headers['public-token'] = authStore.publicLinkToken
}
}
const httpClient =
!authStore.accessToken || authStore.publicLinkContextReady
? clientService.httpUnAuthenticated
: clientService.httpAuthenticated

const auth = new Auth({
accessToken: authStore.accessToken,
publicLinkToken: authStore.publicLinkToken,
publicLinkPassword: authStore.publicLinkPassword
})

config.method = method
config.url = url
config.headers = { ...auth.getHeaders(), ...(config?.headers || {}) }

return httpClient.request(config)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/web-pkg/src/services/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class Auth {
'Basic ' + Buffer.from(['public', this.publicLinkPassword].join(':')).toString('base64')
}),
...(this.accessToken &&
!this.publicLinkPassword && {
!this.publicLinkPassword &&
!this.publicLinkToken && {
Authorization: 'Bearer ' + this.accessToken
})
}
Expand Down