Password reset: multi-app support with custom notification linking to correct frontend (app/portal/admin). Email change: self-service with password confirmation and admin-initiated, both sending verification to new address with 24h expiry. Confirmation sent to old email on completion. Password change: authenticated endpoint revoking other sessions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import axios from 'axios'
|
|
import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
|
|
import { useAuthStore } from '@/stores/useAuthStore'
|
|
|
|
const apiClient: AxiosInstance = axios.create({
|
|
baseURL: import.meta.env.VITE_API_URL,
|
|
withCredentials: true,
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
timeout: 30000,
|
|
})
|
|
|
|
apiClient.interceptors.request.use(
|
|
(config: InternalAxiosRequestConfig) => {
|
|
const authStore = useAuthStore()
|
|
|
|
if (authStore.token) {
|
|
config.headers.Authorization = `Bearer ${authStore.token}`
|
|
}
|
|
|
|
if (import.meta.env.DEV) {
|
|
console.log(`🚀 ${config.method?.toUpperCase()} ${config.url}`, config.data)
|
|
}
|
|
|
|
return config
|
|
},
|
|
error => Promise.reject(error),
|
|
)
|
|
|
|
apiClient.interceptors.response.use(
|
|
response => {
|
|
if (import.meta.env.DEV) {
|
|
console.log(`✅ ${response.status} ${response.config.url}`, response.data)
|
|
}
|
|
|
|
return response
|
|
},
|
|
error => {
|
|
if (import.meta.env.DEV) {
|
|
console.error(
|
|
`❌ ${error.response?.status} ${error.config?.url}`,
|
|
error.response?.data,
|
|
)
|
|
}
|
|
|
|
if (error.response?.status === 401) {
|
|
const authStore = useAuthStore()
|
|
|
|
if (authStore.isInitialized) {
|
|
authStore.clearLocalSession()
|
|
|
|
if (typeof window !== 'undefined') {
|
|
const path = window.location.pathname
|
|
const publicPaths = ['/login', '/wachtwoord-vergeten', '/wachtwoord-resetten', '/verify-email-change']
|
|
if (!publicPaths.some(p => path.startsWith(p)) && !path.startsWith('/register')) {
|
|
window.location.href = '/login'
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Promise.reject(error)
|
|
},
|
|
)
|
|
|
|
export { apiClient }
|