merge login
This commit is contained in:
@@ -55,7 +55,7 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Send to backend
|
// Send to backend
|
||||||
const response = await fetch('https://bot.kediritechnopark.com/webhook/master-agent/ask', {
|
const response = await fetch('https://bot.kediritechnopark.com/webhook/master-agent/ask/dev', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ pertanyaan: message, sessionId: JSON.parse(localStorage.getItem('session')).sessionId, lastSeen: new Date().toISOString() }),
|
body: JSON.stringify({ pertanyaan: message, sessionId: JSON.parse(localStorage.getItem('session')).sessionId, lastSeen: new Date().toISOString() }),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import React, { useRef, useState, useEffect } from 'react';
|
import React, { useRef, useState, useEffect } from 'react';
|
||||||
import styles from './Dashboard.module.css';
|
import styles from './Dashboard.module.css';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import Modal from './Modal';
|
import Modal from './Modal';
|
||||||
import Conversations from './Conversations';
|
import Conversations from './Conversations';
|
||||||
@@ -21,21 +22,32 @@ const Dashboard = () => {
|
|||||||
botMessages: 0,
|
botMessages: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const [isDragging, setIsDragging] = useState(false);
|
const [isDragging, setIsDragging] = useState(false);
|
||||||
const [selectedFile, setSelectedFile] = useState(null);
|
const [selectedFile, setSelectedFile] = useState(null);
|
||||||
|
|
||||||
|
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||||
|
|
||||||
const handleFile = (file) => {
|
const handleFile = (file) => {
|
||||||
if (file) {
|
if (file) {
|
||||||
setSelectedFile(file);
|
setSelectedFile(file);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
setIsLoggedIn(!!token);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
localStorage.removeItem('token');
|
||||||
|
localStorage.removeItem('user');
|
||||||
|
window.location.reload(); // Bisa juga: window.location.href = '/login';
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchStats() {
|
async function fetchStats() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://bot.kediritechnopark.com/webhook/master-agent/dashboard');
|
const response = await fetch('https://bot.kediritechnopark.com/webhook/master-agent/dashboard/dev');
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setDiscussedTopics(data[0]?.result?.topics)
|
setDiscussedTopics(data[0]?.result?.topics)
|
||||||
|
|
||||||
@@ -59,7 +71,6 @@ const Dashboard = () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
setStats({
|
setStats({
|
||||||
totalChats: totalSessions.size,
|
totalChats: totalSessions.size,
|
||||||
userMessages,
|
userMessages,
|
||||||
@@ -74,6 +85,45 @@ const Dashboard = () => {
|
|||||||
fetchStats();
|
fetchStats();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchData = async () => {
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('https://bot.kediritechnopark.com/webhook/profile/dev', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status === 401 || response.status === 403) {
|
||||||
|
// Token tidak valid atau tidak punya akses, redirect ke login
|
||||||
|
localStorage.removeItem('token'); // Hapus token agar bersih
|
||||||
|
navigate('/login');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Fetch gagal dengan status: ' + response.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
|
// lanjutkan penggunaan data
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error);
|
||||||
|
// Bisa juga redirect ke login kalau error tertentu
|
||||||
|
navigate('/login');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchData();
|
||||||
|
}, [navigate]);
|
||||||
|
|
||||||
const openConversationsModal = () => {
|
const openConversationsModal = () => {
|
||||||
setModalContent(<Conversations conversations={conversations} />);
|
setModalContent(<Conversations conversations={conversations} />);
|
||||||
};
|
};
|
||||||
@@ -82,7 +132,6 @@ const Dashboard = () => {
|
|||||||
setModalContent(<DiscussedTopics topics={discussedTopics} />);
|
setModalContent(<DiscussedTopics topics={discussedTopics} />);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!rawData.length) return;
|
if (!rawData.length) return;
|
||||||
|
|
||||||
@@ -121,7 +170,6 @@ const Dashboard = () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const datasets = prefixes.map(prefix => ({
|
const datasets = prefixes.map(prefix => ({
|
||||||
label: prefixLabelMap[prefix],
|
label: prefixLabelMap[prefix],
|
||||||
data: counts[prefix],
|
data: counts[prefix],
|
||||||
@@ -167,6 +215,11 @@ const Dashboard = () => {
|
|||||||
return (
|
return (
|
||||||
<div className={styles.dashboardContainer}>
|
<div className={styles.dashboardContainer}>
|
||||||
<div className={styles.dashboardHeader}>
|
<div className={styles.dashboardHeader}>
|
||||||
|
{isLoggedIn ? (
|
||||||
|
<button onClick={handleLogout} className={styles.logoutButton}>Logout</button>
|
||||||
|
) : (
|
||||||
|
<a href="/login" className={styles.loginButton}>Login</a>
|
||||||
|
)}
|
||||||
<img src="/dermalounge.jpg" alt="Bot Avatar" />
|
<img src="/dermalounge.jpg" alt="Bot Avatar" />
|
||||||
<div>
|
<div>
|
||||||
<h1 className={styles.h1}>Dermalounge AI Admin Dashboard</h1>
|
<h1 className={styles.h1}>Dermalounge AI Admin Dashboard</h1>
|
||||||
@@ -198,7 +251,6 @@ const Dashboard = () => {
|
|||||||
<canvas ref={chartRef}></canvas>
|
<canvas ref={chartRef}></canvas>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div className={styles.chartSection}>
|
<div className={styles.chartSection}>
|
||||||
<h2 className={styles.chartTitle}>Update data</h2>
|
<h2 className={styles.chartTitle}>Update data</h2>
|
||||||
|
|
||||||
@@ -245,7 +297,6 @@ const Dashboard = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<div className={styles.footer}>
|
<div className={styles.footer}>
|
||||||
© 2025 Kediri Technopark
|
© 2025 Kediri Technopark
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -144,3 +144,51 @@
|
|||||||
display: block;
|
display: block;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.dashboardHeader {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 16px;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #075e54;
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loginButton {
|
||||||
|
position: absolute;
|
||||||
|
top: 10px;
|
||||||
|
right: 10px;
|
||||||
|
background-color: #0b7366;
|
||||||
|
color: white;
|
||||||
|
padding: 8px 18px;
|
||||||
|
border-radius: 8px;
|
||||||
|
text-decoration: none;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 12px;
|
||||||
|
transition: background-color 0.3s ease;
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loginButton:hover {
|
||||||
|
background-color: #0f9b8a;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logoutButton {
|
||||||
|
position: absolute;
|
||||||
|
top: 10px;
|
||||||
|
right: 10px;
|
||||||
|
background-color: #ad1212;
|
||||||
|
color: white;
|
||||||
|
padding: 8px 14px;
|
||||||
|
border-radius: 8px;
|
||||||
|
text-decoration: none;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 12px;
|
||||||
|
transition: background-color 0.3s ease;
|
||||||
|
border: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logoutButton:hover {
|
||||||
|
background-color: #cb0f0f;
|
||||||
|
}
|
||||||
42
src/Login.js
42
src/Login.js
@@ -13,13 +13,47 @@ const Login = () => {
|
|||||||
setFormData({ ...formData, [e.target.name]: e.target.value });
|
setFormData({ ...formData, [e.target.name]: e.target.value });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = (e) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (formData.username === 'dermalounge' && formData.password === '1234') {
|
try {
|
||||||
window.location.href = '/dashboard'; // redirect after successful login
|
const loginResponse = await fetch('https://bot.kediritechnopark.com/webhook/login', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(formData)
|
||||||
|
});
|
||||||
|
|
||||||
|
const loginDataRaw = await loginResponse.json();
|
||||||
|
const loginData = Array.isArray(loginDataRaw) ? loginDataRaw[0] : loginDataRaw;
|
||||||
|
|
||||||
|
if (loginData?.success && loginData?.token) {
|
||||||
|
localStorage.setItem('token', loginData.token);
|
||||||
|
|
||||||
|
const profileResponse = await fetch('https://bot.kediritechnopark.com/webhook/profile', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${loginData.token}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const profileDataRaw = await profileResponse.json();
|
||||||
|
const profileData = Array.isArray(profileDataRaw) ? profileDataRaw[0] : profileDataRaw;
|
||||||
|
|
||||||
|
if (profileData?.success) {
|
||||||
|
localStorage.setItem('user', JSON.stringify(profileData.user));
|
||||||
|
window.location.href = '/dashboard';
|
||||||
} else {
|
} else {
|
||||||
setError('Username atau password salah');
|
setError('Token tidak valid');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setError(loginData?.message || 'Username atau password salah');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Login Error:', err);
|
||||||
|
setError('Gagal terhubung ke server');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.button {
|
.button {
|
||||||
background-color: #4285F4;
|
background-color: #337f83;
|
||||||
color: white;
|
color: white;
|
||||||
border: none;
|
border: none;
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
@@ -58,7 +58,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.button:hover {
|
.button:hover {
|
||||||
background-color: #3367D6;
|
background-color: #3c9a9f;
|
||||||
}
|
}
|
||||||
|
|
||||||
.error {
|
.error {
|
||||||
|
|||||||
Reference in New Issue
Block a user