Add login/logout to Momentry Studio sidebar with default demo/demo123
- Added login/logout buttons to left sidebar bottom - Shows current user name when logged in - Login modal with username/password fields - Default credentials: demo/demo123 - auth_login Tauri command using SqliteProvider
This commit is contained in:
Generated
+17
@@ -3570,6 +3570,7 @@ dependencies = [
|
|||||||
"tokio-postgres",
|
"tokio-postgres",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"toml 0.8.23",
|
"toml 0.8.23",
|
||||||
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"ureq",
|
"ureq",
|
||||||
@@ -7120,6 +7121,22 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-http"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.13.0",
|
||||||
|
"bytes",
|
||||||
|
"http 1.4.2",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"http-body-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-layer"
|
name = "tower-layer"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ pub struct UserInfo {
|
|||||||
pub status: String,
|
pub status: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct LoginResult {
|
||||||
|
pub success: bool,
|
||||||
|
pub username: String,
|
||||||
|
pub message: String,
|
||||||
|
}
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
static ref DATA_PROVIDER: LazyLock<Arc<Mutex<Box<dyn DataProvider>>>> =
|
static ref DATA_PROVIDER: LazyLock<Arc<Mutex<Box<dyn DataProvider>>>> =
|
||||||
LazyLock::new(|| {
|
LazyLock::new(|| {
|
||||||
@@ -20,6 +27,27 @@ lazy_static::lazy_static! {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn auth_login(username: String, password: String) -> Result<LoginResult, String> {
|
||||||
|
let provider = DATA_PROVIDER.lock().unwrap();
|
||||||
|
|
||||||
|
let valid = provider.check_password(&username, &password).map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
if valid {
|
||||||
|
Ok(LoginResult {
|
||||||
|
success: true,
|
||||||
|
username: username.clone(),
|
||||||
|
message: "Login successful".to_string(),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Ok(LoginResult {
|
||||||
|
success: false,
|
||||||
|
username: String::new(),
|
||||||
|
message: "Invalid username or password".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn list_auth_users() -> Result<Vec<UserInfo>, String> {
|
pub async fn list_auth_users() -> Result<Vec<UserInfo>, String> {
|
||||||
let provider = DATA_PROVIDER.lock().unwrap();
|
let provider = DATA_PROVIDER.lock().unwrap();
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ fn main() {
|
|||||||
get_backup_config,
|
get_backup_config,
|
||||||
set_backup_config,
|
set_backup_config,
|
||||||
run_backup,
|
run_backup,
|
||||||
|
auth_login,
|
||||||
list_auth_users,
|
list_auth_users,
|
||||||
create_auth_user,
|
create_auth_user,
|
||||||
update_auth_user,
|
update_auth_user,
|
||||||
|
|||||||
@@ -1,21 +1,57 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { onMounted } from 'vue'
|
import { onMounted, ref } from 'vue'
|
||||||
import { useAppStore } from './stores/app'
|
import { useAppStore } from './stores/app'
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
const appStore = useAppStore()
|
const appStore = useAppStore()
|
||||||
|
const isLoggedIn = ref(false)
|
||||||
|
const showLoginModal = ref(false)
|
||||||
|
const loginForm = ref({
|
||||||
|
username: 'demo',
|
||||||
|
password: 'demo123'
|
||||||
|
})
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await appStore.initializeApp()
|
await appStore.initializeApp()
|
||||||
|
isLoggedIn.value = true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleLogin = async () => {
|
||||||
|
if (!loginForm.value.username || !loginForm.value.password) {
|
||||||
|
ElMessage.warning('Please enter username and password')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await appStore.login(loginForm.value.username, loginForm.value.password)
|
||||||
|
if (result) {
|
||||||
|
isLoggedIn.value = true
|
||||||
|
showLoginModal.value = false
|
||||||
|
loginForm.value.password = ''
|
||||||
|
ElMessage.success('Login successful')
|
||||||
|
} else {
|
||||||
|
ElMessage.error('Login failed')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
ElMessage.error(`Login error: ${error}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleLogout = () => {
|
||||||
|
appStore.logout()
|
||||||
|
isLoggedIn.value = false
|
||||||
|
appStore.user = ''
|
||||||
|
ElMessage.success('Logout successful')
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="app-container">
|
<div class="app-container">
|
||||||
<el-container>
|
<el-container>
|
||||||
<el-header class="app-header">
|
<el-header class="app-header">
|
||||||
<h2>Momentry Display Engine</h2>
|
<h2>Momentry Studio</h2>
|
||||||
<div class="user-info">
|
<div class="user-info">
|
||||||
<span>User: {{ appStore.user }}</span>
|
<span>User: {{ appStore.user || 'Guest' }}</span>
|
||||||
</div>
|
</div>
|
||||||
</el-header>
|
</el-header>
|
||||||
<el-container>
|
<el-container>
|
||||||
@@ -54,6 +90,32 @@ onMounted(async () => {
|
|||||||
<span>Monitor</span>
|
<span>Monitor</span>
|
||||||
</el-menu-item>
|
</el-menu-item>
|
||||||
</el-menu>
|
</el-menu>
|
||||||
|
|
||||||
|
<!-- Login/Logout Section at bottom -->
|
||||||
|
<div class="sidebar-bottom">
|
||||||
|
<div class="user-display" v-if="isLoggedIn">
|
||||||
|
<el-icon><UserFilled /></el-icon>
|
||||||
|
<span class="username">{{ appStore.user }}</span>
|
||||||
|
</div>
|
||||||
|
<el-button
|
||||||
|
v-if="isLoggedIn"
|
||||||
|
type="danger"
|
||||||
|
size="small"
|
||||||
|
class="logout-btn"
|
||||||
|
@click="handleLogout"
|
||||||
|
>
|
||||||
|
Logout
|
||||||
|
</el-button>
|
||||||
|
<el-button
|
||||||
|
v-else
|
||||||
|
type="primary"
|
||||||
|
size="small"
|
||||||
|
class="login-btn"
|
||||||
|
@click="showLoginModal = true"
|
||||||
|
>
|
||||||
|
Login
|
||||||
|
</el-button>
|
||||||
|
</div>
|
||||||
</el-aside>
|
</el-aside>
|
||||||
<el-main class="app-main">
|
<el-main class="app-main">
|
||||||
<router-view v-if="!appStore.loading" />
|
<router-view v-if="!appStore.loading" />
|
||||||
@@ -64,6 +126,35 @@ onMounted(async () => {
|
|||||||
</el-main>
|
</el-main>
|
||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
|
|
||||||
|
<!-- Login Modal -->
|
||||||
|
<el-dialog
|
||||||
|
v-model="showLoginModal"
|
||||||
|
title="Login"
|
||||||
|
width="300px"
|
||||||
|
:close-on-click-modal="false"
|
||||||
|
>
|
||||||
|
<el-form>
|
||||||
|
<el-form-item label="Username">
|
||||||
|
<el-input
|
||||||
|
v-model="loginForm.username"
|
||||||
|
placeholder="Enter username"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="Password">
|
||||||
|
<el-input
|
||||||
|
v-model="loginForm.password"
|
||||||
|
type="password"
|
||||||
|
placeholder="Enter password"
|
||||||
|
show-password
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
<template #footer>
|
||||||
|
<el-button @click="showLoginModal = false">Cancel</el-button>
|
||||||
|
<el-button type="primary" @click="handleLogin">Login</el-button>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -93,11 +184,36 @@ onMounted(async () => {
|
|||||||
.app-aside {
|
.app-aside {
|
||||||
background: #545c64;
|
background: #545c64;
|
||||||
overflow-y: auto;
|
overflow-y: auto;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.app-menu {
|
.app-menu {
|
||||||
border: none;
|
border: none;
|
||||||
background: transparent;
|
background: transparent;
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar-bottom {
|
||||||
|
padding: 15px;
|
||||||
|
border-top: 1px solid #666;
|
||||||
|
background: #3a4149;
|
||||||
|
}
|
||||||
|
|
||||||
|
.user-display {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
color: #fff;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.username {
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-btn, .logout-btn {
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.app-main {
|
.app-main {
|
||||||
|
|||||||
@@ -57,6 +57,24 @@ export const useAppStore = defineStore('app', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function login(username, password) {
|
||||||
|
try {
|
||||||
|
const result = await invoke('auth_login', { username, password })
|
||||||
|
if (result && result.success) {
|
||||||
|
user.value = username
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Login error:', error)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function logout() {
|
||||||
|
user.value = ''
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentTreeType,
|
currentTreeType,
|
||||||
user,
|
user,
|
||||||
@@ -69,6 +87,8 @@ export const useAppStore = defineStore('app', () => {
|
|||||||
loadServiceStatus,
|
loadServiceStatus,
|
||||||
loadHealthData,
|
loadHealthData,
|
||||||
loadMonitorData,
|
loadMonitorData,
|
||||||
initializeApp
|
initializeApp,
|
||||||
|
login,
|
||||||
|
logout
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user