Compare commits
No commits in common. "542f81d9c2f9043982b89d1edda47946ad3e1c14" and "1130e6a574b234dff46897e0743057f3e6e711fc" have entirely different histories.
542f81d9c2
...
1130e6a574
@ -87,8 +87,6 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { solarFetch } from "~/utils/request"
|
|
||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
|
|
||||||
const error = ref<string | null>(null)
|
const error = ref<string | null>(null)
|
||||||
@ -125,12 +123,16 @@ async function readTickets({ page, itemsPerPage }: { page?: number; itemsPerPage
|
|||||||
if (page) pagination.tickets.page = page
|
if (page) pagination.tickets.page = page
|
||||||
|
|
||||||
reverting.sessions = true
|
reverting.sessions = true
|
||||||
const res = await solarFetch(
|
const res = await fetch(
|
||||||
"/cgi/auth/users/me/tickets?" +
|
`${config.public.solarNetworkApi}/cgi/auth/users/me/tickets?` +
|
||||||
new URLSearchParams({
|
new URLSearchParams({
|
||||||
take: pagination.tickets.pageSize.toString(),
|
take: pagination.tickets.pageSize.toString(),
|
||||||
offset: ((pagination.tickets.page - 1) * pagination.tickets.pageSize).toString(),
|
offset: ((pagination.tickets.page - 1) * pagination.tickets.pageSize).toString(),
|
||||||
}),
|
}),
|
||||||
|
{
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
error.value = await res.text()
|
error.value = await res.text()
|
||||||
@ -147,12 +149,16 @@ async function readEvents({ page, itemsPerPage }: { page?: number; itemsPerPage?
|
|||||||
if (page) pagination.events.page = page
|
if (page) pagination.events.page = page
|
||||||
|
|
||||||
reverting.events = true
|
reverting.events = true
|
||||||
const res = await solarFetch(
|
const res = await fetch(
|
||||||
"/cgi/auth/users/me/events?" +
|
`${config.public.solarNetworkApi}/cgi/auth/users/me/events?` +
|
||||||
new URLSearchParams({
|
new URLSearchParams({
|
||||||
take: pagination.events.pageSize.toString(),
|
take: pagination.events.pageSize.toString(),
|
||||||
offset: ((pagination.events.page - 1) * pagination.events.pageSize).toString(),
|
offset: ((pagination.events.page - 1) * pagination.events.pageSize).toString(),
|
||||||
}),
|
}),
|
||||||
|
{
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
error.value = await res.text()
|
error.value = await res.text()
|
||||||
@ -168,8 +174,10 @@ Promise.all([readTickets({}), readEvents({})])
|
|||||||
|
|
||||||
async function killTicket(item: any) {
|
async function killTicket(item: any) {
|
||||||
reverting.sessions = true
|
reverting.sessions = true
|
||||||
const res = await solarFetch(`/cgi/auth/users/me/tickets/${item.id}`, {
|
const res = await fetch(`${config.public.solarNetworkApi}/cgi/auth/users/me/tickets/${item.id}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
})
|
})
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
error.value = await res.text()
|
error.value = await res.text()
|
||||||
|
@ -51,8 +51,6 @@ async function getToken(tk: string) {
|
|||||||
error.value = err
|
error.value = err
|
||||||
throw new Error(err)
|
throw new Error(err)
|
||||||
} else {
|
} else {
|
||||||
const out = await res.json()
|
|
||||||
auth.setTokenSet(out["access_token"], out["refresh_token"])
|
|
||||||
error.value = null
|
error.value = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,6 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from "vue"
|
import { ref } from "vue"
|
||||||
import { solarFetch } from "~/utils/request"
|
|
||||||
|
|
||||||
definePageMeta({
|
definePageMeta({
|
||||||
middleware: ["auth"],
|
middleware: ["auth"],
|
||||||
@ -87,7 +86,10 @@ const metadata = ref<any>(null)
|
|||||||
const panel = ref("confirm")
|
const panel = ref("confirm")
|
||||||
|
|
||||||
async function tryAuthorize() {
|
async function tryAuthorize() {
|
||||||
const res = await solarFetch(`/cgi/auth/auth/o/authorize${window.location.search}`)
|
const res = await fetch(`${config.public.solarNetworkApi}/cgi/auth/auth/o/authorize` + window.location.search, {
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
|
})
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
error.value = await res.text()
|
error.value = await res.text()
|
||||||
@ -116,8 +118,10 @@ function decline() {
|
|||||||
|
|
||||||
async function approve() {
|
async function approve() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const res = await solarFetch(`/cgi/auth/auth/o/authorize${window.location.search}`, {
|
const res = await fetch(`${config.public.solarNetworkApi}/cgi/auth/auth/o/authorize` + window.location.search, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { ref } from "vue"
|
import { ref } from "vue"
|
||||||
import { solarFetch } from "~/utils/request"
|
|
||||||
|
|
||||||
export interface Userinfo {
|
export interface Userinfo {
|
||||||
isLoggedIn: boolean
|
isLoggedIn: boolean
|
||||||
@ -18,10 +17,6 @@ export function useAtk() {
|
|||||||
return useCookie("__hydrogen_atk", { watch: "shallow" })
|
return useCookie("__hydrogen_atk", { watch: "shallow" })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useRtk() {
|
|
||||||
return useCookie("__hydrogen_rtk", { watch: "shallow" })
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useLoggedInState() {
|
export function useLoggedInState() {
|
||||||
return computed(() => useAtk().value != null)
|
return computed(() => useAtk().value != null)
|
||||||
}
|
}
|
||||||
@ -30,40 +25,6 @@ export const useUserinfo = defineStore("userinfo", () => {
|
|||||||
const userinfo = ref(defaultUserinfo)
|
const userinfo = ref(defaultUserinfo)
|
||||||
const isReady = ref(false)
|
const isReady = ref(false)
|
||||||
|
|
||||||
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() {
|
async function readProfiles() {
|
||||||
if (!useLoggedInState().value) {
|
if (!useLoggedInState().value) {
|
||||||
isReady.value = true
|
isReady.value = true
|
||||||
@ -71,7 +32,10 @@ export const useUserinfo = defineStore("userinfo", () => {
|
|||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
|
|
||||||
const res = await solarFetch("/cgi/auth/users/me")
|
const res = await fetch(`${config.public.solarNetworkApi}/cgi/auth/users/me`, {
|
||||||
|
headers: { Authorization: `Bearer ${useAtk().value}` },
|
||||||
|
credentials: "include",
|
||||||
|
})
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
return
|
return
|
||||||
@ -87,5 +51,5 @@ export const useUserinfo = defineStore("userinfo", () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { userinfo, lastRefreshedAt, isReady, setTokenSet, getAtk, readProfiles }
|
return { userinfo, isReady, readProfiles }
|
||||||
})
|
})
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
export async function solarFetch(input: string | URL | globalThis.Request, init?: RequestInit) {
|
|
||||||
const auth = useUserinfo()
|
|
||||||
const config = useRuntimeConfig()
|
|
||||||
|
|
||||||
return await fetch(`${config.public.solarNetworkApi}${input}`, {
|
|
||||||
...init,
|
|
||||||
headers: {
|
|
||||||
...init?.headers,
|
|
||||||
"Authorization": `Bearer ${await auth.getAtk()}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user