Figured out auth & middlewares

This commit is contained in:
Julien Nahum
2023-12-16 19:21:03 +01:00
parent be891b0615
commit 7c2db2052a
38 changed files with 4862 additions and 1189 deletions

View File

@@ -0,0 +1,9 @@
export default defineNuxtRouteMiddleware(async(to, from) => {
const authStore = useAuthStore()
authStore.initStore(useCookie('token').value, useCookie('admin_token').value)
if (authStore.token && !authStore.user) {
const {data, error} = await useOpnApi('user')
authStore.setUser(data.value)
}
})

View File

@@ -1,8 +1,7 @@
import {useAuthStore} from "../../resources/js/stores/auth.js";
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
if (!authStore.user?.admin) {
navigateTo({ name: 'home' })
if (authStore.check && !authStore.user?.admin) {
console.log('redirecting to home')
return navigateTo({ name: 'home' })
}
})

View File

@@ -1,11 +1,10 @@
import {useAuthStore} from "../../resources/js/stores/auth.js";
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
if (!authStore.check) {
useCookie('intended_url').value = to.path
navigateTo({ name: 'login' })
console.log('redirecting to login')
return navigateTo({ name: 'login' })
}
})

View File

@@ -1,5 +0,0 @@
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
authStore.loadTokenFromCookie()
authStore.fetchUserIfNotFetched()
})

View File

@@ -0,0 +1,16 @@
import opnformConfig from "~/opnform.config.js";
function getDomain (url) {
return (new URL(url)).hostname
}
export default defineNuxtRouteMiddleware((to, from) => {
if (opnformConfig.custom_domains_enabled && process.client) {
const isCustomDomain = getDomain(window.location.href) !== getDomain(opnformConfig.app_url)
if (isCustomDomain && !['forms.show_public'].includes(to.name)) {
// If route isn't a public form, redirect
return navigateTo({name: 'home',query: {utm_source: 'failed_custom_domain_redirect'}});
}
}
})

9
client/middleware/guest.js vendored Normal file
View File

@@ -0,0 +1,9 @@
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
if (authStore.check) {
console.log('redirecting to home')
return navigateTo({ name: 'home' })
}
})

7
client/middleware/subscribed.js vendored Normal file
View File

@@ -0,0 +1,7 @@
export default defineNuxtRouteMiddleware((to, from) => {
const authStore = useAuthStore()
if (authStore.check && !authStore.user?.is_subscribed) {
return navigateTo({ name: 'pricing' })
}
})