Feature: System settings in DB - configurable backend URLs and API key
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { create } from 'zustand'
|
||||
import api from '../api/client'
|
||||
import { useSettingsStore } from './settings'
|
||||
|
||||
export const useAuthStore = create((set) => ({
|
||||
user: null,
|
||||
@@ -9,6 +10,8 @@ export const useAuthStore = create((set) => ({
|
||||
login: async (username, password) => {
|
||||
const data = await api.login(username, password)
|
||||
set({ user: data.user, isAuthenticated: true })
|
||||
// Load system settings after login
|
||||
useSettingsStore.getState().fetchSettings()
|
||||
return data
|
||||
},
|
||||
|
||||
@@ -25,6 +28,7 @@ export const useAuthStore = create((set) => ({
|
||||
try {
|
||||
const data = await api.me()
|
||||
set({ user: data.user || data, isAuthenticated: true, loading: false })
|
||||
useSettingsStore.getState().fetchSettings()
|
||||
} catch {
|
||||
api.logout()
|
||||
set({ user: null, isAuthenticated: false, loading: false })
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { create } from 'zustand'
|
||||
import api from '../api/client'
|
||||
|
||||
export const useSettingsStore = create((set, get) => ({
|
||||
settings: {},
|
||||
loaded: false,
|
||||
|
||||
fetchSettings: async () => {
|
||||
try {
|
||||
const resp = await api.getSettings()
|
||||
set({ settings: resp.data || {}, loaded: true })
|
||||
} catch {
|
||||
// ignore - settings not available before login
|
||||
}
|
||||
},
|
||||
|
||||
getSetting: (key, fallback = '') => {
|
||||
return get().settings[key] || fallback
|
||||
},
|
||||
|
||||
getBackendHost: () => {
|
||||
const url = get().settings.backend_url_internal || ''
|
||||
try {
|
||||
return new URL(url).hostname
|
||||
} catch {
|
||||
// fallback: strip protocol and port manually
|
||||
return url.replace(/^https?:\/\//, '').replace(/:\d+$/, '')
|
||||
}
|
||||
},
|
||||
}))
|
||||
Reference in New Issue
Block a user