opt for prod
This commit is contained in:
parent
d9f9aaedc5
commit
2958bf69ed
@ -36,3 +36,6 @@ ADMIN_EMAIL=taratur@gmail.com
|
||||
DEFAULT_ADMIN_EMAIL=taratur@gmail.com
|
||||
DEFAULT_ADMIN_PASSWORD=irina7654321
|
||||
DEFAULT_ADMIN_NAME=admin
|
||||
|
||||
CORS_ORIGIN=http://localhost:5173
|
||||
NODE_ENV=development
|
||||
|
||||
179
backend/package-lock.json
generated
179
backend/package-lock.json
generated
@ -32,6 +32,7 @@
|
||||
"passport-local": "^1.0.0",
|
||||
"pg": "^8.13.1",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^5.0.0",
|
||||
"rxjs": "^7.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -41,7 +42,7 @@
|
||||
"@types/bcrypt": "^5.0.2",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^20.3.1",
|
||||
"@types/node": "^20.17.28",
|
||||
"@types/passport-jwt": "^4.0.1",
|
||||
"@types/passport-local": "^1.0.38",
|
||||
"@types/supertest": "^2.0.12",
|
||||
@ -2947,6 +2948,65 @@
|
||||
"node-pre-gyp": "bin/node-pre-gyp"
|
||||
}
|
||||
},
|
||||
"node_modules/@mapbox/node-pre-gyp/node_modules/brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"balanced-match": "^1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@mapbox/node-pre-gyp/node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
|
||||
"deprecated": "Glob versions prior to v9 are no longer supported",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"fs.realpath": "^1.0.0",
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "^3.1.1",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/@mapbox/node-pre-gyp/node_modules/minimatch": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
|
||||
"integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"brace-expansion": "^1.1.7"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@mapbox/node-pre-gyp/node_modules/rimraf": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
|
||||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
|
||||
"deprecated": "Rimraf versions prior to v4 are no longer supported",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"glob": "^7.1.3"
|
||||
},
|
||||
"bin": {
|
||||
"rimraf": "bin.js"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/@nestjs/cli": {
|
||||
"version": "10.4.9",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-10.4.9.tgz",
|
||||
@ -7378,6 +7438,69 @@
|
||||
"node": "^10.12.0 || >=12.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/flat-cache/node_modules/brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"balanced-match": "^1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/flat-cache/node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
|
||||
"deprecated": "Glob versions prior to v9 are no longer supported",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"fs.realpath": "^1.0.0",
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "^3.1.1",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/flat-cache/node_modules/minimatch": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
|
||||
"integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"brace-expansion": "^1.1.7"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/flat-cache/node_modules/rimraf": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
|
||||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
|
||||
"deprecated": "Rimraf versions prior to v4 are no longer supported",
|
||||
"dev": true,
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"glob": "^7.1.3"
|
||||
},
|
||||
"bin": {
|
||||
"rimraf": "bin.js"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/flatted": {
|
||||
"version": "3.3.3",
|
||||
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.3.tgz",
|
||||
@ -10825,64 +10948,20 @@
|
||||
}
|
||||
},
|
||||
"node_modules/rimraf": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
|
||||
"integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
|
||||
"deprecated": "Rimraf versions prior to v4 are no longer supported",
|
||||
"version": "5.0.10",
|
||||
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-5.0.10.tgz",
|
||||
"integrity": "sha512-l0OE8wL34P4nJH/H2ffoaniAokM2qSmrtXHmlpvYr5AVVX8msAyW0l8NVJFDxlSK4u3Uh/f41cQheDVdnYijwQ==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"glob": "^7.1.3"
|
||||
"glob": "^10.3.7"
|
||||
},
|
||||
"bin": {
|
||||
"rimraf": "bin.js"
|
||||
"rimraf": "dist/esm/bin.mjs"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/rimraf/node_modules/brace-expansion": {
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"balanced-match": "^1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/rimraf/node_modules/glob": {
|
||||
"version": "7.2.3",
|
||||
"resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
|
||||
"integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
|
||||
"deprecated": "Glob versions prior to v9 are no longer supported",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"fs.realpath": "^1.0.0",
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "^3.1.1",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/isaacs"
|
||||
}
|
||||
},
|
||||
"node_modules/rimraf/node_modules/minimatch": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
|
||||
"integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"brace-expansion": "^1.1.7"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/run-async": {
|
||||
"version": "2.4.1",
|
||||
"resolved": "https://registry.npmjs.org/run-async/-/run-async-2.4.1.tgz",
|
||||
|
||||
@ -57,7 +57,7 @@
|
||||
"@types/bcrypt": "^5.0.2",
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/node": "^20.3.1",
|
||||
"@types/node": "^20.17.28",
|
||||
"@types/passport-jwt": "^4.0.1",
|
||||
"@types/passport-local": "^1.0.38",
|
||||
"@types/supertest": "^2.0.12",
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// src/main.ts
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { AppModule } from "./app.module";
|
||||
import { Logger, ValidationPipe } from "@nestjs/common";
|
||||
@ -15,9 +14,26 @@ async function bootstrap() {
|
||||
|
||||
// Enable CORS
|
||||
app.enableCors({
|
||||
origin: true,
|
||||
origin: [
|
||||
"https://www.placebo.mk",
|
||||
"https://placebo.mk",
|
||||
"http://localhost:5173",
|
||||
],
|
||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
||||
credentials: true,
|
||||
allowedHeaders: [
|
||||
"Origin",
|
||||
"X-Requested-With",
|
||||
"Content-Type",
|
||||
"Accept",
|
||||
"Authorization",
|
||||
],
|
||||
exposedHeaders: [
|
||||
'Access-Control-Allow-Origin',
|
||||
'Access-Control-Allow-Credentials',
|
||||
],
|
||||
preflightContinue: false,
|
||||
optionsSuccessStatus: 204,
|
||||
});
|
||||
|
||||
// Global pipes
|
||||
|
||||
@ -2,6 +2,11 @@ version: "3.8"
|
||||
|
||||
services:
|
||||
backend:
|
||||
logging:
|
||||
driver: "json-file"
|
||||
options:
|
||||
max-size: "10m"
|
||||
max-file: "3"
|
||||
container_name: imk-backend
|
||||
build:
|
||||
context: ./backend
|
||||
@ -12,6 +17,7 @@ services:
|
||||
- NODE_ENV=production
|
||||
- PORT=3000
|
||||
- DATABASE_URL=postgresql://postgres:postgres@imk-postgres:5432/postgres?schema=public
|
||||
- FRONTEND_URL=https://www.placebo.mk
|
||||
env_file:
|
||||
- .env
|
||||
deploy:
|
||||
@ -40,7 +46,7 @@ services:
|
||||
timeout: 10s
|
||||
retries: 3
|
||||
start_period: 15s
|
||||
restart: unless-stopped
|
||||
restart: always
|
||||
postgres:
|
||||
container_name: imk-postgres
|
||||
image: postgres:14-alpine
|
||||
@ -60,7 +66,7 @@ services:
|
||||
retries: 5
|
||||
networks:
|
||||
- app_network
|
||||
restart: unless-stopped
|
||||
restart: always
|
||||
|
||||
redis:
|
||||
container_name: imk-redis
|
||||
@ -77,7 +83,7 @@ services:
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
restart: unless-stopped
|
||||
restart: always
|
||||
|
||||
networks:
|
||||
app_network:
|
||||
|
||||
@ -1,28 +1,41 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { getAllUsers, getAllDocuments, getUserInfo, createUser, resetUserPassword } from '../../services/api';
|
||||
import DocumentUpload from '../documentUpload/DocumentUpload';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { FiUsers, FiFile, FiUpload, FiUserPlus, FiLoader, FiKey } from 'react-icons/fi';
|
||||
import { useState, useEffect } from "react";
|
||||
import {
|
||||
getAllUsers,
|
||||
getAllDocuments,
|
||||
getUserInfo,
|
||||
createUser,
|
||||
resetUserPassword,
|
||||
} from "../../services/api";
|
||||
import DocumentUpload from "../documentUpload/DocumentUpload";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import {
|
||||
FiUsers,
|
||||
FiFile,
|
||||
FiUpload,
|
||||
FiUserPlus,
|
||||
FiLoader,
|
||||
FiKey,
|
||||
} from "react-icons/fi";
|
||||
|
||||
function AdminPanel() {
|
||||
const navigate = useNavigate();
|
||||
const [activeTab, setActiveTab] = useState('documents');
|
||||
const [activeTab, setActiveTab] = useState("documents");
|
||||
const [users, setUsers] = useState([]);
|
||||
const [documents, setDocuments] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState('');
|
||||
const [error, setError] = useState("");
|
||||
const [isAdmin, setIsAdmin] = useState(false);
|
||||
const [resetPasswordModal, setResetPasswordModal] = useState({
|
||||
isOpen: false,
|
||||
userId: null,
|
||||
userName: '',
|
||||
newPassword: '',
|
||||
userName: "",
|
||||
newPassword: "",
|
||||
});
|
||||
const [newUser, setNewUser] = useState({
|
||||
name: '',
|
||||
email: '',
|
||||
password: '',
|
||||
isAdmin: false
|
||||
name: "",
|
||||
email: "",
|
||||
password: "",
|
||||
isAdmin: false,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
@ -39,32 +52,32 @@ function AdminPanel() {
|
||||
try {
|
||||
const response = await getUserInfo();
|
||||
if (!response?.data?.isAdmin) {
|
||||
navigate('/');
|
||||
navigate("/");
|
||||
} else {
|
||||
setIsAdmin(true);
|
||||
await fetchData();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Admin check failed:', error);
|
||||
navigate('/');
|
||||
// console.error("Admin check failed:", error);
|
||||
navigate("/");
|
||||
}
|
||||
};
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
setError("");
|
||||
try {
|
||||
if (activeTab === 'users') {
|
||||
if (activeTab === "users") {
|
||||
const response = await getAllUsers();
|
||||
setUsers(response.data);
|
||||
} else if (activeTab === 'documents') {
|
||||
} else if (activeTab === "documents") {
|
||||
const response = await getAllDocuments();
|
||||
console.log('Documents data:', response.data);
|
||||
// console.log('Documents data:', response.data);
|
||||
setDocuments(response.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Fetch error:', err);
|
||||
setError('Failed to fetch data. Please try again.');
|
||||
// console.error("Fetch error:", err);
|
||||
setError("Failed to fetch data. Please try again.");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@ -75,41 +88,44 @@ function AdminPanel() {
|
||||
try {
|
||||
await createUser(newUser);
|
||||
setNewUser({
|
||||
name: '',
|
||||
email: '',
|
||||
password: '',
|
||||
isAdmin: false
|
||||
name: "",
|
||||
email: "",
|
||||
password: "",
|
||||
isAdmin: false,
|
||||
});
|
||||
fetchData();
|
||||
} catch (err) {
|
||||
setError('Failed to create user');
|
||||
setError("Failed to create user");
|
||||
}
|
||||
};
|
||||
|
||||
const handleResetPassword = async (e) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
await resetUserPassword(resetPasswordModal.userId, resetPasswordModal.newPassword);
|
||||
await resetUserPassword(
|
||||
resetPasswordModal.userId,
|
||||
resetPasswordModal.newPassword,
|
||||
);
|
||||
setResetPasswordModal({
|
||||
isOpen: false,
|
||||
userId: null,
|
||||
userName: '',
|
||||
newPassword: '',
|
||||
userName: "",
|
||||
newPassword: "",
|
||||
});
|
||||
// Show success message
|
||||
setError('Password reset successful');
|
||||
setTimeout(() => setError(''), 3000);
|
||||
setError("Password reset successful");
|
||||
setTimeout(() => setError(""), 3000);
|
||||
} catch (err) {
|
||||
setError('Failed to reset password');
|
||||
setError("Failed to reset password");
|
||||
}
|
||||
};
|
||||
|
||||
if (!isAdmin) return null;
|
||||
|
||||
const tabs = [
|
||||
{ id: 'documents', name: 'Documents', icon: FiFile },
|
||||
{ id: 'users', name: 'Users', icon: FiUsers },
|
||||
{ id: 'upload', name: 'Upload Document', icon: FiUpload }
|
||||
{ id: "documents", name: "Documents", icon: FiFile },
|
||||
{ id: "users", name: "Users", icon: FiUsers },
|
||||
{ id: "upload", name: "Upload Document", icon: FiUpload },
|
||||
];
|
||||
|
||||
if (loading) {
|
||||
@ -127,7 +143,9 @@ function AdminPanel() {
|
||||
<div className="min-h-screen bg-gradient-to-br from-primary-900 to-primary-800 p-6">
|
||||
<div className="max-w-7xl mx-auto">
|
||||
<header className="mb-8 mt-20">
|
||||
<h1 className="text-3xl font-bold text-white mb-2">Admin Dashboard</h1>
|
||||
<h1 className="text-3xl font-bold text-white mb-2">
|
||||
Admin Dashboard
|
||||
</h1>
|
||||
<p className="text-neutral-400">Manage users and documents</p>
|
||||
</header>
|
||||
|
||||
@ -139,9 +157,11 @@ function AdminPanel() {
|
||||
onClick={() => setActiveTab(id)}
|
||||
className={`
|
||||
px-4 py-2 rounded-lg flex items-center space-x-2 transition-colors
|
||||
${activeTab === id
|
||||
? 'bg-primary-600 text-white'
|
||||
: 'text-neutral-400 hover:bg-primary-700/50 hover:text-white'}
|
||||
${
|
||||
activeTab === id
|
||||
? "bg-primary-600 text-white"
|
||||
: "text-neutral-400 hover:bg-primary-700/50 hover:text-white"
|
||||
}
|
||||
`}
|
||||
>
|
||||
<Icon className="w-4 h-4" />
|
||||
@ -167,11 +187,13 @@ function AdminPanel() {
|
||||
type="password"
|
||||
placeholder="New Password"
|
||||
value={resetPasswordModal.newPassword}
|
||||
onChange={(e) => setResetPasswordModal({
|
||||
...resetPasswordModal,
|
||||
newPassword: e.target.value
|
||||
})}
|
||||
className="w-full bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
onChange={(e) =>
|
||||
setResetPasswordModal({
|
||||
...resetPasswordModal,
|
||||
newPassword: e.target.value,
|
||||
})
|
||||
}
|
||||
className="w-full bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
text-white placeholder-neutral-400 focus:outline-none focus:border-primary-500
|
||||
focus:ring-1 focus:ring-primary-500"
|
||||
required
|
||||
@ -180,20 +202,22 @@ function AdminPanel() {
|
||||
<div className="flex justify-end space-x-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setResetPasswordModal({
|
||||
isOpen: false,
|
||||
userId: null,
|
||||
userName: '',
|
||||
newPassword: '',
|
||||
})}
|
||||
onClick={() =>
|
||||
setResetPasswordModal({
|
||||
isOpen: false,
|
||||
userId: null,
|
||||
userName: "",
|
||||
newPassword: "",
|
||||
})
|
||||
}
|
||||
className="px-4 py-2 text-neutral-400 hover:text-white transition-colors"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
className="flex items-center justify-center space-x-2 px-4 py-2
|
||||
bg-primary-600 hover:bg-primary-700 text-white rounded-lg
|
||||
className="flex items-center justify-center space-x-2 px-4 py-2
|
||||
bg-primary-600 hover:bg-primary-700 text-white rounded-lg
|
||||
transition-colors shadow-lg"
|
||||
>
|
||||
<FiKey className="w-4 h-4" />
|
||||
@ -206,30 +230,48 @@ function AdminPanel() {
|
||||
)}
|
||||
|
||||
<div className="grid gap-6">
|
||||
{activeTab === 'documents' && (
|
||||
{activeTab === "documents" && (
|
||||
<div className="bg-primary-800/50 backdrop-blur-lg rounded-xl overflow-hidden shadow-xl">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b border-primary-700">
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Title</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Status</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Uploaded By</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Shared With</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Created At</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Title
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Status
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Uploaded By
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Shared With
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Created At
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{documents.map((doc) => (
|
||||
<tr key={doc.id} className="border-b border-primary-700/50 hover:bg-primary-700/30">
|
||||
<tr
|
||||
key={doc.id}
|
||||
className="border-b border-primary-700/50 hover:bg-primary-700/30"
|
||||
>
|
||||
<td className="px-6 py-4 text-white">{doc.title}</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-medium ${
|
||||
doc.status === 'completed' ? 'bg-green-500/20 text-green-300' :
|
||||
doc.status === 'pending' ? 'bg-yellow-500/20 text-yellow-300' :
|
||||
doc.status === 'uploading' ? 'bg-primary-500/20 text-primary-300' :
|
||||
'bg-neutral-500/20 text-neutral-300'
|
||||
}`}>
|
||||
<span
|
||||
className={`px-3 py-1 rounded-full text-xs font-medium ${
|
||||
doc.status === "completed"
|
||||
? "bg-green-500/20 text-green-300"
|
||||
: doc.status === "pending"
|
||||
? "bg-yellow-500/20 text-yellow-300"
|
||||
: doc.status === "uploading"
|
||||
? "bg-primary-500/20 text-primary-300"
|
||||
: "bg-neutral-500/20 text-neutral-300"
|
||||
}`}
|
||||
>
|
||||
{doc.status}
|
||||
</span>
|
||||
</td>
|
||||
@ -237,13 +279,16 @@ function AdminPanel() {
|
||||
{doc.uploadedBy?.name} ({doc.uploadedBy?.email})
|
||||
</td>
|
||||
<td className="px-6 py-4 text-neutral-300">
|
||||
{doc.sharedWith && doc.sharedWith.length > 0
|
||||
? doc.sharedWith.map(user => (
|
||||
<div key={user.id} className="whitespace-nowrap">
|
||||
{doc.sharedWith && doc.sharedWith.length > 0
|
||||
? doc.sharedWith.map((user) => (
|
||||
<div
|
||||
key={user.id}
|
||||
className="whitespace-nowrap"
|
||||
>
|
||||
{user.name} ({user.email})
|
||||
</div>
|
||||
))
|
||||
: 'None'}
|
||||
: "None"}
|
||||
</td>
|
||||
<td className="px-6 py-4 text-neutral-300">
|
||||
{new Date(doc.createdAt).toLocaleString()}
|
||||
@ -256,17 +301,24 @@ function AdminPanel() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{activeTab === 'users' && (
|
||||
{activeTab === "users" && (
|
||||
<>
|
||||
<div className="bg-primary-800/50 backdrop-blur-lg rounded-xl p-6 mb-6 shadow-xl">
|
||||
<h2 className="text-xl font-bold text-white mb-4">Create New User</h2>
|
||||
<form onSubmit={handleCreateUser} className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
<h2 className="text-xl font-bold text-white mb-4">
|
||||
Create New User
|
||||
</h2>
|
||||
<form
|
||||
onSubmit={handleCreateUser}
|
||||
className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Name"
|
||||
value={newUser.name}
|
||||
onChange={(e) => setNewUser({ ...newUser, name: e.target.value })}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
onChange={(e) =>
|
||||
setNewUser({ ...newUser, name: e.target.value })
|
||||
}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
text-white placeholder-neutral-400 focus:outline-none focus:border-primary-500
|
||||
focus:ring-1 focus:ring-primary-500"
|
||||
required
|
||||
@ -275,8 +327,10 @@ function AdminPanel() {
|
||||
type="email"
|
||||
placeholder="Email"
|
||||
value={newUser.email}
|
||||
onChange={(e) => setNewUser({ ...newUser, email: e.target.value })}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
onChange={(e) =>
|
||||
setNewUser({ ...newUser, email: e.target.value })
|
||||
}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
text-white placeholder-neutral-400 focus:outline-none focus:border-primary-500
|
||||
focus:ring-1 focus:ring-primary-500"
|
||||
required
|
||||
@ -285,8 +339,10 @@ function AdminPanel() {
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
value={newUser.password}
|
||||
onChange={(e) => setNewUser({ ...newUser, password: e.target.value })}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
onChange={(e) =>
|
||||
setNewUser({ ...newUser, password: e.target.value })
|
||||
}
|
||||
className="bg-primary-700/30 border border-primary-600 rounded-lg px-4 py-2
|
||||
text-white placeholder-neutral-400 focus:outline-none focus:border-primary-500
|
||||
focus:ring-1 focus:ring-primary-500"
|
||||
required
|
||||
@ -296,16 +352,20 @@ function AdminPanel() {
|
||||
type="checkbox"
|
||||
id="isAdmin"
|
||||
checked={newUser.isAdmin}
|
||||
onChange={(e) => setNewUser({ ...newUser, isAdmin: e.target.checked })}
|
||||
className="rounded border-primary-600 bg-primary-700/30 text-primary-500
|
||||
onChange={(e) =>
|
||||
setNewUser({ ...newUser, isAdmin: e.target.checked })
|
||||
}
|
||||
className="rounded border-primary-600 bg-primary-700/30 text-primary-500
|
||||
focus:ring-primary-500"
|
||||
/>
|
||||
<label htmlFor="isAdmin" className="text-white">Is Admin</label>
|
||||
<label htmlFor="isAdmin" className="text-white">
|
||||
Is Admin
|
||||
</label>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
className="flex items-center justify-center space-x-2 px-4 py-2
|
||||
bg-primary-600 hover:bg-primary-700 text-white rounded-lg
|
||||
className="flex items-center justify-center space-x-2 px-4 py-2
|
||||
bg-primary-600 hover:bg-primary-700 text-white rounded-lg
|
||||
transition-colors shadow-lg"
|
||||
>
|
||||
<FiUserPlus className="w-4 h-4" />
|
||||
@ -318,31 +378,46 @@ function AdminPanel() {
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b border-primary-700">
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Name</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Email</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Role</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">Actions</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Name
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Email
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Role
|
||||
</th>
|
||||
<th className="px-6 py-4 text-left text-sm text-neutral-400">
|
||||
Actions
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{users.map((user) => (
|
||||
<tr key={user.id} className="border-b border-primary-700/50 hover:bg-primary-700/30">
|
||||
<tr
|
||||
key={user.id}
|
||||
className="border-b border-primary-700/50 hover:bg-primary-700/30"
|
||||
>
|
||||
<td className="px-6 py-4 text-white">{user.name}</td>
|
||||
<td className="px-6 py-4 text-white">{user.email}</td>
|
||||
<td className="px-6 py-4">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-medium
|
||||
${user.isAdmin ? 'bg-primary-500/20 text-primary-300' : 'bg-neutral-500/20 text-neutral-300'}`}>
|
||||
{user.isAdmin ? 'Admin' : 'User'}
|
||||
<span
|
||||
className={`px-3 py-1 rounded-full text-xs font-medium
|
||||
${user.isAdmin ? "bg-primary-500/20 text-primary-300" : "bg-neutral-500/20 text-neutral-300"}`}
|
||||
>
|
||||
{user.isAdmin ? "Admin" : "User"}
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<button
|
||||
onClick={() => setResetPasswordModal({
|
||||
isOpen: true,
|
||||
userId: user.id,
|
||||
userName: user.name,
|
||||
newPassword: '',
|
||||
})}
|
||||
onClick={() =>
|
||||
setResetPasswordModal({
|
||||
isOpen: true,
|
||||
userId: user.id,
|
||||
userName: user.name,
|
||||
newPassword: "",
|
||||
})
|
||||
}
|
||||
className="flex items-center space-x-1 text-neutral-400 hover:text-white transition-colors"
|
||||
>
|
||||
<FiKey className="w-4 h-4" />
|
||||
@ -357,7 +432,7 @@ function AdminPanel() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{activeTab === 'upload' && (
|
||||
{activeTab === "upload" && (
|
||||
<div className="bg-primary-800/50 backdrop-blur-lg rounded-xl p-6 shadow-xl">
|
||||
<DocumentUpload />
|
||||
</div>
|
||||
@ -368,4 +443,4 @@ function AdminPanel() {
|
||||
);
|
||||
}
|
||||
|
||||
export default AdminPanel;
|
||||
export default AdminPanel;
|
||||
|
||||
70
frontend/src/hooks/_useAuth.jsx
Normal file
70
frontend/src/hooks/_useAuth.jsx
Normal file
@ -0,0 +1,70 @@
|
||||
import { createContext, useContext, useState, useEffect } from "react";
|
||||
import api from "../services/api";
|
||||
|
||||
const AuthContext = createContext(null);
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [user, setUser] = useState(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUser = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await api.get("/auth/user-info"); // Updated endpoint
|
||||
setUser(response.data);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch user info:", error);
|
||||
localStorage.removeItem("token");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchUser();
|
||||
}, []);
|
||||
|
||||
const login = async (username, password) => {
|
||||
try {
|
||||
const response = await api.post("/auth/login", { username, password });
|
||||
console.log("Login response:", response.data); // Debug log
|
||||
|
||||
const { access_token } = response.data;
|
||||
localStorage.setItem("token", access_token);
|
||||
|
||||
// Fetch user info after login
|
||||
const userResponse = await api.get("/auth/user-info");
|
||||
const userData = userResponse.data;
|
||||
|
||||
setUser(userData);
|
||||
return userData; // Return the user data for redirect logic
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem("token");
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, isLoading, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
@ -1,3 +1,4 @@
|
||||
// frontend/src/hooks/useAuth.jsx
|
||||
import { createContext, useContext, useState, useEffect } from "react";
|
||||
import api from "../services/api";
|
||||
|
||||
@ -6,6 +7,7 @@ const AuthContext = createContext(null);
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [user, setUser] = useState(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUser = async () => {
|
||||
@ -16,11 +18,12 @@ export const AuthProvider = ({ children }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await api.get("/auth/user-info"); // Updated endpoint
|
||||
const response = await api.get("/auth/user-info");
|
||||
setUser(response.data);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch user info:", error);
|
||||
localStorage.removeItem("token");
|
||||
setError(error.message);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@ -32,17 +35,15 @@ export const AuthProvider = ({ children }) => {
|
||||
const login = async (username, password) => {
|
||||
try {
|
||||
const response = await api.post("/auth/login", { username, password });
|
||||
console.log("Login response:", response.data); // Debug log
|
||||
|
||||
const { access_token } = response.data;
|
||||
|
||||
localStorage.setItem("token", access_token);
|
||||
|
||||
// Fetch user info after login
|
||||
// Fetch user info after successful login
|
||||
const userResponse = await api.get("/auth/user-info");
|
||||
const userData = userResponse.data;
|
||||
setUser(userResponse.data);
|
||||
|
||||
setUser(userData);
|
||||
return userData; // Return the user data for redirect logic
|
||||
return userResponse.data;
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
throw error;
|
||||
@ -55,7 +56,7 @@ export const AuthProvider = ({ children }) => {
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, isLoading, login, logout }}>
|
||||
<AuthContext.Provider value={{ user, isLoading, error, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
import axios from "axios";
|
||||
|
||||
const API_URL = "http://localhost:3000";
|
||||
const API_URL =
|
||||
process.env.NODE_ENV === "production"
|
||||
? "https://imkapi.oblak.solutions"
|
||||
: "http://localhost:3000";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
@ -112,5 +115,14 @@ export const forgotPassword = (email) =>
|
||||
api.post("/auth/forgot-password", { email });
|
||||
export const resetPassword = (token, newPassword) =>
|
||||
api.post("/auth/reset-password", { token, newPassword });
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem("token");
|
||||
window.location.href = "/login";
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
export default api;
|
||||
|
||||
@ -4,14 +4,29 @@ import react from "@vitejs/plugin-react";
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
"/api": {
|
||||
target: "http://localhost:3000",
|
||||
changeOrigin: true,
|
||||
secure: false,
|
||||
// server: {
|
||||
// port: 5173,
|
||||
// proxy: {
|
||||
// "/api": {
|
||||
// target: "http://localhost:3000",
|
||||
// changeOrigin: true,
|
||||
// secure: false,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
build: {
|
||||
outDir: "dist",
|
||||
sourcemap: false,
|
||||
rollupOptions: {
|
||||
output: {
|
||||
manualChunks: {
|
||||
vendor: ["react", "react-dom", "react-router-dom"],
|
||||
ui: ["@headlessui/react", "@heroicons/react"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
define: {
|
||||
"process.env.API_URL": JSON.stringify("https://imkapi.oblak.solutions"),
|
||||
},
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user