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

61
client/stores/auth.js vendored
View File

@@ -1,6 +1,5 @@
import {defineStore} from 'pinia'
import axios from 'axios'
import {useOpnFetch} from "~/composables/useOpnFetch.js";
export const useAuthStore = defineStore('auth', {
state: () => {
@@ -27,41 +26,36 @@ export const useAuthStore = defineStore('auth', {
},
setToken(token) {
useCookie('token', {maxAge: 60 * 60 * 24 * 30}).value = token
this.setCookie('token', token)
this.token = token
},
setAdminToken(token) {
useCookie('admin_token', {maxAge: 60 * 60 * 24 * 30}).value = token
this.setCookie('admin_token', token)
this.admin_token = token
},
loadTokenFromCookie() {
this.token = useCookie('token').value
this.admin_token = useCookie('admin_token').value
},
async fetchUser() {
useOpnFetch('/user').then(({data, error}) => {
console.log('fetch user', data,error)
if (error.value) {
console.error('Error fetching user', error.value)
this.setToken(null)
}
this.user = data.value
this.initServiceClients()
return this.user
})
},
async fetchUserIfNotFetched() {
if (this.user === null && this.token) {
await this.fetchUser()
setCookie(name, value) {
if (process.client) {
useCookie(name).value = value
}
},
initStore(token, adminToken) {
this.token = token
this.admin_token = adminToken
},
setUser(user) {
if (!user) {
console.error('Error.setting.user')
this.setToken(null)
}
this.user = user
this.initServiceClients()
},
updateUser(payload) {
this.user = payload
this.initServiceClients()
@@ -73,13 +67,14 @@ export const useAuthStore = defineStore('auth', {
useCrisp().setUser(this.user)
// Init sentry
Sentry.configureScope((scope) => {
scope.setUser({
id: this.user.id,
email: this.user.email,
subscription: this.user?.is_subscribed
})
})
// console.log(process)
// $sentry.configureScope((scope) => {
// scope.setUser({
// id: this.user.id,
// email: this.user.email,
// subscription: this.user?.is_subscribed
// })
// })
},
async logout() {

View File

@@ -1,5 +1,4 @@
import {defineStore} from 'pinia'
import {useOpnFetch} from "~/composables/useOpnFetch.js";
export const formsEndpoint = '/open/workspaces/{workspaceId}/forms'
export let currentPage = 1
@@ -56,13 +55,14 @@ export const useFormsStore = defineStore('forms', {
},
load(workspaceId) {
this.startLoading()
return useOpnFetch(formsEndpoint.replace('{workspaceId}', workspaceId) + '?page=' + currentPage).get().then((response) => {
return useOpnApi(formsEndpoint.replace('{workspaceId}', workspaceId) + '?page=' + currentPage)
.then(({data, error}) => {
if (currentPage === 1) {
this.set(response.data.data)
this.set(data.value.data)
} else {
this.append(response.data.data)
this.append(data.value.data)
}
if (currentPage < response.data.meta.last_page) {
if (currentPage < data.value.meta.last_page) {
currentPage += 1
this.load(workspaceId)
} else {

View File

@@ -1,9 +1,6 @@
import axios from 'axios'
import { defineStore } from 'pinia'
export const templatesEndpoint = '/api/templates'
import {defineStore} from 'pinia'
export const templatesEndpoint = '/templates'
export const useTemplatesStore = defineStore('templates', {
state: () => ({
content: [],
@@ -19,56 +16,57 @@ export const useTemplatesStore = defineStore('templates', {
},
getTemplateTypes: (state) => (slugs) => {
if (state.types.length === 0) return null
return Object.values(state.types).filter((val) => slugs.includes(val.slug)).map((item) => { return item.name })
return Object.values(state.types).filter((val) => slugs.includes(val.slug)).map((item) => {
return item.name
})
},
getTemplateIndustries: (state) => (slugs) => {
if (state.industries.length === 0) return null
return Object.values(state.industries).filter((val) => slugs.includes(val.slug)).map((item) => { return item.name })
return Object.values(state.industries).filter((val) => slugs.includes(val.slug)).map((item) => {
return item.name
})
}
},
actions: {
set (items) {
set(items) {
this.content = items
this.allLoaded = true
},
append (items) {
const ids = items.map((item) => { return item.id })
append(items) {
const ids = items.map((item) => {
return item.id
})
this.content = this.content.filter((val) => !ids.includes(val.id))
this.content = this.content.concat(items)
},
addOrUpdate (item) {
addOrUpdate(item) {
this.content = this.content.filter((val) => val.id !== item.id)
this.content.push(item)
},
remove (item) {
remove(item) {
this.content = this.content.filter((val) => val.id !== item.id)
},
startLoading () {
startLoading() {
this.loading = true
},
stopLoading () {
stopLoading() {
this.loading = false
},
setAllLoaded (val) {
setAllLoaded(val) {
this.allLoaded = val
},
resetState () {
resetState() {
this.set([])
this.stopLoading()
},
loadTypesAndIndustries () {
if (Object.keys(this.industries).length === 0) {
import('@/data/forms/templates/industries.json').then((module) => {
this.industries = module.default
})
}
if (Object.keys(this.types).length === 0) {
import('@/data/forms/templates/types.json').then((module) => {
this.types = module.default
})
async loadTypesAndIndustries() {
if (Object.keys(this.industries).length === 0 || Object.keys(this.types).length === 0) {
const files = import.meta.glob('~/data/forms/templates/*.json')
this.industries = await files['/data/forms/templates/industries.json']()
this.types = await files['/data/forms/templates/types.json']()
}
},
loadTemplate (slug) {
loadTemplate(slug) {
this.startLoading()
this.loadTypesAndIndustries()
@@ -77,31 +75,31 @@ export const useTemplatesStore = defineStore('templates', {
return Promise.resolve()
}
return axios.get(templatesEndpoint + '/' + slug).then((response) => {
this.addOrUpdate(response.data)
return useOpnApi(templatesEndpoint + '/' + slug).then(({data, error}) => {
this.addOrUpdate(data.value)
this.stopLoading()
}).catch((error) => {
this.stopLoading()
})
},
loadAll (options=null) {
loadAll(options = null) {
this.startLoading()
this.loadTypesAndIndustries()
// Prepare with options
let queryStr = ''
if(options !== null){
if (options !== null) {
for (const [key, value] of Object.entries(options)) {
queryStr += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(value)
}
queryStr = queryStr.slice(1)
}
return axios.get((queryStr) ? templatesEndpoint + '?' + queryStr : templatesEndpoint).then((response) => {
if(options !== null){
this.set(response.data)
return useOpnApi((queryStr) ? templatesEndpoint + '?' + queryStr : templatesEndpoint).then(({data, error}) => {
if (options !== null) {
this.set(data.value)
this.setAllLoaded(false)
} else {
this.append(response.data)
this.append(data.value)
this.setAllLoaded(true)
}
this.stopLoading()
@@ -109,7 +107,7 @@ export const useTemplatesStore = defineStore('templates', {
this.stopLoading()
})
},
loadIfEmpty () {
loadIfEmpty() {
if (!this.allLoaded) {
return this.loadAll()
}

View File

@@ -1,5 +1,4 @@
import {defineStore} from 'pinia'
import {useOpnFetch} from "~/composables/useOpnFetch.js"
import {useStorage} from "@vueuse/core"
export const workspaceEndpoint = 'open/workspaces/'
@@ -17,9 +16,9 @@ export const useWorkspacesStore = defineStore('workspaces', {
if (state.content.length === 0) return null
return state.content.find(item => item.id === id)
},
getCurrent: (state) => () => {
if (state.content.length === 0 || state.currentId === null) return null
return state.content.find(item => item.id === state.currentId)
getCurrent (){
if (this.content.length === 0 || this.currentId === null) return null
return this.content.find(item => item.id === this.currentId)
}
},
actions: {
@@ -71,9 +70,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
load() {
this.set([])
this.startLoading()
return useOpnFetch(workspaceEndpoint,{server: false}).then(({data,error}) => {
console.log(data,error)
this.set(data)
return useOpnApi(workspaceEndpoint).then(({data, error}) => {
this.set(data.value)
this.stopLoading()
})
},
@@ -85,8 +83,8 @@ export const useWorkspacesStore = defineStore('workspaces', {
},
delete(id) {
this.startLoading()
return useOpnFetch(workspaceEndpoint + id, {method: 'DELETE'}).then((response) => {
this.remove(response.data.workspace_id)
return useOpnApi(workspaceEndpoint + id, {method: 'DELETE'}).then(({data}) => {
this.remove(data.value.workspace_id)
this.stopLoading()
})
}