conflict fixed
This commit is contained in:
+11
-14
@@ -3,32 +3,29 @@ import axios, { type AxiosError } from 'axios'
|
||||
export const api = axios.create({
|
||||
baseURL: process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:5089/api',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
withCredentials: true,
|
||||
})
|
||||
|
||||
// Inject auth token on every request
|
||||
api.interceptors.request.use((config) => {
|
||||
// TODO: replace with real token from cookie once auth is implemented
|
||||
const token = typeof window !== 'undefined'
|
||||
? document.cookie.split('; ').find(r => r.startsWith('access-token='))?.split('=')[1]
|
||||
: null
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`
|
||||
return config
|
||||
})
|
||||
type UnauthorizedHandler = () => void
|
||||
let unauthorizedHandler: UnauthorizedHandler | null = null
|
||||
|
||||
export function registerUnauthorizedHandler(fn: UnauthorizedHandler): () => void {
|
||||
unauthorizedHandler = fn
|
||||
return () => { unauthorizedHandler = null }
|
||||
}
|
||||
|
||||
// Normalize error responses
|
||||
api.interceptors.response.use(
|
||||
(res) => res,
|
||||
(error: AxiosError<{ error: string }>) => {
|
||||
const status = error.response?.status
|
||||
if (status === 401) {
|
||||
if (typeof window !== 'undefined') window.location.href = '/login'
|
||||
return new Promise(() => {}) // redirect in progress — never resolve so no error toast fires
|
||||
if (status === 401 && typeof window !== 'undefined' && window.location.pathname !== '/login') {
|
||||
unauthorizedHandler?.()
|
||||
return new Promise(() => {})
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// Helper to extract error message from API response
|
||||
export function getApiError(error: unknown): string {
|
||||
if (axios.isAxiosError(error)) {
|
||||
return error.response?.data?.error ?? error.message
|
||||
|
||||
@@ -1,41 +1,38 @@
|
||||
import { api } from '@/lib/api'
|
||||
import type {
|
||||
PaginatedResponse,
|
||||
UserResponse,
|
||||
RoleResponse,
|
||||
PermissionResponse,
|
||||
} from '@/types/api'
|
||||
|
||||
import type { PageParams } from './master'
|
||||
import type { UserResponse, RoleResponse, PermissionResponse } from '@/types/api'
|
||||
|
||||
// ─── Users ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface UserBody {
|
||||
export interface CreateUserBody {
|
||||
username: string
|
||||
password: string
|
||||
fullName: string
|
||||
roleId: string
|
||||
reportsToUserId?: string | null
|
||||
password?: string
|
||||
isActive?: boolean
|
||||
}
|
||||
|
||||
export interface UserListParams extends PageParams {
|
||||
roleId?: string
|
||||
search?: string
|
||||
export interface UpdateUserBody {
|
||||
fullName: string
|
||||
}
|
||||
|
||||
export const users = {
|
||||
list: (params?: UserListParams) =>
|
||||
api.get<PaginatedResponse<UserResponse>>('/users', { params }).then(r => r.data),
|
||||
list: () =>
|
||||
api.get<UserResponse[]>('/users').then(r => r.data),
|
||||
|
||||
get: (id: string) =>
|
||||
api.get<UserResponse>(`/users/${id}`).then(r => r.data),
|
||||
|
||||
create: (body: UserBody) =>
|
||||
create: (body: CreateUserBody) =>
|
||||
api.post<UserResponse>('/users', body).then(r => r.data),
|
||||
|
||||
update: (id: string, body: Partial<UserBody>) =>
|
||||
update: (id: string, body: UpdateUserBody) =>
|
||||
api.put<UserResponse>(`/users/${id}`, body).then(r => r.data),
|
||||
|
||||
assignRole: (id: string, roleId: string) =>
|
||||
api.put<UserResponse>(`/users/${id}/role`, { roleId }).then(r => r.data),
|
||||
|
||||
deactivate: (id: string) =>
|
||||
api.delete(`/users/${id}`),
|
||||
}
|
||||
|
||||
// ─── Roles ────────────────────────────────────────────────────────────────────
|
||||
@@ -43,11 +40,6 @@ export const users = {
|
||||
export interface RoleBody {
|
||||
name: string
|
||||
level: number
|
||||
isActive?: boolean
|
||||
}
|
||||
|
||||
export interface RoleWithPermissions extends RoleResponse {
|
||||
permissions: PermissionResponse[]
|
||||
}
|
||||
|
||||
export const roles = {
|
||||
@@ -55,16 +47,22 @@ export const roles = {
|
||||
api.get<RoleResponse[]>('/roles').then(r => r.data),
|
||||
|
||||
get: (id: string) =>
|
||||
api.get<RoleWithPermissions>(`/roles/${id}`).then(r => r.data),
|
||||
api.get<RoleResponse>(`/roles/${id}`).then(r => r.data),
|
||||
|
||||
getPermissions: (id: string) =>
|
||||
api.get<PermissionResponse[]>(`/roles/${id}/permissions`).then(r => r.data),
|
||||
|
||||
create: (body: RoleBody) =>
|
||||
api.post<RoleResponse>('/roles', body).then(r => r.data),
|
||||
|
||||
update: (id: string, body: Partial<RoleBody>) =>
|
||||
update: (id: string, body: RoleBody) =>
|
||||
api.put<RoleResponse>(`/roles/${id}`, body).then(r => r.data),
|
||||
|
||||
assignPermissions: (id: string, permissionIds: string[]) =>
|
||||
api.put(`/roles/${id}/permissions`, { permissionIds }).then(r => r.data),
|
||||
|
||||
deactivate: (id: string) =>
|
||||
api.delete(`/roles/${id}`),
|
||||
}
|
||||
|
||||
// ─── Permissions ──────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user