Added an option to specify demo user, will be created on startup if it does not exist
This commit is contained in:
@@ -21,13 +21,13 @@
|
||||
const appInfo = ref(null);
|
||||
const appInfoErr = ref(null);
|
||||
|
||||
async function logInIfDemoMode(demoMode) {
|
||||
async function logInIfDemoMode(demoMode, demoUsername) {
|
||||
if (!demoMode) {
|
||||
return;
|
||||
}
|
||||
|
||||
const demouser = ref({
|
||||
username: "demo",
|
||||
username: demoUsername,
|
||||
password: ""
|
||||
});
|
||||
const res = await postLogin(demouser)
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
onMounted(() => {
|
||||
getAppInfo(appInfo, appInfoErr)
|
||||
.then(() => logInIfDemoMode(appInfo.value.demoMode));
|
||||
.then(() => logInIfDemoMode(appInfo.value.demoMode, appInfo.value.demoUsername));
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ 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'"`
|
||||
DemoMode bool `toml:"demo-mode" default:"false" comment:"Activate demo mode: anyone connecting to the instance will be logged in as a single user."`
|
||||
DemoUsername string `toml:"demo-username" default:"demo" comment:"Name of the single user used for the demo."`
|
||||
AddUser UserListAsStrings `toml:"add-user" short:"a" comment:"Add an user on startup following htpasswd bcrypt format, example: [\"demo:$2y$10$UHR2646SZo2W.Rhna7bn5eWNLXWJZ/Sa3oLd9RlxlXs57Bwp6isOS\",\"user:$2y$10$3WYUp.VDpzJRywtrxO1s/uWfUIKpTE4yh5B1d2RCef3hvczYbEWTC\"]"`
|
||||
}
|
||||
|
||||
@@ -46,6 +47,7 @@ func defaultConfig() Config {
|
||||
InventaireUrl: "https://inventaire.io",
|
||||
DisableRegistration: false,
|
||||
DemoMode: false,
|
||||
DemoUsername: "demo",
|
||||
AddUser: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
||||
@@ -53,6 +54,11 @@ func CreateDefaultUsers(db *gorm.DB, config *config.Config) error {
|
||||
usernames = append(usernames, splittedString[0])
|
||||
usersPasswordMap[splittedString[0]] = splittedString[1]
|
||||
}
|
||||
if !slices.Contains(usernames, config.DemoUsername) {
|
||||
usernames = append(usernames, config.DemoUsername)
|
||||
usersPasswordMap[config.DemoUsername] = ""
|
||||
|
||||
}
|
||||
|
||||
var existingUsers []model.User
|
||||
err := db.Where("name IN ?", usernames).Find(&existingUsers).Error
|
||||
|
||||
@@ -3,6 +3,7 @@ package dto
|
||||
type AppInfo struct {
|
||||
RegistrationDisabled bool `json:"registrationDisabled"`
|
||||
DemoMode bool `json:"demoMode"`
|
||||
DemoUsername string `json:"demoUsername"`
|
||||
}
|
||||
|
||||
type BookGet struct {
|
||||
|
||||
@@ -11,5 +11,6 @@ func GetAppInfo(ac appcontext.AppContext) {
|
||||
ac.C.JSON(http.StatusOK, dto.AppInfo{
|
||||
RegistrationDisabled: ac.Config.DisableRegistration,
|
||||
DemoMode: ac.Config.DemoMode,
|
||||
DemoUsername: ac.Config.DemoUsername,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func PostLoginHandler(ac appcontext.AppContext) {
|
||||
}
|
||||
username = user.Username
|
||||
} else {
|
||||
username = "demo"
|
||||
username = ac.Config.DemoUsername
|
||||
}
|
||||
|
||||
var jwtToken string
|
||||
|
||||
Reference in New Issue
Block a user