v2: fixed oauth errors, oauth clients native control added
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
"use server"
|
||||
|
||||
import type { CreatedOAuthClient, OAuthClient } from "@/types";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { tryRefresh, clearTokens } from "@/actions/auth";
|
||||
|
||||
interface CreateClientPayload {
|
||||
name: string;
|
||||
redirect_uris: string[];
|
||||
grant_types: string[];
|
||||
scopes: string[];
|
||||
}
|
||||
|
||||
interface UpdateClientPayload {
|
||||
name: string;
|
||||
redirect_uris: string[];
|
||||
scopes: string[];
|
||||
}
|
||||
|
||||
export async function fetchOAuthClients(): Promise<OAuthClient[] | []> {
|
||||
const store = await cookies();
|
||||
const accessToken = store.get("access_token")?.value;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/clients`, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
const refreshed = await tryRefresh();
|
||||
if (!refreshed) {
|
||||
await clearTokens();
|
||||
return [];
|
||||
}
|
||||
return fetchOAuthClients();
|
||||
}
|
||||
|
||||
if (!response.ok) return [];
|
||||
return (await response.json()) as OAuthClient[];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function createClient(
|
||||
payload: CreateClientPayload
|
||||
): Promise<{ success: boolean; client?: CreatedOAuthClient; message?: string }> {
|
||||
const t = await getTranslations("errors");
|
||||
const store = await cookies();
|
||||
const accessToken = store.get("access_token")?.value;
|
||||
|
||||
if (!payload.name || payload.redirect_uris.length === 0) {
|
||||
return { success: false, message: t("allFieldsRequired") };
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/clients`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
const refreshed = await tryRefresh();
|
||||
if (!refreshed) {
|
||||
await clearTokens();
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
return createClient(payload);
|
||||
}
|
||||
|
||||
if (response.status === 400) return { success: false, message: t("invalidInputError") };
|
||||
if (!response.ok) return { success: false, message: t("serverError") };
|
||||
|
||||
const client = (await response.json()) as CreatedOAuthClient;
|
||||
return { success: true, client };
|
||||
} catch {
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateClient(
|
||||
id: string,
|
||||
payload: UpdateClientPayload
|
||||
): Promise<{ success: boolean; client?: OAuthClient; message?: string }> {
|
||||
const t = await getTranslations("errors");
|
||||
const store = await cookies();
|
||||
const accessToken = store.get("access_token")?.value;
|
||||
|
||||
if (!id || !payload.name || payload.redirect_uris.length === 0) {
|
||||
return { success: false, message: t("allFieldsRequired") };
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/clients/${id}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
const refreshed = await tryRefresh();
|
||||
if (!refreshed) {
|
||||
await clearTokens();
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
return updateClient(id, payload);
|
||||
}
|
||||
|
||||
if (response.status === 400) return { success: false, message: t("invalidInputError") };
|
||||
if (response.status === 404) return { success: false, message: t("notFoundError") };
|
||||
if (!response.ok) return { success: false, message: t("serverError") };
|
||||
|
||||
const client = (await response.json()) as OAuthClient;
|
||||
return { success: true, client };
|
||||
} catch {
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteClient(id: string): Promise<{ success: boolean; message?: string }> {
|
||||
const t = await getTranslations("errors");
|
||||
const store = await cookies();
|
||||
const accessToken = store.get("access_token")?.value;
|
||||
|
||||
if (!id) return { success: false, message: t("serverError") };
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.KOTOSOFT_ID_API_URL}/api/v1/admin/clients/${id}`, {
|
||||
method: "DELETE",
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
const refreshed = await tryRefresh();
|
||||
if (!refreshed) {
|
||||
await clearTokens();
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
return deleteClient(id);
|
||||
}
|
||||
|
||||
if (!response.ok) return { success: false, message: t("serverError") };
|
||||
return { success: true };
|
||||
} catch {
|
||||
return { success: false, message: t("serverError") };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user