Capital/stores/userinfo.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

import { defineStore } from "pinia"
import { ref } from "vue"
2024-08-12 17:08:32 +00:00
import { solarFetch } from "~/utils/request"
export interface Userinfo {
isLoggedIn: boolean
displayName: string
data: any
}
export const defaultUserinfo: Userinfo = {
isLoggedIn: false,
displayName: "Citizen",
data: null,
}
2024-08-12 15:51:42 +00:00
export function useAtk() {
return useCookie("__hydrogen_atk", { watch: "shallow" })
}
export function useRtk() {
return useCookie("__hydrogen_rtk", { watch: "shallow" })
}
2024-08-12 15:51:42 +00:00
export function useLoggedInState() {
return computed(() => useAtk().value != null)
}
export const useUserinfo = defineStore("userinfo", () => {
const userinfo = ref(defaultUserinfo)
const isReady = ref(false)
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() {
if (lastRefreshedAt.value != null && Math.floor(Math.abs(Date.now() - lastRefreshedAt.value.getTime()) / 60000) < 3) {
return useAtk().value
}
const config = useRuntimeConfig()
const res = await fetch(`${config.public.solarNetworkApi}/cgi/auth/auth/token`, {
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"])
return out["access_token"]
}
}
async function readProfiles() {
2024-08-12 15:51:42 +00:00
if (!useLoggedInState().value) {
isReady.value = true
}
const config = useRuntimeConfig()
2024-08-12 17:08:32 +00:00
const res = await solarFetch("/cgi/auth/users/me")
if (res.status !== 200) {
return
}
const data = await res.json()
isReady.value = true
userinfo.value = {
isLoggedIn: true,
displayName: data["nick"],
data: data,
}
}
2024-08-12 17:08:32 +00:00
return { userinfo, lastRefreshedAt, isReady, setTokenSet, getAtk, readProfiles }
})