🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
feat: better way to detect initial account creation
Deluan10ead1f5f2e8
diff --git a/server/app/app.go b/server/app/app.goindex 25d9b66a5..749f7dd97 100644--- a/server/app/app.go+++ b/server/app/app.go@@ -49,6 +49,7 @@ func (app *Router) routes() http.Handler {})// Serve UI app assets+ r.Handle("/", ServeIndex(app.ds))r.Handle("/*", http.StripPrefix(app.path, http.FileServer(assets.AssetFile())))return rdiff --git a/server/app/serve_index.go b/server/app/serve_index.gonew file mode 100644index 000000000..cd3a5a992--- /dev/null+++ b/server/app/serve_index.go@@ -0,0 +1,43 @@+package app++import (+ "encoding/json"+ "html/template"+ "io/ioutil"+ "net/http"++ "github.com/deluan/navidrome/assets"+ "github.com/deluan/navidrome/log"+ "github.com/deluan/navidrome/model"+)++// Injects the `firstTime` config in the `index.html` template+func ServeIndex(ds model.DataStore) http.HandlerFunc {+ return func(w http.ResponseWriter, r *http.Request) {+ c, err := ds.User(r.Context()).CountAll()+ firstTime := c == 0 && err == nil++ t := template.New("initial state")+ fs := assets.AssetFile()+ indexHtml, err := fs.Open("index.html")+ if err != nil {+ log.Error(r, "Could not find `index.html` template", err)+ }+ indexStr, err := ioutil.ReadAll(indexHtml)+ if err != nil {+ log.Error(r, "Could not read from `index.html`", err)+ }+ t, _ = t.Parse(string(indexStr))+ appConfig := map[string]interface{}{+ "firstTime": firstTime,+ }+ j, _ := json.Marshal(appConfig)+ data := map[string]interface{}{+ "AppConfig": string(j),+ }+ err = t.Execute(w, data)+ if err != nil {+ log.Error(r, "Could not execute `index.html` template", err)+ }+ }+}diff --git a/ui/public/index.html b/ui/public/index.htmlindex 12d88a620..43d1a58fb 100644--- a/ui/public/index.html+++ b/ui/public/index.html@@ -25,6 +25,9 @@Learn how to configure a non-root public URL by running `npm run build`.--><title>Navidrome</title>+ <script>+ window.__APP_CONFIG__ = "{{.AppConfig}}"+ </script></head><body><noscript>You need to enable JavaScript to run this app.</noscript>diff --git a/ui/src/App.js b/ui/src/App.jsindex 28de38f3d..e3f12e730 100644--- a/ui/src/App.js+++ b/ui/src/App.js@@ -1,4 +1,4 @@-import React, { useState } from 'react'+import React from 'react'import { Admin, resolveBrowserLocale, Resource } from 'react-admin'import dataProvider from './dataProvider'import authProvider from './authProvider'@@ -19,48 +19,35 @@ const i18nProvider = polyglotI18nProvider(resolveBrowserLocale())-const App = () => (- <Admin- theme={theme}- customReducers={{ queue: playQueueReducer }}- dataProvider={dataProvider}- authProvider={authProvider}- i18nProvider={i18nProvider}- layout={Layout}- loginPage={Login}- >- {(permissions) => [- <Resource name="artist" {...artist} options={{ subMenu: 'library' }} />,- <Resource name="album" {...album} options={{ subMenu: 'library' }} />,- <Resource name="song" {...song} options={{ subMenu: 'library' }} />,- permissions === 'admin' ? <Resource name="user" {...user} /> : null,- <Player />- ]}- </Admin>-)+const App = () => {+ try {+ const appConfig = JSON.parse(window.__APP_CONFIG__)-// TODO: This is a complicated way to force a first check for initial setup. A better way would be to send this info-// set in the `window` object in the index.html-const AppWrapper = () => {- const [checked, setChecked] = useState(false)+ // This flags to the login process that it should create the first account instead+ if (appConfig.firstTime) {+ localStorage.setItem('initialAccountCreation', 'true')+ }+ } catch (e) {}- if (!checked) {- dataProvider- .getOne('keepalive', { id: new Date().getTime() })- .then(() => setChecked(true))- .catch((err) => {- authProvider- .checkError(err)- .then(() => {- setChecked(true)- })- .catch(() => {- setChecked(true)- })- })- return null- }- return <App />+ return (+ <Admin+ theme={theme}+ customReducers={{ queue: playQueueReducer }}+ dataProvider={dataProvider}+ authProvider={authProvider}+ i18nProvider={i18nProvider}+ layout={Layout}+ loginPage={Login}+ >+ {(permissions) => [+ <Resource name="artist" {...artist} options={{ subMenu: 'library' }} />,+ <Resource name="album" {...album} options={{ subMenu: 'library' }} />,+ <Resource name="song" {...song} options={{ subMenu: 'library' }} />,+ permissions === 'admin' ? <Resource name="user" {...user} /> : null,+ <Player />+ ]}+ </Admin>+ )}-export default AppWrapper+export default Appdiff --git a/ui/src/authProvider.js b/ui/src/authProvider.jsindex 3a4e561b4..675faaa33 100644--- a/ui/src/authProvider.js+++ b/ui/src/authProvider.js@@ -56,11 +56,7 @@ const authProvider = {checkAuth: () =>localStorage.getItem('token') ? Promise.resolve() : Promise.reject(),- checkError: (error) => {- const { status, message } = error- if (message === 'no users created') {- localStorage.setItem('initialAccountCreation', 'true')- }+ checkError: ({ status }) => {if (status === 401 || status === 403) {removeItems()return Promise.reject()