Fix sentry nuxt and custom domain redirect

This commit is contained in:
Julien Nahum 2024-01-12 17:32:10 +01:00
parent b29cdf8d8a
commit be43c5cae3
4 changed files with 14 additions and 18 deletions

View File

@ -28,7 +28,7 @@ class CustomDomainRestriction
'success' => false, 'success' => false,
'message' => 'Invalid domain', 'message' => 'Invalid domain',
'error' => 'invalid_domain', 'error' => 'invalid_domain',
], 401); ], 420);
} }
// Check if domain is different from current domain // Check if domain is different from current domain
@ -43,7 +43,7 @@ class CustomDomainRestriction
'success' => false, 'success' => false,
'message' => 'Unknown domain', 'message' => 'Unknown domain',
'error' => 'invalid_domain', 'error' => 'invalid_domain',
], 400); ], 420);
} }
Workspace::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspace) { Workspace::addGlobalScope('domain-restricted', function (Builder $builder) use ($workspace) {

View File

@ -42,26 +42,20 @@ export function getOpnRequestsOptions(request, opts) {
return { return {
baseURL: config.public.apiBase, baseURL: config.public.apiBase,
onResponseError({response}) { async onResponseError({response}) {
const authStore = useAuthStore() const authStore = useAuthStore()
console.log(response)
const {status} = response const {status} = response
if (status === 401) { 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) { if (authStore.check) {
console.log("Logging out due to 401") console.log("Logging out due to 401")
authStore.logout() authStore.logout()
useRouter().push({name: 'login'}) useRouter().push({name: 'login'})
} }
} } else if (status === 420) {
// If invalid domain, redirect to main domain
if (status >= 500) { window.location.href = config.public.appUrl + '?utm_source=failed_custom_domain_redirect'
} else if (status >= 500) {
console.error('Request error', status) console.error('Request error', status)
} }
}, },

8
client/lib/utils.js vendored
View File

@ -47,7 +47,7 @@ export const appUrl = (path = '/') => {
*/ */
export const getHost = function () { export const getHost = function () {
if (process.server) { if (process.server) {
return useNuxtApp().ssrContext?.event.context.siteConfigNitroOrigin || useNuxtApp().ssrContext?.event.node.req.headers.host return getDomain(useNuxtApp().ssrContext?.event.context.siteConfigNitroOrigin) || useNuxtApp().ssrContext?.event.node.req.headers.host
} else { } else {
return window.location.host return window.location.host
} }
@ -59,6 +59,7 @@ export const getHost = function () {
* @returns {*} * @returns {*}
*/ */
export const getDomain = function (url) { export const getDomain = function (url) {
if (url.includes('localhost')) return 'localhost'
return (new URL(url)).hostname return (new URL(url)).hostname
} }
@ -68,7 +69,8 @@ export const getDomain = function (url) {
*/ */
export const customDomainUsed = function() { export const customDomainUsed = function() {
const config = useRuntimeConfig() const config = useRuntimeConfig()
const appUrl = config.public.appUrl const appDomain = getDomain(config.public.appUrl)
const host = getHost()
return getDomain(getHost()) !== getDomain(appUrl) return host !== appDomain && getDomain(host) !== appDomain
} }

View File

@ -14,7 +14,7 @@ export default defineNuxtConfig({
'nuxt3-notifications', 'nuxt3-notifications',
'nuxt-simple-sitemap', 'nuxt-simple-sitemap',
'@nuxt/image', '@nuxt/image',
// ... opnformConfig.sentry_dsn ? ['@nuxtjs/sentry'] : [], ... process.env.NUXT_PUBLIC_SENTRY_DSN ? ['@nuxtjs/sentry'] : [],
], ],
build: { build: {
transpile: ["vue-notion"], transpile: ["vue-notion"],