corrections after the code review
test-org/trust-module-frontend/pipeline/pr-rc This commit looks good Details

authorization-token
DmitriyA 2025-04-21 09:01:36 -04:00
parent 54cf5504a4
commit b4d653f3a6
3 changed files with 26 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import LoginModal from "./Components/UI/LoginModal";
import { lightTheme, darkTheme } from "./Style/theme"; import { lightTheme, darkTheme } from "./Style/theme";
import Logo from './assets/images/logo.svg?react'; import Logo from './assets/images/logo.svg?react';
import { checkAuth } from "./Components/UI/auth"; import { checkAuth } from "./Components/UI/auth";
import axios from 'axios';
function App() { function App() {
const [authState, setAuthState] = useState({ const [authState, setAuthState] = useState({
@ -56,7 +57,7 @@ function App() {
const handleLogout = async () => { const handleLogout = async () => {
try { try {
await fetch('http://192.168.2.39:3000/api/auth/logout', { await axios.get(`${import.meta.env.VITE_BACK_URL}/api/metrics`, {
method: 'POST', method: 'POST',
credentials: 'include' credentials: 'include'
}); });

View File

@ -3,6 +3,7 @@ import Modal from "./Modal";
import "../../Style/LoginModal.css"; import "../../Style/LoginModal.css";
import Logo from '../../assets/images/logo.svg?react'; import Logo from '../../assets/images/logo.svg?react';
import TextField from '@mui/material/TextField'; import TextField from '@mui/material/TextField';
import axios from 'axios';
const LoginModal = ({ onLogin, onClose }) => { const LoginModal = ({ onLogin, onClose }) => {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
@ -16,7 +17,8 @@ const LoginModal = ({ onLogin, onClose }) => {
e.preventDefault(); e.preventDefault();
try { try {
const response = await fetch('http://192.168.2.39:3000/api/auth/login', { const response = await axios.post(
`${import.meta.env.VITE_BACK_URL}/api/auth/login`, {
method: 'POST', method: 'POST',
credentials: 'include', credentials: 'include',
headers: { headers: {

View File

@ -1,18 +1,23 @@
import axios from 'axios';
export const checkAuth = async () => { export const checkAuth = async () => {
try { try {
const response = await fetch('http://192.168.2.39:3000/api/auth/check', { const response = await axios.get(
method: 'GET', `${import.meta.env.VITE_BACK_URL}/api/auth/check`,
credentials: 'include', // Важно для отправки cookies {
withCredentials: true, // аналог `credentials: 'include'` в fetch
headers: { headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`, Authorization: `Bearer ${localStorage.getItem('access_token') || ''}`,
}, },
}); }
);
if (!response.ok) { // У axios нет свойства .ok, проверяем статус 200-299
if (response.status >= 200 && response.status < 300) {
return response.data; // Данные уже в JSON, не нужно .json()
} else {
throw new Error('Not authenticated'); throw new Error('Not authenticated');
} }
return await response.json();
} catch (err) { } catch (err) {
console.error('Auth check failed:', err); console.error('Auth check failed:', err);
return { isAuthenticated: false }; return { isAuthenticated: false };