Files
hats-app/app/actions/user.ts
T
Ichsan Kurniawan 5cbded392a Add server API helper, logging & misc updates
Introduce a reusable serverApiCall helper and file-based logger (lib/server-api.ts, lib/logger.ts) to centralize backend requests, attach session auth, and capture request/response/error logs. Update server actions to use the new helper (updateProfileAction, changeAvatarAction) and ensure profile revalidation. Add client-side avatar upload size limit (5MB) and normalize avatar base64 payload. Add new OpenGraph/Twitter images, bump app version to v1.1.1, and add a v1.1.1 entry in release notes. Update default API/IMG env URLs and metadataBase fallback, fix minor release-notes link capitalization, and adjust a debug console.log in admin user page.
2026-02-09 14:48:03 +07:00

41 lines
1.3 KiB
TypeScript

"use server"
import axios from "@/lib/axios";
import { serverApiCall } from "@/lib/server-api";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { getServerSession } from "next-auth";
import { revalidatePath } from "next/cache";
export async function updateProfileAction(payload: any) {
const result = await serverApiCall('PUT', '/user/profile/update', payload);
if (result.success) {
revalidatePath('/profile');
}
return result;
}
export async function changeAvatarAction(nik: string, base64Image: string) {
const cleanBase64 = base64Image ? base64Image.replace('data:image/jpeg;base64,', '').replace('data:image/png;base64,', '') : '';
const payload = { nik, avatar: cleanBase64 };
const result = await serverApiCall('POST', '/user/change-avatar', payload);
if (result.success) {
revalidatePath('/profile'); // Refresh data
}
return result;
}
export const changePassword = async (payload: any) => {
try {
const session = await getServerSession(authOptions);
axios.defaults.headers.common['Authorization'] = `Bearer ${session?.user?.access_token}`;
await axios.post('/user/profile/change-password', payload)
} catch (error) {
throw new Error(error as any)
}
}