Add custom domain support
This commit is contained in:
27
client/composables/useOpnApi.js
vendored
27
client/composables/useOpnApi.js
vendored
@@ -1,3 +1,4 @@
|
||||
import {getDomain, getHost, customDomainUsed} from "~/lib/utils.js";
|
||||
|
||||
function addAuthHeader(request, options) {
|
||||
const authStore = useAuthStore()
|
||||
@@ -17,6 +18,14 @@ function addPasswordToFormRequest(request, options) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add custom domain header if custom domain is used
|
||||
*/
|
||||
function addCustomDomainHeader(request, options) {
|
||||
if (!customDomainUsed()) return
|
||||
options.headers['x-custom-domain'] = getDomain(getHost())
|
||||
}
|
||||
|
||||
export function getOpnRequestsOptions(request, opts) {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
@@ -29,17 +38,27 @@ export function getOpnRequestsOptions(request, opts) {
|
||||
|
||||
addAuthHeader(request, opts)
|
||||
addPasswordToFormRequest(request, opts)
|
||||
addCustomDomainHeader(request, opts)
|
||||
|
||||
return {
|
||||
baseURL: config.public.apiBase,
|
||||
onResponseError({response}) {
|
||||
const authStore = useAuthStore()
|
||||
console.log(response)
|
||||
const {status} = response
|
||||
|
||||
if (status === 401 && authStore.check) {
|
||||
console.log("Logging out due to 401")
|
||||
authStore.logout()
|
||||
useRouter().push({name: 'login'})
|
||||
if (status === 401) {
|
||||
if (response.body.error && response.body.error === 'invalid_domain' && process.client) {
|
||||
// If invalid domain, redirect to main domain
|
||||
window.location.href = config.public.appUrl + '?utm_source=failed_custom_domain_redirect'
|
||||
return
|
||||
}
|
||||
|
||||
if (authStore.check) {
|
||||
console.log("Logging out due to 401")
|
||||
authStore.logout()
|
||||
useRouter().push({name: 'login'})
|
||||
}
|
||||
}
|
||||
|
||||
if (status >= 500) {
|
||||
|
||||
Reference in New Issue
Block a user