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 Logo from './assets/images/logo.svg?react';
import { checkAuth } from "./Components/UI/auth";
import axios from 'axios';
function App() {
const [authState, setAuthState] = useState({
@ -56,7 +57,7 @@ function App() {
const handleLogout = async () => {
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',
credentials: 'include'
});

View File

@ -3,6 +3,7 @@ import Modal from "./Modal";
import "../../Style/LoginModal.css";
import Logo from '../../assets/images/logo.svg?react';
import TextField from '@mui/material/TextField';
import axios from 'axios';
const LoginModal = ({ onLogin, onClose }) => {
const [username, setUsername] = useState("");
@ -16,7 +17,8 @@ const LoginModal = ({ onLogin, onClose }) => {
e.preventDefault();
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',
credentials: 'include',
headers: {

View File

@ -1,18 +1,23 @@
import axios from 'axios';
export const checkAuth = async () => {
try {
const response = await fetch('http://192.168.2.39:3000/api/auth/check', {
method: 'GET',
credentials: 'include', // Важно для отправки cookies
const response = await axios.get(
`${import.meta.env.VITE_BACK_URL}/api/auth/check`,
{
withCredentials: true, // аналог `credentials: 'include'` в fetch
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');
}
return await response.json();
} catch (err) {
console.error('Auth check failed:', err);
return { isAuthenticated: false };