main
Some checks failed
UI Deploy (Next-Auth Support) 🎨 / build-and-deploy (push) Has been cancelled

This commit is contained in:
Harun CAN
2026-03-30 00:22:06 +03:00
parent 45a540c530
commit 8bd995ea18
44 changed files with 3721 additions and 11852 deletions

View File

@@ -1,45 +1,86 @@
import { apiRequest } from "@/lib/api/api-service";
import { ApiResponse } from "@/types/api-response";
import { LoginDto, RegisterDto, RefreshTokenDto, AuthResponse } from "./types";
/**
* Auth Service - Example Implementation
* Matches Backend: /api/auth/*
* Auth Service — NextAuth CredentialsProvider için server-side auth çağrıları.
* `apiRequest` yerine doğrudan fetch kullanır (server-side, session gerektirmez).
*/
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000/api';
interface LoginDto {
email: string;
password: string;
}
interface AuthUser {
id: string;
email: string;
firstName?: string;
lastName?: string;
roles?: string[];
}
interface AuthResponse {
accessToken: string;
refreshToken: string;
expiresIn?: number;
user: AuthUser;
}
interface ApiResponse<T> {
success: boolean;
data: T;
message?: string;
statusCode?: number;
}
async function authFetch<T>(url: string, body: unknown): Promise<ApiResponse<T>> {
try {
const res = await fetch(`${API_URL}${url}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
const json = await res.json();
if (!res.ok) {
return {
success: false,
data: null as unknown as T,
message: json?.message || 'İstek başarısız',
statusCode: res.status,
};
}
return {
success: true,
data: json?.data ?? json,
message: json?.message,
statusCode: res.status,
};
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Bağlantı hatası';
return {
success: false,
data: null as unknown as T,
message,
};
}
}
const login = (data: LoginDto) => {
return apiRequest<ApiResponse<AuthResponse>>({
url: "/auth/login",
client: "auth",
method: "post",
data,
});
return authFetch<AuthResponse>('/auth/login', data);
};
const register = (data: RegisterDto) => {
return apiRequest<ApiResponse<AuthResponse>>({
url: "/auth/register",
client: "auth",
method: "post",
data,
});
const register = (data: { email: string; password: string; firstName?: string; lastName?: string }) => {
return authFetch<AuthResponse>('/auth/register', data);
};
const refreshToken = (data: RefreshTokenDto) => {
return apiRequest<ApiResponse<AuthResponse>>({
url: "/auth/refresh",
client: "auth",
method: "post",
data,
});
const refreshToken = (data: { refreshToken: string }) => {
return authFetch<AuthResponse>('/auth/refresh', data);
};
const logout = () => {
return apiRequest<ApiResponse<null>>({
url: "/auth/logout",
client: "auth",
method: "post",
});
return authFetch<null>('/auth/logout', {});
};
export const authService = {