2024-06-05 15:35:46 +02:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
|
import { useContentStore } from "~/composables/stores/useContentStore.js"
|
|
|
|
|
|
|
|
|
|
export const providersEndpoint = "/open/providers"
|
|
|
|
|
|
|
|
|
|
export const useOAuthProvidersStore = defineStore("oauth_providers", () => {
|
|
|
|
|
const contentStore = useContentStore()
|
|
|
|
|
const alert = useAlert()
|
|
|
|
|
|
2024-08-29 13:28:02 +02:00
|
|
|
const googleDrivePermission = 'https://www.googleapis.com/auth/drive.file'
|
|
|
|
|
|
2024-06-07 12:12:24 +02:00
|
|
|
const services = computed(() => {
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
name: 'google',
|
|
|
|
|
title: 'Google',
|
|
|
|
|
icon: 'mdi:google',
|
|
|
|
|
enabled: true
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const getService = (service) => {
|
|
|
|
|
return services.value.find((item) => item.name === service)
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
const fetchOAuthProviders = () => {
|
|
|
|
|
contentStore.resetState()
|
|
|
|
|
contentStore.startLoading()
|
|
|
|
|
|
|
|
|
|
return opnFetch(providersEndpoint).then(
|
|
|
|
|
(data) => {
|
|
|
|
|
contentStore.save(data)
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connect = (service, redirect = false) => {
|
|
|
|
|
contentStore.resetState()
|
|
|
|
|
contentStore.startLoading()
|
|
|
|
|
|
|
|
|
|
const intention = new URL(window.location.href).pathname
|
|
|
|
|
|
|
|
|
|
opnFetch(`/settings/providers/connect/${service}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: {
|
|
|
|
|
...redirect ? { intention } : {},
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then((data) => {
|
|
|
|
|
window.location.href = data.url
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
try {
|
|
|
|
|
alert.error(error.data.message)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
alert.error("An error occurred while connecting an account")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 15:22:57 +02:00
|
|
|
const guestConnect = (service, redirect = false) => {
|
|
|
|
|
contentStore.resetState()
|
|
|
|
|
contentStore.startLoading()
|
|
|
|
|
|
|
|
|
|
const intention = new URL(window.location.href).pathname
|
|
|
|
|
|
|
|
|
|
opnFetch(`/oauth/connect/${service}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: {
|
|
|
|
|
...redirect ? { intention } : {},
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then((data) => {
|
2025-03-25 10:41:11 +01:00
|
|
|
window.open(data.url, '_blank')
|
2024-08-19 15:22:57 +02:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
try {
|
|
|
|
|
alert.error(error.data.message)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
alert.error("An error occurred while connecting an account")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
const providers = computed(() => contentStore.getAll.value)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...contentStore,
|
2024-08-29 13:28:02 +02:00
|
|
|
googleDrivePermission,
|
2024-06-07 12:12:24 +02:00
|
|
|
services,
|
|
|
|
|
getService,
|
2024-06-05 15:35:46 +02:00
|
|
|
fetchOAuthProviders,
|
|
|
|
|
providers,
|
2024-08-19 15:22:57 +02:00
|
|
|
connect,
|
|
|
|
|
guestConnect
|
2024-06-05 15:35:46 +02:00
|
|
|
}
|
|
|
|
|
})
|