diff --git a/next.config.ts b/next.config.ts index 174f98b..f955c56 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,8 +1,11 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { + env: { + MEDOLS_CLIENT_KEY: process.env.MEDOLS_CLIENT_KEY, + }, images: { - domains: ["kontenhumas.com"], + domains: ["mikulnews.com", "dev.mikulnews.com"], }, eslint: { ignoreDuringBuilds: true, diff --git a/service/article.ts b/service/article.ts index fa719ca..98d1d5b 100644 --- a/service/article.ts +++ b/service/article.ts @@ -1,13 +1,8 @@ import { PaginationRequest } from "@/types/globals"; -import { - httpDeleteInterceptor, - httpGet, - httpPost, - httpPut, -} from "./http-config/axios-base-service"; import Cookies from "js-cookie"; +import { httpGet } from "./http-config/http-base-services"; +import { httpDeleteInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; -const token = Cookies.get("access_token"); export async function getListArticle(props: PaginationRequest) { const { page, @@ -22,9 +17,6 @@ export async function getListArticle(props: PaginationRequest) { categorySlug, isBanner, } = props; - const headers = { - "content-type": "application/json", - }; return await httpGet( `/articles?limit=${limit}&page=${page}&isPublish=${ isPublish === undefined ? "" : isPublish @@ -33,7 +25,7 @@ export async function getListArticle(props: PaginationRequest) { }&categoryId=${category || ""}&sortBy=${sortBy || "created_at"}&sort=${ sort || "asc" }&category=${categorySlug || ""}&isBanner=${isBanner || ""}`, - headers + null ); } @@ -54,29 +46,18 @@ export async function getTopArticles(props: PaginationRequest) { } export async function createArticle(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/articles`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function createArticleSchedule(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/articles/publish-scheduling?id=${data.id}&date=${data.date}`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function updateArticle(id: string, data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/articles/${id}`; - return await httpPut(pathUrl, headers, data); + return await httpPutInterceptor(pathUrl, data); } export async function getArticleById(id: any) { @@ -94,21 +75,11 @@ export async function deleteArticle(id: string) { } export async function getArticleByCategory() { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpGet(`/article-categories?limit=1000`, headers); + return await httpGet(`/article-categories?limit=1000`); } export async function getCategoryPagination(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpGet( - `/article-categories?limit=${data?.limit}&page=${data?.page}&title=${data?.search}`, - headers + `/article-categories?limit=${data?.limit}&page=${data?.page}&title=${data?.search}` ); } @@ -116,13 +87,14 @@ export async function uploadArticleFile(id: string, data: any) { const headers = { "content-type": "multipart/form-data", }; - return await httpPost(`/article-files/${id}`, headers, data); + return await httpPostInterceptor(`/article-files/${id}`, data, headers); } + export async function uploadArticleThumbnail(id: string, data: any) { const headers = { "content-type": "multipart/form-data", }; - return await httpPost(`/articles/thumbnail/${id}`, headers, data); + return await httpPostInterceptor(`/articles/thumbnail/${id}`, data, headers); } export async function deleteArticleFiles(id: number) { @@ -133,35 +105,18 @@ export async function deleteArticleFiles(id: number) { } export async function getUserLevelDataStat(startDate: string, endDate: string) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; return await httpGet( - `/articles/statistic/user-levels?startDate=${startDate}&endDate=${endDate}`, - headers + `/articles/statistic/user-levels?startDate=${startDate}&endDate=${endDate}` ); } export async function getStatisticMonthly(year: string) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpGet(`/articles/statistic/monthly?year=${year}`, headers); + return await httpGet(`/articles/statistic/monthly?year=${year}`); } export async function getStatisticMonthlyFeedback(year: string) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpGet(`/feedbacks/statistic/monthly?year=${year}`, headers); + return await httpGet(`/feedbacks/statistic/monthly?year=${year}`); } export async function getStatisticSummary() { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpGet(`/articles/statistic/summary`, headers); + return await httpGet(`/articles/statistic/summary`); } export async function submitApproval(data: { @@ -169,11 +124,7 @@ export async function submitApproval(data: { message: string; statusId: number; }) { - const headers = { - "content-type": "multipart/form-data", - Authorization: `Bearer ${token}`, - }; - return await httpPost(`/article-approvals`, headers, data); + return await httpPostInterceptor(`/article-approvals`, data); } export async function updateIsBannerArticle(id: number, status: boolean) { @@ -181,5 +132,5 @@ export async function updateIsBannerArticle(id: number, status: boolean) { "content-type": "application/json", }; const pathUrl = `/articles/banner/${id}?isBanner=${status}`; - return await httpPut(pathUrl, headers); + return await httpPutInterceptor(pathUrl, headers); } diff --git a/service/http-config/axios-base-instance.ts b/service/http-config/axios-base-instance.ts index 62db852..03f30f6 100644 --- a/service/http-config/axios-base-instance.ts +++ b/service/http-config/axios-base-instance.ts @@ -1,6 +1,6 @@ import axios from "axios"; -const baseURL = "http://38.47.180.165:8802"; +const baseURL = "https://dev.mikulnews.com/api"; const axiosBaseInstance = axios.create({ baseURL, diff --git a/service/http-config/axios-base-service.ts b/service/http-config/axios-base-service.ts index ab7101d..c33fd16 100644 --- a/service/http-config/axios-base-service.ts +++ b/service/http-config/axios-base-service.ts @@ -4,42 +4,14 @@ import axiosBaseInstance from "./http-base-instance"; import mediahubBaseInstance from "./mediahub-base-service"; import Cookies from "js-cookie"; -function getCookie(name: string) { - const value = `; ${document.cookie}`; - console.log("val", value); - const parts: any = value.split(`; ${name}=`); - if (parts.length === 2) { - return parts.pop().split(";").shift(); - } - return undefined; -} +const defaultHeaders = { + "Content-Type": "application/json", +}; -function removeCookies() { - console.log("run"); - Cookies.remove("csrf_"); - Cookies.remove("session_id"); - - document.cookie = "csrf_=; Max-Age=0;path=/"; - document.cookie = "session_id=; Max-Age=0;path=/"; -} export async function httpPost(pathUrl: any, headers: any, data?: any) { - // const aCookie = getCookie("session_id"); - - // if (aCookie === undefined) { - // console.log("kosong"); - // } else { - // console.log("cookie ada", aCookie); - // } - // removeCookies(); - // const csrfNow = Cookies.get("csrf_"); - // const sessionNow = Cookies.get("session_id"); - // console.log("now", csrfNow, sessionNow); const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.csrf_token; - const defaultHeaders = { - "Content-Type": "application/json", - }; const mergedHeaders = { ...defaultHeaders, ...headers, diff --git a/service/http-config/axios-interceptor-instance.ts b/service/http-config/axios-interceptor-instance.ts index 49f3864..e90570d 100644 --- a/service/http-config/axios-interceptor-instance.ts +++ b/service/http-config/axios-interceptor-instance.ts @@ -1,12 +1,72 @@ import axios from "axios"; +import { postSignIn } from "../master-user"; +import Cookies from "js-cookie"; -const baseURL = "https://kontenhumas.com/api"; +const baseURL = "https://dev.mikulnews.com/api"; + +const refreshToken = Cookies.get("refresh_token"); const axiosInterceptorInstance = axios.create({ baseURL, headers: { - "content-type": "application/json", + "Content-Type": "application/json", + "X-Client-Key": process.env.MEDOLS_CLIENT_KEY }, + withCredentials: true, }); +// Request interceptor +axiosInterceptorInstance.interceptors.request.use( + (config) => { + console.log("Config interceptor : ", config); + const accessToken = Cookies.get("access_token"); + if (accessToken) { + if (config.headers) + config.headers.Authorization = "Bearer " + accessToken; + } + return config; + }, + (error) => { + return Promise.reject(error); + } +); + +// Response interceptor +axiosInterceptorInstance.interceptors.response.use( + (response) => { + console.log("Response interceptor : ", response); + return response; + }, + async function (error) { + console.log("Error interceptor : ", error.response.status); + const originalRequest = error.config; + if (error.response.status === 401 && !originalRequest._retry) { + originalRequest._retry = true; + const data = { + grantType: "refresh_token", + refreshToken: refreshToken, + clientId: "mediahub-app", + }; + console.log("refresh token ", data); + const res = await postSignIn(data); + if (res?.error) { + Object.keys(Cookies.get()).forEach((cookieName) => { + Cookies.remove(cookieName); + }); + } else { + const { access_token } = res?.data; + const { refresh_token } = res?.data; + if (access_token) { + Cookies.set("access_token", access_token); + Cookies.set("refresh_token", refresh_token); + } + } + + return axiosInterceptorInstance(originalRequest); + } + + return Promise.reject(error); + } +); + export default axiosInterceptorInstance; diff --git a/service/http-config/axios-interceptor-service.ts b/service/http-config/axios-interceptor-service.ts deleted file mode 100644 index fc3e37d..0000000 --- a/service/http-config/axios-interceptor-service.ts +++ /dev/null @@ -1,71 +0,0 @@ -import axios from "axios"; -import { postSignIn } from "../master-user"; -import Cookies from "js-cookie"; - -const baseURL = "https://kontenhumas.com/api"; - -const refreshToken = Cookies.get("refresh_token"); - -const axiosInterceptorInstance = axios.create({ - baseURL, - headers: { - "content-type": "application/json", - }, - withCredentials: true, -}); - -// Request interceptor -axiosInterceptorInstance.interceptors.request.use( - (config) => { - console.log("Config interceptor : ", config); - const accessToken = Cookies.get("access_token"); - if (accessToken) { - if (config.headers) - config.headers.Authorization = "Bearer " + accessToken; - } - return config; - }, - (error) => { - return Promise.reject(error); - } -); - -// Response interceptor -axiosInterceptorInstance.interceptors.response.use( - (response) => { - console.log("Response interceptor : ", response); - return response; - }, - async function (error) { - console.log("Error interceptor : ", error.response.status); - const originalRequest = error.config; - if (error.response.status === 401 && !originalRequest._retry) { - originalRequest._retry = true; - const data = { - grantType: "refresh_token", - refreshToken: refreshToken, - clientId: "mediahub-app", - }; - console.log("refresh token ", data); - const res = await postSignIn(data); - if (res?.error) { - Object.keys(Cookies.get()).forEach((cookieName) => { - Cookies.remove(cookieName); - }); - } else { - const { access_token } = res?.data; - const { refresh_token } = res?.data; - if (access_token) { - Cookies.set("access_token", access_token); - Cookies.set("refresh_token", refresh_token); - } - } - - return axiosInterceptorInstance(originalRequest); - } - - return Promise.reject(error); - } -); - -export default axiosInterceptorInstance; diff --git a/service/http-config/http-base-instance.ts b/service/http-config/http-base-instance.ts index 95ebf60..b7d47a7 100644 --- a/service/http-config/http-base-instance.ts +++ b/service/http-config/http-base-instance.ts @@ -1,11 +1,12 @@ import axios from "axios"; -const baseURL = "https://kontenhumas.com/api"; +const baseURL = "https://dev.mikulnews.com/api"; const axiosBaseInstance = axios.create({ baseURL, headers: { "content-type": "application/json", + "X-Client-Key": process.env.MEDOLS_CLIENT_KEY }, withCredentials: true, }); diff --git a/service/http-config/http-base-services.ts b/service/http-config/http-base-services.ts index b6fc06f..92e46f9 100644 --- a/service/http-config/http-base-services.ts +++ b/service/http-config/http-base-services.ts @@ -1,8 +1,21 @@ -import axiosBaseInstance from "./axios-base-instance"; +import axiosBaseInstance from "./http-base-instance"; + +const defaultHeaders = { + "Content-Type": "application/json", + "X-Client-Key": process.env.MEDOLS_CLIENT_KEY +}; + +export async function httpGet(pathUrl: any, headers?: any) { + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + }; + + console.log("Merged Headers : ", mergedHeaders); -export async function httpGet(pathUrl: any, headers: any) { const response = await axiosBaseInstance - .get(pathUrl, { headers }) + .get(pathUrl, { headers: mergedHeaders }) .catch((error) => error.response); console.log("Response base svc : ", response); if (response?.data.success) { diff --git a/service/http-config/http-interceptor-services.ts b/service/http-config/http-interceptor-services.ts index ff772b2..a9238b6 100644 --- a/service/http-config/http-interceptor-services.ts +++ b/service/http-config/http-interceptor-services.ts @@ -1,6 +1,12 @@ import { useRouter } from "next/navigation"; -import axiosInterceptorInstance from "./axios-interceptor-instance"; import Cookies from "js-cookie"; +import axiosInterceptorInstance from "./axios-interceptor-instance"; +import { getCsrfToken } from "../master-user"; + +const defaultHeaders = { + "Content-Type": "application/json", + "X-Client-Key": process.env.MEDOLS_CLIENT_KEY +}; export async function httpGetInterceptor(pathUrl: any) { const response = await axiosInterceptorInstance @@ -29,8 +35,17 @@ export async function httpGetInterceptor(pathUrl: any) { } export async function httpPostInterceptor(pathUrl: any, data: any, headers?: any) { + const resCsrf = await getCsrfToken(); + const csrfToken = resCsrf?.data?.csrf_token; + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), + }; + const response = await axiosInterceptorInstance - .post(pathUrl, data, { headers }) + .post(pathUrl, data, { headers: mergedHeaders }) .catch((error) => error.response); console.log("Response interceptor : ", response); if (response?.status == 200 || response?.status == 201) { @@ -51,9 +66,49 @@ export async function httpPostInterceptor(pathUrl: any, data: any, headers?: any } } -export async function httpDeleteInterceptor(pathUrl: any) { +export async function httpPutInterceptor(pathUrl: any, data: any, headers?: any) { + const resCsrf = await getCsrfToken(); + const csrfToken = resCsrf?.data?.csrf_token; + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), + }; const response = await axiosInterceptorInstance - .delete(pathUrl) + .put(pathUrl, data, { headers: mergedHeaders }) + .catch((error) => error.response); + console.log("Response interceptor : ", response); + if (response?.status == 200 || response?.status == 201) { + return { + error: false, + message: "success", + data: response?.data, + }; + } else if (response?.status == 401) { + Cookies.set("is_logout", "true"); + window.location.href = "/"; + } else { + return { + error: true, + message: response?.data?.message || response?.data || null, + data: null, + }; + } +} + +export async function httpDeleteInterceptor(pathUrl: any, headers: any) { + const resCsrf = await getCsrfToken(); + const csrfToken = resCsrf?.data?.csrf_token; + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), + }; + + const response = await axiosInterceptorInstance + .delete(pathUrl, { headers: mergedHeaders }) .catch((error) => error.response); console.log("Response interceptor : ", response); if (response?.status == 200 || response?.status == 201) { diff --git a/service/master-user.ts b/service/master-user.ts index 82f3b2d..cf875e1 100644 --- a/service/master-user.ts +++ b/service/master-user.ts @@ -160,26 +160,5 @@ export async function getCsrfToken() { const headers = { "content-type": "application/json", }; - return httpGetTemp(pathUrl, headers); -} - -async function httpGetTemp(pathUrl: any, headers: any) { - const response = await axiosBaseInstance.get(pathUrl, { headers }).catch(function (error: any) { - console.log(error); - return error.response; - }); - console.log("Response base svc : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} + return httpGet(pathUrl, headers); +} \ No newline at end of file