Files
OpenVPN-Monitoring-Simple/APP_UI/src/views/Account.vue
T

387 lines
15 KiB
Vue
Raw Normal View History

2026-01-28 22:37:47 +03:00
<template>
<div class="user-account-container mx-auto" style="max-width: 1000px;">
<div class="row g-4">
<!-- Security Card -->
<div class="col-lg-6">
<div class="card h-100">
2026-01-29 22:48:57 +03:00
<div class="card-header seccard d-flex justify-content-between align-items-center">
2026-01-28 22:37:47 +03:00
<span><i class="fas fa-user-shield me-2"></i>Security Settings</span>
</div>
<div class="card-body p-4">
<div class="text-center py-4 d-flex flex-column align-items-center">
<div class="card-interior-icon">
<i class="fas fa-user-check fa-3x text-primary"></i>
</div>
<div class="card-interior-title">
<h4 class="m-0 text-heading">Update Credentials</h4>
</div>
<div class="card-interior-description">
<p class="text-muted m-0">Maintain your account security by updating your password regularly to prevent unauthorized access.</p>
</div>
<button class="btn btn-action btn-action-primary py-2 fw-bold btn-account-action" @click="showPwModal">
Change Password
</button>
<button class="btn btn-action btn-action-secondary py-2 fw-bold btn-account-action mt-2" @click="showUserModal">
Change Username
</button>
2026-01-28 22:37:47 +03:00
</div>
</div>
</div>
</div>
<!-- 2FA Card -->
<div class="col-lg-6">
<div class="card p-0 h-100">
2026-01-29 22:48:57 +03:00
<div class="card-header seccard d-flex justify-content-between align-items-center">
2026-01-28 22:37:47 +03:00
<span><i class="fas fa-key me-2"></i>Two-Factor Authentication</span>
<span v-if="isEnabled" class="status-badge status-valid">
<i class="fas fa-check-circle me-1"></i>Active
</span>
</div>
<div class="card-body p-4">
<div v-if="!isEnabled">
<div v-if="!step2" class="text-center py-4 d-flex flex-column align-items-center">
<div class="card-interior-icon">
<i class="fas fa-shield-alt fa-3x text-primary"></i>
</div>
<div class="card-interior-title">
<h4 class="m-0 text-heading">Secure Your Account</h4>
</div>
<div class="card-interior-description">
<p class="text-muted m-0">Protect your OpenVPN Controller dashboard with second-layer security using Google Authenticator or any TOTP app.</p>
2026-01-28 22:37:47 +03:00
</div>
<button class="btn btn-action btn-action-primary py-2 fw-bold btn-account-action" @click="initSetup" :disabled="loading">
<span v-if="loading" class="spinner-border spinner-border-sm me-2"></span>
Start Setup
</button>
</div>
<div v-else class="setup-steps fade-in">
<div class="step-item mb-5 text-center">
<h5 class="mb-3 text-heading">1. Scan the QR Code</h5>
<p class="text-muted small mb-4">Open your authenticator app and scan this code or manually enter the secret.</p>
<div class="qr-setup-container mx-auto">
<div class="qr-code-wrapper p-3 d-inline-block rounded shadow-sm bg-white">
<qrcode-vue :value="setupData.uri" :size="150" level="H" :background="'#ffffff'" foreground="#24292f" />
</div>
<div class="secret-box mt-3">
<div class="small fw-bold text-muted mb-2">Manual Secret:</div>
<div class="user-select-all font-monospace p-2 secret-value-box rounded small d-inline-block w-100">
{{ setupData.secret }}
</div>
</div>
</div>
</div>
<div class="step-item text-center">
<h5 class="mb-3 text-heading">2. Verify Connection</h5>
<div class="verification-group mx-auto" style="max-width: 300px;">
<div class="input-group mb-3 shadow-sm rounded">
<input
type="text"
class="form-control text-center fw-bold"
v-model="otp"
placeholder="000000"
maxlength="6"
inputmode="numeric"
autocomplete="one-time-code"
aria-label="6-digit authentication code"
2026-01-28 22:37:47 +03:00
>
<button class="btn btn-action btn-action-primary px-4 fw-bold" @click="verifyAndEnable" :disabled="loading">
<span v-if="loading" class="spinner-border spinner-border-sm me-2"></span>
Enable
</button>
</div>
</div>
</div>
</div>
</div>
<div v-else class="text-center py-4 d-flex flex-column align-items-center">
<div class="card-interior-icon">
<i class="fas fa-key fa-3x text-primary"></i>
</div>
<div class="card-interior-title">
<h4 class="m-0 text-heading">2FA is Enabled</h4>
</div>
<div class="card-interior-description">
<p class="text-muted m-0">Your dashboard is now protected with two-factor authentication.</p>
</div>
<button class="btn btn-action btn-action-danger py-2 fw-bold btn-account-action" @click="disableConfirm">
<i class="fas fa-ban me-2"></i>Disable 2FA
</button>
</div>
</div>
</div>
</div>
</div>
<!-- Confirmation before 2FA is disabled; results are shown as toasts -->
2026-01-28 22:37:47 +03:00
<ConfirmModal ref="disable2FAModal" @confirm="handleDisable2FA" />
<!-- Password Change Modal -->
<BaseModal id="passwordChangeModal" title="Change Account Password" ref="pwModal">
<template #body>
<form @submit.prevent="handleChangePassword" id="pwForm">
<div class="mb-3">
<label class="form-label small fw-bold text-muted text-uppercase">Current Password</label>
<input
type="password"
class="form-control"
v-model="passwordForm.current_password"
placeholder="Enter current password"
autocomplete="current-password"
2026-01-28 22:37:47 +03:00
required
>
</div>
<div class="mb-3">
<label class="form-label small fw-bold text-muted text-uppercase">New Password</label>
<input
type="password"
class="form-control"
v-model="passwordForm.new_password"
placeholder="Min. 8 characters"
autocomplete="new-password"
2026-01-28 22:37:47 +03:00
required
>
</div>
<div class="mb-2">
<label class="form-label small fw-bold text-muted text-uppercase">Confirm New Password</label>
<input
type="password"
class="form-control"
v-model="passwordForm.confirm_password"
placeholder="Confirm new password"
autocomplete="new-password"
2026-01-28 22:37:47 +03:00
required
>
</div>
</form>
</template>
<template #footer>
<button type="button" class="btn-action btn-action-secondary" @click="pwModal.hide()">Cancel</button>
<button type="submit" form="pwForm" class="btn-action btn-action-save" :disabled="pwLoading">
<span v-if="pwLoading" class="spinner-border spinner-border-sm me-2"></span>
Update Password
</button>
</template>
</BaseModal>
<!-- Username Change Modal -->
<BaseModal id="usernameChangeModal" title="Change Account Username" ref="userModal">
<template #body>
<form @submit.prevent="handleChangeUsername" id="userForm">
<div class="mb-3">
<label class="form-label small fw-bold text-muted text-uppercase">New Username</label>
<input
type="text"
class="form-control"
v-model="usernameForm.new_username"
placeholder="3-32 chars: letters, digits, . _ -"
pattern="[A-Za-z][A-Za-z0-9_.\-]{2,31}"
autocomplete="off"
required
>
</div>
<div class="mb-3">
<label class="form-label small fw-bold text-muted text-uppercase">Current Password</label>
<input
type="password"
class="form-control"
v-model="usernameForm.current_password"
placeholder="Enter current password"
autocomplete="current-password"
required
>
</div>
<div class="mb-2" v-if="isEnabled">
<label class="form-label small fw-bold text-muted text-uppercase">2FA Code</label>
<input
type="text"
class="form-control"
v-model="usernameForm.otp"
placeholder="6-digit code"
inputmode="numeric"
autocomplete="one-time-code"
required
>
</div>
</form>
</template>
<template #footer>
<button type="button" class="btn-action btn-action-secondary" @click="userModal.hide()">Cancel</button>
<button type="submit" form="userForm" class="btn-action btn-action-save" :disabled="userLoading">
<span v-if="userLoading" class="spinner-border spinner-border-sm me-2"></span>
Update Username
</button>
</template>
</BaseModal>
2026-01-28 22:37:47 +03:00
</div>
</template>
<script setup>
import { ref, onMounted, reactive } from 'vue';
import { useApi } from '../composables/useApi';
import QrcodeVue from 'qrcode.vue';
import BaseModal from '../components/BaseModal.vue';
import ConfirmModal from '../components/ConfirmModal.vue';
import { useToast } from '../composables/useToast';
2026-01-28 22:37:47 +03:00
const { apiClient, authApiClient } = useApi();
const toast = useToast();
2026-01-28 22:37:47 +03:00
// UI Refs
const pwModal = ref(null);
const userModal = ref(null);
2026-01-28 22:37:47 +03:00
const disable2FAModal = ref(null);
// Password Logic
const pwLoading = ref(false);
const passwordForm = reactive({
current_password: '',
new_password: '',
confirm_password: ''
});
const showPwModal = () => {
passwordForm.current_password = '';
passwordForm.new_password = '';
passwordForm.confirm_password = '';
pwModal.value.show();
};
const handleChangePassword = async () => {
if (passwordForm.new_password !== passwordForm.confirm_password) {
toast.show('New passwords do not match', 'error');
2026-01-28 22:37:47 +03:00
return;
}
pwLoading.value = true;
try {
await authApiClient.post('/change-password', {
2026-01-28 22:37:47 +03:00
current_password: passwordForm.current_password,
new_password: passwordForm.new_password
});
pwModal.value.hide();
toast.show('Password updated successfully.', 'success');
2026-01-28 22:37:47 +03:00
} catch (err) {
toast.show(err.response?.data?.error || 'Failed to update password', 'error');
2026-01-28 22:37:47 +03:00
} finally {
pwLoading.value = false;
}
};
// Username Logic
const userLoading = ref(false);
const usernameForm = reactive({ new_username: '', current_password: '', otp: '' });
const showUserModal = () => {
usernameForm.new_username = '';
usernameForm.current_password = '';
usernameForm.otp = '';
userModal.value.show();
};
const handleChangeUsername = async () => {
if (!/^[A-Za-z][A-Za-z0-9_.-]{2,31}$/.test(usernameForm.new_username)) {
toast.show('Username must be 3-32 chars, start with a letter, and contain only letters, digits, . _ -', 'error');
return;
}
userLoading.value = true;
try {
const res = await authApiClient.post('/change-username', {
new_username: usernameForm.new_username,
current_password: usernameForm.current_password,
otp: usernameForm.otp || undefined
});
try { localStorage.setItem('ovpmon_user', res.data.username); } catch (e) { /* ignore */ }
window.dispatchEvent(new Event('ovpmon-user-changed'));
userModal.value.hide();
toast.show('Username updated. Use the new username the next time you sign in.', 'success');
} catch (err) {
toast.show(err.response?.data?.error || 'Failed to update username', 'error');
} finally {
userLoading.value = false;
}
};
2026-01-28 22:37:47 +03:00
// 2FA Logic
const isEnabled = ref(false);
const step2 = ref(false);
const loading = ref(false);
const otp = ref('');
const setupData = ref({ secret: '', uri: '' });
const fetchUserStatus = async () => {
loading.value = true;
try {
const res = await apiClient.get('/user/me');
isEnabled.value = res.data.is_2fa_enabled;
} catch (err) {
console.error('Failed to fetch user status:', err);
} finally {
loading.value = false;
}
};
onMounted(fetchUserStatus);
2026-01-28 22:37:47 +03:00
const initSetup = async () => {
loading.value = true;
try {
const res = await authApiClient.post('/setup-2fa');
2026-01-28 22:37:47 +03:00
setupData.value = res.data;
step2.value = true;
} catch (err) {
toast.show(err.response?.data?.error || 'Failed to initialize 2FA setup.', 'error');
2026-01-28 22:37:47 +03:00
} finally {
loading.value = false;
}
};
const verifyAndEnable = async () => {
if (!otp.value || otp.value.length !== 6) return;
loading.value = true;
try {
await authApiClient.post('/enable-2fa', {
2026-01-28 22:37:47 +03:00
secret: setupData.value.secret,
otp: otp.value
});
isEnabled.value = true;
toast.show('Two-factor authentication has been enabled. Your dashboard is now more secure.', 'success');
2026-01-28 22:37:47 +03:00
} catch (err) {
toast.show(err.response?.data?.error || 'The code you entered is invalid. Please check your authenticator app and try again.', 'error');
2026-01-28 22:37:47 +03:00
} finally {
loading.value = false;
}
};
const disableConfirm = () => {
disable2FAModal.value.open({
title: 'Disable 2FA?',
message: 'This will reduce your account security by removing the second-layer protection from your dashboard.',
confirmText: 'Yes, disable',
confirmBtnClass: 'btn-action-danger'
});
};
const handleDisable2FA = async () => {
try {
await authApiClient.post('/disable-2fa');
2026-01-28 22:37:47 +03:00
isEnabled.value = false;
step2.value = false;
toast.show('Two-factor authentication has been disabled. Your account is now using standard password protection.', 'warning');
2026-01-28 22:37:47 +03:00
} catch (err) {
toast.show(err.response?.data?.error || 'Failed to disable two-factor authentication. Please try again.', 'error');
2026-01-28 22:37:47 +03:00
}
};
</script>