navidrome/ui/src/App.jsx
Kartik Ohri 362a8826b9 Implement OpenSubsonic's API Key authentication
Ref: https://opensubsonic.netlify.app/docs/extensions/apikeyauth/.

Add functionality for users to create, manage, and use API keys for authentication.

*   **Database:**
    *   Added a new `api_key`(`20250415111500_add_api_key_table.go`) table to store API keys associated with users.
*   **Backend:**
    *   Defined the `APIKey` model.
    *   Implemented `APIKeyRepository` for database operations.
    *   Added `FindByAPIKey` method to `UserRepository` to allow user lookup via API key.
    *   Updated Subsonic API error responses to include API key-related errors and messaging.
    *   Integrated API key authentication into the subsonic authentication middleware.
*   **UI:**
    *   Added new React components for API Key CRUD operations: `ApiKeyList`, `ApiKeyCreate`, and `ApiKeyEdit`.
    *   The API Key management section can be accessed from the settings menu.

# Conflicts:
#	model/user.go
#	server/subsonic/middlewares.go
#	server/subsonic/middlewares_test.go
#	tests/mock_data_store.go
#	tests/mock_user_repo.go
2025-08-14 02:18:11 +05:30

179 lines
4.9 KiB
JavaScript

import ReactGA from 'react-ga'
import { Provider } from 'react-redux'
import { createHashHistory } from 'history'
import { Admin as RAAdmin, Resource } from 'react-admin'
import { HotKeys } from 'react-hotkeys'
import dataProvider from './dataProvider'
import authProvider from './authProvider'
import { Layout, Login, Logout } from './layout'
import transcoding from './transcoding'
import player from './player'
import user from './user'
import song from './song'
import album from './album'
import artist from './artist'
import playlist from './playlist'
import radio from './radio'
import share from './share'
import library from './library'
import { Player } from './audioplayer'
import customRoutes from './routes'
import {
libraryReducer,
themeReducer,
addToPlaylistDialogReducer,
expandInfoDialogReducer,
listenBrainzTokenDialogReducer,
saveQueueDialogReducer,
playerReducer,
albumViewReducer,
activityReducer,
settingsReducer,
replayGainReducer,
downloadMenuDialogReducer,
shareDialogReducer,
} from './reducers'
import createAdminStore from './store/createAdminStore'
import { i18nProvider } from './i18n'
import config, { shareInfo } from './config'
import { keyMap } from './hotkeys'
import useChangeThemeColor from './useChangeThemeColor'
import SharePlayer from './share/SharePlayer'
import { HTML5Backend } from 'react-dnd-html5-backend'
import { DndProvider } from 'react-dnd'
import missing from './missing/index.js'
import apikey from './apikey/index.js'
const history = createHashHistory()
if (config.gaTrackingId) {
ReactGA.initialize(config.gaTrackingId)
history.listen((location) => {
ReactGA.pageview(location.pathname)
})
ReactGA.pageview(window.location.pathname)
}
const adminStore = createAdminStore({
authProvider,
dataProvider,
history,
customReducers: {
library: libraryReducer,
player: playerReducer,
albumView: albumViewReducer,
theme: themeReducer,
addToPlaylistDialog: addToPlaylistDialogReducer,
downloadMenuDialog: downloadMenuDialogReducer,
expandInfoDialog: expandInfoDialogReducer,
listenBrainzTokenDialog: listenBrainzTokenDialogReducer,
saveQueueDialog: saveQueueDialogReducer,
shareDialog: shareDialogReducer,
activity: activityReducer,
settings: settingsReducer,
replayGain: replayGainReducer,
},
})
const App = () => (
<Provider store={adminStore}>
<Admin />
</Provider>
)
const Admin = (props) => {
useChangeThemeColor()
/* eslint-disable react/jsx-key */
return (
<RAAdmin
disableTelemetry
dataProvider={dataProvider}
authProvider={authProvider}
i18nProvider={i18nProvider}
customRoutes={customRoutes}
history={history}
layout={Layout}
loginPage={Login}
logoutButton={Logout}
{...props}
>
{(permissions) => [
<Resource name="album" {...album} options={{ subMenu: 'albumList' }} />,
<Resource name="artist" {...artist} />,
<Resource name="song" {...song} />,
<Resource
name="radio"
{...(permissions === 'admin' ? radio.admin : radio.all)}
/>,
config.enableSharing && <Resource name="share" {...share} />,
<Resource
name="playlist"
{...playlist}
options={{ subMenu: 'playlist' }}
/>,
<Resource name="user" {...user} options={{ subMenu: 'settings' }} />,
<Resource
name="apikey"
{...apikey}
options={{ subMenu: 'settings' }}
/>,
<Resource
name="player"
{...player}
options={{ subMenu: 'settings' }}
/>,
permissions === 'admin' ? (
<Resource
name="transcoding"
{...transcoding}
options={{ subMenu: 'settings' }}
/>
) : (
<Resource name="transcoding" />
),
permissions === 'admin' ? (
<Resource
name="library"
{...library}
options={{ subMenu: 'settings' }}
/>
) : null,
permissions === 'admin' ? (
<Resource
name="missing"
{...missing}
options={{ subMenu: 'settings' }}
/>
) : null,
<Resource name="translation" />,
<Resource name="genre" />,
<Resource name="tag" />,
<Resource name="playlistTrack" />,
<Resource name="keepalive" />,
<Resource name="insights" />,
<Resource name="config" />,
<Player />,
]}
</RAAdmin>
)
/* eslint-enable react/jsx-key */
}
const AppWithHotkeys = () => {
let language = localStorage.getItem('locale') || 'en'
document.documentElement.lang = language
if (config.enableSharing && shareInfo) {
return <SharePlayer />
}
return (
<HotKeys keyMap={keyMap}>
<DndProvider backend={HTML5Backend}>
<App />
</DndProvider>
</HotKeys>
)
}
export default AppWithHotkeys