62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
"use server"
|
|
|
|
import { Consent } from "@/types"
|
|
import { cookies } from "next/headers"
|
|
import { clearTokens, tryRefresh } from "./auth"
|
|
|
|
export async function getConsents() {
|
|
const store = await cookies()
|
|
const accessToken = store.get("access_token")?.value
|
|
|
|
const response = await fetch(
|
|
`${process.env.KOTOSOFT_ID_API_URL}/api/v1/me/consents`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (response.status === 401) {
|
|
const refreshed = await tryRefresh();
|
|
if (!refreshed) {
|
|
await clearTokens();
|
|
return null;
|
|
}
|
|
return getConsents();
|
|
}
|
|
|
|
if (!response.ok) return []
|
|
|
|
var consents = await response.json() as Consent[];
|
|
consents.forEach((consent: Consent) => {
|
|
if (consent.client_id == process.env.CLIENT_ID) {
|
|
consent.isRevokable = false
|
|
} else {
|
|
consent.isRevokable = true
|
|
}
|
|
});
|
|
|
|
return consents
|
|
}
|
|
|
|
export async function revokeConsent(clientId: string) {
|
|
const store = await cookies()
|
|
const accessToken = store.get("access_token")?.value
|
|
|
|
const response = await fetch(
|
|
`${process.env.KOTOSOFT_ID_API_URL}/api/v1/me/consents/${clientId}`,
|
|
{
|
|
method: "DELETE",
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.ok) {
|
|
return { success: false }
|
|
}
|
|
|
|
return { success: true }
|
|
} |