diff --git a/front/src/api.js b/front/src/api.js
index 128b717..c1babd1 100644
--- a/front/src/api.js
+++ b/front/src/api.js
@@ -34,8 +34,8 @@ function useFetch(data, error, url) {
}
}
-export function getAppInfo(appInfo, appInfoErr) {
- fetch('/ws/appinfo', {
+export async function getAppInfo(appInfo, appInfoErr) {
+ return fetch('/ws/appinfo', {
method: 'GET'
}).then((res) => res.json())
.then((json) => appInfo.value = json)
diff --git a/internal/apitest/get_appinfo_test.go b/internal/apitest/get_appinfo_test.go
index 8513126..c00a4b4 100644
--- a/internal/apitest/get_appinfo_test.go
+++ b/internal/apitest/get_appinfo_test.go
@@ -27,4 +27,5 @@ func TestGetAppInfo_Ok(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, false, appInfo.RegistrationDisabled)
+ assert.Equal(t, false, appInfo.DemoMode)
}
diff --git a/internal/config/config.go b/internal/config/config.go
index d73dc10..29644a5 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -19,6 +19,7 @@ type Config struct {
Limit int `toml:"limit" default:"100" comment:"A single API call will return at most this number of records."`
InventaireUrl string `toml:"inventaire-url" default:"https://inventaire.io" comment:"An inventaire.io instance URL."`
DisableRegistration bool `toml:"disable-registration" default:"false" comment:"Disable new account creation."`
+ DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as user 'demo'"`
}
func defaultConfig() Config {
@@ -31,6 +32,7 @@ func defaultConfig() Config {
Limit: 100,
InventaireUrl: "https://inventaire.io",
DisableRegistration: false,
+ DemoMode: false,
}
}
diff --git a/internal/dto/out.go b/internal/dto/out.go
index 506ca7a..28381a1 100644
--- a/internal/dto/out.go
+++ b/internal/dto/out.go
@@ -2,6 +2,7 @@ package dto
type AppInfo struct {
RegistrationDisabled bool `json:"registrationDisabled"`
+ DemoMode bool `json:"demoMode"`
}
type BookGet struct {
diff --git a/internal/routes/appinfo.go b/internal/routes/appinfo.go
index b5215c8..e91c8ab 100644
--- a/internal/routes/appinfo.go
+++ b/internal/routes/appinfo.go
@@ -8,5 +8,8 @@ import (
)
func GetAppInfo(ac appcontext.AppContext) {
- ac.C.JSON(http.StatusOK, dto.AppInfo{RegistrationDisabled: ac.Config.DisableRegistration})
+ ac.C.JSON(http.StatusOK, dto.AppInfo{
+ RegistrationDisabled: ac.Config.DisableRegistration,
+ DemoMode: ac.Config.DemoMode,
+ })
}
diff --git a/internal/routes/userlogin.go b/internal/routes/userlogin.go
index 4c724a8..14b297b 100644
--- a/internal/routes/userlogin.go
+++ b/internal/routes/userlogin.go
@@ -16,21 +16,29 @@ import (
)
func PostLoginHandler(ac appcontext.AppContext) {
- var user dto.UserLogin
- err := ac.C.ShouldBindJSON(&user)
- if err != nil {
- myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
- return
- }
- if !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
- ac.C.JSON(http.StatusUnauthorized,
- gin.H{"error": i18nresource.GetTranslatedMessage(&ac, "InvalidCredentials")})
- return
+ var username string
+
+ if !ac.Config.DemoMode {
+ var user dto.UserLogin
+ err := ac.C.ShouldBindJSON(&user)
+ if err != nil {
+ myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
+ return
+ }
+
+ if !ac.Config.DemoMode && !isUserAndPasswordOk(ac.Db, user.Username, user.Password) {
+ ac.C.JSON(http.StatusUnauthorized,
+ gin.H{"error": i18nresource.GetTranslatedMessage(&ac, "InvalidCredentials")})
+ return
+ }
+ username = user.Username
+ } else {
+ username = "demo"
}
var jwtToken string
- jwtToken, err = jwtauth.GenerateJwtToken(user.Username)
+ jwtToken, err := jwtauth.GenerateJwtToken(username)
if err != nil {
ac.C.JSON(http.StatusUnauthorized,
gin.H{"error": fmt.Errorf("Error when generating JWT token: %w", err)})