feat: update CSRF Security on Login and API
This commit is contained in:
parent
fdf810d197
commit
50496ebd73
|
|
@ -35,7 +35,7 @@ const imageSchema = z.object({
|
||||||
// tags: z.string().min(1, { message: "Judul diperlukan" }),
|
// tags: z.string().min(1, { message: "Judul diperlukan" }),
|
||||||
});
|
});
|
||||||
|
|
||||||
const page = (props: { states?: any }) => {
|
const page = (props: { states?: string }) => {
|
||||||
const { states } = props;
|
const { states } = props;
|
||||||
const MySwal = withReactContent(Swal);
|
const MySwal = withReactContent(Swal);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,24 @@ const Hero: React.FC = () => {
|
||||||
|
|
||||||
const [heroData, setHeroData] = useState<any>();
|
const [heroData, setHeroData] = useState<any>();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
async function fetchCategories() {
|
||||||
|
const url = 'https://netidhub.com/api/csrf';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data; // Menampilkan data yang diterima dari API
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Fetch error: ', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchCategories();
|
||||||
initFetch();
|
initFetch();
|
||||||
}, []);
|
}, []);
|
||||||
const initFetch = async () => {
|
const initFetch = async () => {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { cn, setCookiesEncrypt } from "@/lib/utils";
|
import { cn, setCookiesEncrypt } from "@/lib/utils";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
import { getProfile, login } from "@/service/auth";
|
import { getCsrfToken, getProfile, login } from "@/service/auth";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { Link, useRouter } from "@/components/navigation";
|
import { Link, useRouter } from "@/components/navigation";
|
||||||
import { warning } from "@/lib/swal";
|
import { warning } from "@/lib/swal";
|
||||||
|
|
@ -49,12 +49,15 @@ const LoginForm = () => {
|
||||||
// Fungsi submit form
|
// Fungsi submit form
|
||||||
const onSubmit: SubmitHandler<LoginFormValues> = async (data) => {
|
const onSubmit: SubmitHandler<LoginFormValues> = async (data) => {
|
||||||
try {
|
try {
|
||||||
|
// const response = null;
|
||||||
const response = await login({
|
const response = await login({
|
||||||
...data,
|
...data,
|
||||||
grant_type: "password",
|
grantType: "password",
|
||||||
client_id: "mediahub-app",
|
clientId: "mediahub-app",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log("LOGIN: ", response);
|
||||||
|
|
||||||
if (response?.error) {
|
if (response?.error) {
|
||||||
toast.error("Username / Password Tidak Sesuai");
|
toast.error("Username / Password Tidak Sesuai");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -134,7 +137,6 @@ const LoginForm = () => {
|
||||||
) {
|
) {
|
||||||
if (profile?.data?.data?.userLevel?.id == 761 || profile?.data?.data?.userLevel?.parentLevelId == 761) {
|
if (profile?.data?.data?.userLevel?.id == 761 || profile?.data?.data?.userLevel?.parentLevelId == 761) {
|
||||||
window.location.href = "/in/welcome";
|
window.location.href = "/in/welcome";
|
||||||
// router.push('/admin/dashboard');
|
|
||||||
Cookies.set("status", "login", {
|
Cookies.set("status", "login", {
|
||||||
expires: 1,
|
expires: 1,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import { getAPIDummy } from "./http-config/axiosCustom";
|
import { getAPIDummy } from "./http-config/axiosCustom";
|
||||||
import { httpPost } from "./http-config/http-base-service";
|
import { httpGet, httpPost } from "./http-config/http-base-service";
|
||||||
import {
|
import {
|
||||||
httpGetInterceptor,
|
httpGetInterceptor,
|
||||||
httpGetInterceptorWithToken,
|
httpGetInterceptorWithToken,
|
||||||
|
|
@ -8,12 +8,66 @@ import {
|
||||||
} from "./http-config/http-interceptor-service";
|
} from "./http-config/http-interceptor-service";
|
||||||
|
|
||||||
export async function login(data: any) {
|
export async function login(data: any) {
|
||||||
|
|
||||||
|
const res = await getCsrfToken();
|
||||||
|
const csrfToken = res?.data?.token;
|
||||||
|
|
||||||
|
|
||||||
|
console.log("Token CSRF : ", csrfToken);
|
||||||
|
|
||||||
const pathUrl = "signin";
|
const pathUrl = "signin";
|
||||||
const headers = {
|
const headers = {
|
||||||
'content-type': 'application/x-www-form-urlencoded',
|
'accept': 'application/json',
|
||||||
|
'content-type': 'application/json',
|
||||||
|
'X-XSRF-TOKEN': csrfToken
|
||||||
};
|
};
|
||||||
return httpPost(pathUrl, headers, qs.stringify(data));
|
return httpPost(pathUrl, headers, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// export async function login(data: any, csrfToken: string) {
|
||||||
|
// const url = 'http://localhost:8080/mediahub/users/signin';
|
||||||
|
// try {
|
||||||
|
// const response = await fetch(url, {
|
||||||
|
// method: 'POST',
|
||||||
|
// credentials: 'include',
|
||||||
|
// headers: {
|
||||||
|
// 'Content-Type': 'application/json',
|
||||||
|
// 'X-XSRF-TOKEN': csrfToken || ''
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!response.ok) {
|
||||||
|
// throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
// }
|
||||||
|
// return response; // Menampilkan data yang diterima dari API
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Fetch error: ', error);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
export async function getCsrfToken() {
|
||||||
|
const pathUrl = "csrf";
|
||||||
|
const headers = {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
};
|
||||||
|
return httpGet(pathUrl, headers);
|
||||||
|
// const url = 'https://netidhub.com/api/csrf';
|
||||||
|
// try {
|
||||||
|
// const response = await fetch(url, {
|
||||||
|
// method: 'GET',
|
||||||
|
// credentials: 'include'
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!response.ok) {
|
||||||
|
// throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const data = await response.json();
|
||||||
|
// console.log("csrf : ", data);
|
||||||
|
// return data;
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Fetch error: ', error);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getProfile(token: any) {
|
export async function getProfile(token: any) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ const axiosBaseInstance = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
|
withCredentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default axiosBaseInstance;
|
export default axiosBaseInstance;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ const axiosInterceptorInstance = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
|
withCredentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Request interceptor
|
// Request interceptor
|
||||||
|
|
@ -39,9 +40,9 @@ axiosInterceptorInstance.interceptors.response.use(
|
||||||
if (error.response.status === 401 && !originalRequest._retry) {
|
if (error.response.status === 401 && !originalRequest._retry) {
|
||||||
originalRequest._retry = true;
|
originalRequest._retry = true;
|
||||||
const data = {
|
const data = {
|
||||||
grant_type: "refresh_token",
|
grantType: "refresh_token",
|
||||||
refresh_token: refreshToken,
|
refreshToken: refreshToken,
|
||||||
client_id: "mediahub-app",
|
clientId: "mediahub-app",
|
||||||
};
|
};
|
||||||
console.log("refresh token ", data);
|
console.log("refresh token ", data);
|
||||||
const res = await login(data);
|
const res = await login(data);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue