2024-08-12 07:55:15 +00:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
import { ref } from "vue"
|
2024-08-12 17:08:32 +00:00
|
|
|
import { solarFetch } from "~/utils/request"
|
2024-08-12 07:55:15 +00:00
|
|
|
|
2024-08-12 15:51:42 +00:00
|
|
|
export function useAtk() {
|
2024-09-22 10:00:14 +00:00
|
|
|
return useCookie("__hydrogen_atk", { path: "/", maxAge: 31556952000 })
|
2024-08-12 07:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 16:52:48 +00:00
|
|
|
export function useRtk() {
|
2024-09-22 10:00:14 +00:00
|
|
|
return useCookie("__hydrogen_rtk", { path: "/", maxAge: 31556952000 })
|
2024-08-12 16:52:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 15:51:42 +00:00
|
|
|
export function useLoggedInState() {
|
|
|
|
return computed(() => useAtk().value != null)
|
2024-08-12 07:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useUserinfo = defineStore("userinfo", () => {
|
2024-08-13 08:23:33 +00:00
|
|
|
const userinfo = ref<any>(null)
|
2024-08-12 07:55:15 +00:00
|
|
|
const isReady = ref(false)
|
2024-08-13 08:23:33 +00:00
|
|
|
const isLoggedIn = ref(false)
|
2024-08-12 07:55:15 +00:00
|
|
|
|
2024-08-13 10:26:36 +00:00
|
|
|
let fetchCompleter: Completer<boolean> | null = null
|
|
|
|
let refreshCompleter: Completer<string> | null = null
|
|
|
|
|
2024-08-12 17:08:32 +00:00
|
|
|
const lastRefreshedAt = ref<Date | null>(null)
|
|
|
|
|
|
|
|
function setTokenSet(atk: string, rtk: string) {
|
|
|
|
lastRefreshedAt.value = new Date()
|
|
|
|
useAtk().value = atk
|
|
|
|
useRtk().value = rtk
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getAtk() {
|
2024-08-13 08:23:33 +00:00
|
|
|
if (!useLoggedInState().value) return useAtk().value
|
2024-08-12 17:08:32 +00:00
|
|
|
if (lastRefreshedAt.value != null && Math.floor(Math.abs(Date.now() - lastRefreshedAt.value.getTime()) / 60000) < 3) {
|
|
|
|
return useAtk().value
|
|
|
|
}
|
|
|
|
|
2024-08-13 10:26:36 +00:00
|
|
|
if (refreshCompleter != null) {
|
|
|
|
return await refreshCompleter.promise
|
|
|
|
} else {
|
|
|
|
refreshCompleter = new Completer<string>()
|
|
|
|
}
|
|
|
|
|
2024-08-12 17:08:32 +00:00
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
2024-08-17 15:56:03 +00:00
|
|
|
const res = await fetch(`${config.public.solarNetworkApi}/cgi/id/auth/token`, {
|
2024-08-12 17:08:32 +00:00
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
refresh_token: useRtk().value,
|
|
|
|
grant_type: "refresh_token",
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
if (res.status !== 200) {
|
|
|
|
const err = await res.text()
|
|
|
|
throw new Error(err)
|
|
|
|
} else {
|
|
|
|
const out = await res.json()
|
|
|
|
console.log("[PASSPORT] Access token has been refreshed now.")
|
|
|
|
setTokenSet(out["access_token"], out["refresh_token"])
|
2024-08-13 10:26:36 +00:00
|
|
|
refreshCompleter.complete(out["access_token"])
|
2024-08-12 17:08:32 +00:00
|
|
|
return out["access_token"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 07:55:15 +00:00
|
|
|
async function readProfiles() {
|
2024-08-13 10:26:36 +00:00
|
|
|
if (fetchCompleter != null) {
|
|
|
|
await fetchCompleter.promise
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fetchCompleter = new Completer<boolean>()
|
|
|
|
}
|
|
|
|
|
2024-08-12 15:51:42 +00:00
|
|
|
if (!useLoggedInState().value) {
|
2024-08-13 10:26:36 +00:00
|
|
|
fetchCompleter.complete(true)
|
2024-08-24 16:15:34 +00:00
|
|
|
fetchCompleter = null
|
2024-08-12 07:55:15 +00:00
|
|
|
isReady.value = true
|
2024-08-24 16:15:34 +00:00
|
|
|
return
|
2024-08-12 07:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 15:56:03 +00:00
|
|
|
const res = await solarFetch("/cgi/id/users/me")
|
2024-08-12 07:55:15 +00:00
|
|
|
|
|
|
|
if (res.status !== 200) {
|
2024-08-13 10:26:36 +00:00
|
|
|
fetchCompleter.complete(true)
|
2024-08-24 16:15:34 +00:00
|
|
|
fetchCompleter = null
|
2024-08-13 08:23:33 +00:00
|
|
|
isReady.value = true
|
2024-08-12 07:55:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
2024-08-13 08:23:33 +00:00
|
|
|
isLoggedIn.value = true
|
2024-08-12 07:55:15 +00:00
|
|
|
isReady.value = true
|
2024-08-13 08:23:33 +00:00
|
|
|
userinfo.value = data
|
2024-08-13 10:26:36 +00:00
|
|
|
fetchCompleter.complete(true)
|
2024-08-24 16:15:34 +00:00
|
|
|
fetchCompleter = null
|
2024-08-12 07:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 10:26:36 +00:00
|
|
|
return { userinfo, lastRefreshedAt, isLoggedIn, isReady, fetchCompleter, setTokenSet, getAtk, readProfiles }
|
2024-08-12 07:55:15 +00:00
|
|
|
})
|
2024-08-13 10:26:36 +00:00
|
|
|
|
|
|
|
export class Completer<T> {
|
|
|
|
public readonly promise: Promise<T>
|
|
|
|
public complete: (value: (PromiseLike<T> | T)) => void
|
|
|
|
private reject: (reason?: any) => void
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.promise = new Promise<T>((resolve, reject) => {
|
|
|
|
this.complete = resolve
|
|
|
|
this.reject = reject
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|