Login: label for/id, autocomplete, inputmode for the 2FA code; clickable table rows, headers and client links work from the keyboard. One product name (OpenVPN Controller), own favicon (logo.svg) instead of vite.svg, footer text fixed. Minimal ESLint config (npm run lint, clean). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
388 lines
15 KiB
Vue
388 lines
15 KiB
Vue
<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">
|
|
<div class="card-header seccard d-flex justify-content-between align-items-center">
|
|
<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>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 2FA Card -->
|
|
<div class="col-lg-6">
|
|
<div class="card p-0 h-100">
|
|
<div class="card-header seccard d-flex justify-content-between align-items-center">
|
|
<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>
|
|
</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"
|
|
>
|
|
<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 -->
|
|
<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"
|
|
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"
|
|
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"
|
|
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>
|
|
</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';
|
|
|
|
const { apiClient, authApiClient } = useApi();
|
|
const toast = useToast();
|
|
|
|
// UI Refs
|
|
const pwModal = ref(null);
|
|
const userModal = ref(null);
|
|
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');
|
|
return;
|
|
}
|
|
|
|
pwLoading.value = true;
|
|
try {
|
|
await authApiClient.post('/change-password', {
|
|
current_password: passwordForm.current_password,
|
|
new_password: passwordForm.new_password
|
|
});
|
|
|
|
pwModal.value.hide();
|
|
toast.show('Password updated successfully.', 'success');
|
|
} catch (err) {
|
|
toast.show(err.response?.data?.error || 'Failed to update password', 'error');
|
|
} 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;
|
|
}
|
|
};
|
|
|
|
// 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);
|
|
|
|
const initSetup = async () => {
|
|
loading.value = true;
|
|
try {
|
|
const res = await authApiClient.post('/setup-2fa');
|
|
setupData.value = res.data;
|
|
step2.value = true;
|
|
} catch (err) {
|
|
toast.show(err.response?.data?.error || 'Failed to initialize 2FA setup.', 'error');
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
};
|
|
|
|
const verifyAndEnable = async () => {
|
|
if (!otp.value || otp.value.length !== 6) return;
|
|
loading.value = true;
|
|
try {
|
|
await authApiClient.post('/enable-2fa', {
|
|
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');
|
|
} catch (err) {
|
|
toast.show(err.response?.data?.error || 'The code you entered is invalid. Please check your authenticator app and try again.', 'error');
|
|
} 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');
|
|
|
|
isEnabled.value = false;
|
|
step2.value = false;
|
|
toast.show('Two-factor authentication has been disabled. Your account is now using standard password protection.', 'warning');
|
|
} catch (err) {
|
|
toast.show(err.response?.data?.error || 'Failed to disable two-factor authentication. Please try again.', 'error');
|
|
}
|
|
};
|
|
</script>
|
|
|