Fix eror build

This commit is contained in:
2026-02-25 21:18:26 +08:00
parent 42dcbcfb22
commit 1e7acac193
13 changed files with 110 additions and 58 deletions

View File

@@ -1,11 +1,11 @@
/**
* Authentication helper untuk API endpoints
*
*
* Usage:
* import { requireAuth } from "@/lib/api-auth";
*
* export default async function myEndpoint(context: Context) {
* const authResult = await requireAuth(context);
*
* export default async function myEndpoint() {
* const authResult = await requireAuth();
* if (!authResult.authenticated) {
* return authResult.response;
* }
@@ -13,24 +13,24 @@
* }
*/
import { getSession } from "@/lib/session";
import { getSession, SessionData } from "@/lib/session";
export type AuthResult =
| { authenticated: true; user: any }
export type AuthResult =
| { authenticated: true; user: NonNullable<SessionData["user"]> }
| { authenticated: false; response: Response };
export async function requireAuth(context: any): Promise<AuthResult> {
export async function requireAuth(): Promise<AuthResult> {
try {
// Cek session dari cookies
const session = await getSession();
if (!session || !session.user) {
return {
authenticated: false,
response: new Response(JSON.stringify({
success: false,
message: "Unauthorized - Silakan login terlebih dahulu"
}), {
}), {
status: 401,
headers: { 'Content-Type': 'application/json' }
})
@@ -44,7 +44,7 @@ export async function requireAuth(context: any): Promise<AuthResult> {
response: new Response(JSON.stringify({
success: false,
message: "Akun Anda tidak aktif. Hubungi administrator."
}), {
}), {
status: 403,
headers: { 'Content-Type': 'application/json' }
})
@@ -55,14 +55,13 @@ export async function requireAuth(context: any): Promise<AuthResult> {
authenticated: true,
user: session.user
};
} catch (error) {
console.error("Auth error:", error);
} catch {
return {
authenticated: false,
response: new Response(JSON.stringify({
success: false,
message: "Authentication error"
}), {
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
})
@@ -74,11 +73,11 @@ export async function requireAuth(context: any): Promise<AuthResult> {
* Optional auth - tidak error jika tidak authenticated
* Berguna untuk endpoint yang bisa diakses public atau private
*/
export async function optionalAuth(context: any): Promise<any> {
export async function optionalAuth(): Promise<NonNullable<SessionData["user"]> | null> {
try {
const session = await getSession();
return session?.user || null;
} catch (error) {
} catch {
return null;
}
}