ready to deployment
This commit is contained in:
38
frontend/src/api/api.js
Normal file
38
frontend/src/api/api.js
Normal file
@@ -0,0 +1,38 @@
|
||||
// src/api/api.js
|
||||
import axios from 'axios'
|
||||
import router from '@/router'
|
||||
import { useAuthStore } from '@/stores/authStore'
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: 'http://localhost:8000', // Twój backend
|
||||
})
|
||||
|
||||
// Request interceptor – dodawanie tokena
|
||||
api.interceptors.request.use((config) => {
|
||||
// const token = localStorage.getItem('token')
|
||||
// if (token) {
|
||||
const authStore = useAuthStore()
|
||||
if (authStore.token) {
|
||||
config.headers.Authorization = `Bearer ${authStore.token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
// Response interceptor – obsługa błędów
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
const authStore = useAuthStore()
|
||||
|
||||
if (error.response && error.response.status === 401) {
|
||||
authStore.logout()
|
||||
|
||||
router.push('/')
|
||||
// Używamy redirectu bez routera (bo nie zawsze mamy go w tym miejscu)
|
||||
// window.location.href = '/'
|
||||
}
|
||||
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
export default api
|
||||
11
frontend/src/api/authApi.js
Normal file
11
frontend/src/api/authApi.js
Normal file
@@ -0,0 +1,11 @@
|
||||
// src/api/authApi.js
|
||||
import api from './api'
|
||||
|
||||
export const login = async (email, password) => {
|
||||
const params = new URLSearchParams()
|
||||
params.append('username', email)
|
||||
params.append('password', password)
|
||||
|
||||
const res = await api.post('/token', params)
|
||||
return res.data // { access_token: "...", token_type: "bearer" }
|
||||
}
|
||||
7
frontend/src/api/dataApi.js
Normal file
7
frontend/src/api/dataApi.js
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/api/dataApi.js
|
||||
import api from './api'
|
||||
|
||||
export const getData = async (year, month) => {
|
||||
const res = await api.get(`/data/${year}/${month}`)
|
||||
return res.data
|
||||
}
|
||||
19
frontend/src/api/mock_response.js
Normal file
19
frontend/src/api/mock_response.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import may from '@/assets/may.json'
|
||||
import june from '@/assets/june.json'
|
||||
import july from '@/assets/july.json'
|
||||
|
||||
export default {
|
||||
getResponse(month) {
|
||||
if (!month || typeof month !== 'string' || month.trim() === '' || month === 'lipiec') {
|
||||
return july
|
||||
}
|
||||
|
||||
if (month === 'czerwiec') {
|
||||
return june
|
||||
}
|
||||
|
||||
if (month === 'maj') {
|
||||
return may
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user