Demo mode: automatically connect as demo user
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import NavBarSearch from './NavBarSearch.vue'
|
||||
import BarcodeModal from './BarcodeModal.vue'
|
||||
import { useAuthStore } from './auth.store.js'
|
||||
import { getAppInfo } from './api.js'
|
||||
import { getAppInfo, postLogin } from './api.js'
|
||||
import { onMounted } from 'vue'
|
||||
|
||||
const authStore = useAuthStore();
|
||||
@@ -21,8 +21,23 @@
|
||||
const appInfo = ref(null);
|
||||
const appInfoErr = ref(null);
|
||||
|
||||
async function logInIfDemoMode(demoMode) {
|
||||
if (!demoMode) {
|
||||
return;
|
||||
}
|
||||
|
||||
const demouser = ref({
|
||||
username: "demo",
|
||||
password: ""
|
||||
});
|
||||
const res = await postLogin(demouser)
|
||||
const json = await res.json();
|
||||
await useAuthStore().login({username: demouser.value.username, token: json["token"]})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
getAppInfo(appInfo, appInfoErr);
|
||||
getAppInfo(appInfo, appInfoErr)
|
||||
.then(() => logInIfDemoMode(appInfo.value.demoMode));
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -62,7 +77,7 @@
|
||||
<RouterLink v-if="authStore.user" to="/add" class="navbar-item" activeClass="is-active">
|
||||
{{ $t('navbar.addbook')}}
|
||||
</RouterLink>
|
||||
<div v-if="authStore.user" class="navbar-item is-hidden-desktop">
|
||||
<div v-if="authStore.user && appInfo && !appInfo.demoMode" class="navbar-item is-hidden-desktop">
|
||||
<a @click="logout">
|
||||
{{ $t('navbar.logout')}}
|
||||
<span class="icon" :title="$t('navbar.logout')">
|
||||
@@ -76,9 +91,9 @@
|
||||
<div >
|
||||
{{ authStore.user.username }}
|
||||
</div>
|
||||
<a @click="logout" class="button is-light">
|
||||
{{ $t('navbar.logout')}}
|
||||
</a>
|
||||
<a v-if="appInfo && !appInfo.demoMode" @click="logout" class="button is-light">
|
||||
{{ $t('navbar.logout')}}
|
||||
</a>
|
||||
</div>
|
||||
<div v-else class="navbar-item">
|
||||
<div class="buttons">
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package dto
|
||||
|
||||
type AppInfo struct {
|
||||
RegistrationDisabled bool `json:"registrationDisabled"`
|
||||
DemoMode bool `json:"demoMode"`
|
||||
}
|
||||
|
||||
type BookGet struct {
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)})
|
||||
|
||||
Reference in New Issue
Block a user