2024-04-15 19:39:03 +02:00
|
|
|
import { defineStore } from "pinia"
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const useAuthStore = defineStore("auth", {
|
2023-12-14 16:53:05 +01:00
|
|
|
state: () => {
|
|
|
|
|
return {
|
|
|
|
|
token: null,
|
|
|
|
|
admin_token: null,
|
|
|
|
|
user: null,
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
getters: {
|
2024-04-15 19:39:03 +02:00
|
|
|
check: (state) => state.user !== null && state.user !== undefined,
|
2024-07-26 13:24:34 +02:00
|
|
|
has_active_license: (state) => state.user !== null && state.user !== undefined && state.user.active_license !== null,
|
2024-04-15 19:39:03 +02:00
|
|
|
isImpersonating: (state) =>
|
|
|
|
|
state.admin_token !== null && state.admin_token !== undefined,
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
// Stores admin token temporarily for impersonation
|
2023-12-14 16:53:05 +01:00
|
|
|
startImpersonating() {
|
|
|
|
|
this.setAdminToken(this.token)
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
// Stop admin impersonation
|
2023-12-14 16:53:05 +01:00
|
|
|
stopImpersonating() {
|
2025-03-10 10:32:17 +01:00
|
|
|
// When stopping impersonation, we don't have expiration info for the admin token
|
|
|
|
|
// Use a default long expiration (24 hours) to ensure the admin can continue working
|
|
|
|
|
this.setToken(this.admin_token, 60 * 60 * 24)
|
2024-04-22 16:11:21 +02:00
|
|
|
this.setAdminToken(null)
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
|
2025-03-10 10:32:17 +01:00
|
|
|
setToken(token, expiresIn) {
|
|
|
|
|
// Set cookie with expiration if provided
|
|
|
|
|
const cookieOptions = {}
|
|
|
|
|
|
|
|
|
|
if (expiresIn) {
|
|
|
|
|
// expiresIn is in seconds, maxAge also needs to be in seconds
|
|
|
|
|
cookieOptions.maxAge = expiresIn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setCookie("token", token, cookieOptions)
|
2023-12-09 15:47:03 +01:00
|
|
|
this.token = token
|
|
|
|
|
},
|
|
|
|
|
|
2023-12-14 16:53:05 +01:00
|
|
|
setAdminToken(token) {
|
2024-04-15 19:39:03 +02:00
|
|
|
this.setCookie("admin_token", token)
|
2023-12-14 16:53:05 +01:00
|
|
|
this.admin_token = token
|
|
|
|
|
},
|
|
|
|
|
|
2025-03-10 10:32:17 +01:00
|
|
|
setCookie(name, value, options = {}) {
|
2024-03-28 17:59:41 +01:00
|
|
|
if (import.meta.client) {
|
2025-03-10 10:32:17 +01:00
|
|
|
useCookie(name, options).value = value
|
2023-12-16 19:21:03 +01:00
|
|
|
}
|
2023-12-14 16:53:05 +01:00
|
|
|
},
|
|
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
initStore(token, adminToken) {
|
|
|
|
|
this.token = token
|
|
|
|
|
this.admin_token = adminToken
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
|
2023-12-16 19:21:03 +01:00
|
|
|
setUser(user) {
|
|
|
|
|
if (!user) {
|
2024-04-15 19:39:03 +02:00
|
|
|
console.error("No user, logging out.")
|
2025-03-10 10:32:17 +01:00
|
|
|
// When logging out due to no user, clear the token with maxAge 0
|
|
|
|
|
this.setToken(null, 0)
|
2023-12-14 16:53:05 +01:00
|
|
|
}
|
2023-12-16 19:21:03 +01:00
|
|
|
|
|
|
|
|
this.user = user
|
|
|
|
|
this.initServiceClients()
|
2023-12-14 16:53:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
updateUser(payload) {
|
2023-12-09 15:47:03 +01:00
|
|
|
this.user = payload
|
2023-12-11 11:56:21 +01:00
|
|
|
this.initServiceClients()
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
|
2023-12-09 16:33:56 +01:00
|
|
|
initServiceClients() {
|
|
|
|
|
if (!this.user) return
|
|
|
|
|
useAmplitude().setUser(this.user)
|
|
|
|
|
useCrisp().setUser(this.user)
|
2023-12-14 16:53:05 +01:00
|
|
|
|
2024-02-10 14:10:14 +01:00
|
|
|
// todo: set sentry user
|
2023-12-09 16:33:56 +01:00
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-01-25 05:12:45 +01:00
|
|
|
logout() {
|
2024-04-15 19:39:03 +02:00
|
|
|
opnFetch("logout", { method: "POST" }).catch(() => {})
|
2023-12-09 15:47:03 +01:00
|
|
|
|
|
|
|
|
this.user = null
|
2025-03-10 10:32:17 +01:00
|
|
|
|
|
|
|
|
// Clear the token cookie by setting maxAge to 0
|
|
|
|
|
this.setCookie("token", null, { maxAge: 0 })
|
|
|
|
|
this.token = null
|
2023-12-09 15:47:03 +01:00
|
|
|
},
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
// async fetchOauthUrl() {
|
2024-01-02 19:03:14 +01:00
|
|
|
// const {data} = await axios.post(`/api/oauth/${provider}`)
|
|
|
|
|
// return data.url
|
2024-04-15 19:39:03 +02:00
|
|
|
// },
|
|
|
|
|
},
|
2023-12-09 15:47:03 +01:00
|
|
|
})
|