161 lines
4.8 KiB
Vue
161 lines
4.8 KiB
Vue
<template>
|
|||
|
|
<div class="login-page">
|
||
|
|
<div class="login-card">
|
||
|
|
<div class="login-header">
|
||
|
|
<div class="brand-logo">
|
||
|
|
<i class="fas fa-shield-alt"></i>
|
||
|
|
</div>
|
||
|
|
<h1>OpenVPN Monitor</h1>
|
||
|
|
<p class="text-muted">Secure Access Control</p>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="login-body">
|
||
|
|
<!-- Standard Login -->
|
||
|
|
<form v-if="!requires2FA" @submit.prevent="handleLogin">
|
||
|
|
<div class="mb-4">
|
||
|
|
<label class="form-label">Username</label>
|
||
|
|
<div class="input-group">
|
||
|
|
<span class="input-group-text"><i class="fas fa-user"></i></span>
|
||
|
|
<input
|
||
|
|
type="text"
|
||
|
|
class="form-control"
|
||
|
|
v-model="form.username"
|
||
|
|
placeholder="Enter username"
|
||
|
|
required
|
||
|
|
>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="mb-4">
|
||
|
|
<label class="form-label">Password</label>
|
||
|
|
<div class="input-group">
|
||
|
|
<span class="input-group-text"><i class="fas fa-lock"></i></span>
|
||
|
|
<input
|
||
|
|
type="password"
|
||
|
|
class="form-control"
|
||
|
|
v-model="form.password"
|
||
|
|
placeholder="Enter password"
|
||
|
|
required
|
||
|
|
>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div v-if="error" class="alert alert-danger fade show mb-4">
|
||
|
|
<i class="fas fa-exclamation-circle me-2"></i>{{ error }}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<button type="submit" class="btn btn-primary w-100 py-2 fw-bold" :disabled="loading">
|
||
|
|
<span v-if="loading" class="spinner-border spinner-border-sm me-2"></span>
|
||
|
|
Sign In
|
||
|
|
</button>
|
||
|
|
</form>
|
||
|
|
|
||
|
|
<!-- 2FA Verification -->
|
||
|
|
<form v-else @submit.prevent="handleVerify2FA">
|
||
|
|
<div class="text-center mb-4">
|
||
|
|
<i class="fas fa-key fa-3x text-warning mb-3"></i>
|
||
|
|
<h3>Two-Factor Authentication</h3>
|
||
|
|
<p class="text-muted">Enter the 6-digit code from your authenticator app.</p>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="mb-4">
|
||
|
|
<input
|
||
|
|
type="text"
|
||
|
|
class="form-control form-control-lg text-center fw-bold"
|
||
|
|
v-model="otp"
|
||
|
|
placeholder="000 000"
|
||
|
|
maxlength="6"
|
||
|
|
required
|
||
|
|
autofocus
|
||
|
|
>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div v-if="error" class="alert alert-danger fade show mb-4">
|
||
|
|
<i class="fas fa-exclamation-circle me-2"></i>{{ error }}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<button type="submit" class="btn btn-warning w-100 py-2 fw-bold" :disabled="loading">
|
||
|
|
<span v-if="loading" class="spinner-border spinner-border-sm me-2"></span>
|
||
|
|
Verify & Continue
|
||
|
|
</button>
|
||
|
|
|
||
|
|
<button type="button" class="btn btn-link w-100 mt-2 text-muted" @click="resetLogin">
|
||
|
|
Back to login
|
||
|
|
</button>
|
||
|
|
</form>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="login-footer text-center mt-4">
|
||
|
|
<p class="small text-muted">© 2026 Admin Dashboard</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<script setup>
|
||
|
|
import { reactive, ref } from 'vue';
|
||
|
|
import { useRouter } from 'vue-router';
|
||
|
|
import { useApi } from '../composables/useApi';
|
||
|
|
|
||
|
|
const router = useRouter();
|
||
|
|
const { apiClient } = useApi();
|
||
|
|
const loading = ref(false);
|
||
|
|
const error = ref('');
|
||
|
|
const requires2FA = ref(false);
|
||
|
|
const tempToken = ref('');
|
||
|
|
const otp = ref('');
|
||
|
|
|
||
|
|
const form = reactive({
|
||
|
|
username: '',
|
||
|
|
password: ''
|
||
|
|
});
|
||
|
|
|
||
|
|
const handleLogin = async () => {
|
||
|
|
loading.value = true;
|
||
|
|
error.value = '';
|
||
|
|
try {
|
||
|
|
// Auth routes are under /api/auth, while apiClient defaults to /api/v1
|
||
|
|
// We use a relative path with '..' to go up to /api
|
||
|
|
const response = await apiClient.post('../auth/login', form);
|
||
|
|
if (response.data.requires_2fa) {
|
||
|
|
requires2FA.value = true;
|
||
|
|
tempToken.value = response.data.temp_token;
|
||
|
|
} else {
|
||
|
|
localStorage.setItem('ovpmon_token', response.data.token);
|
||
|
|
localStorage.setItem('ovpmon_user', response.data.username);
|
||
|
|
router.push('/');
|
||
|
|
}
|
||
|
|
} catch (err) {
|
||
|
|
console.error('Login Error:', err);
|
||
|
|
error.value = err.response?.data?.error || 'Login failed. Please check your connection.';
|
||
|
|
} finally {
|
||
|
|
loading.value = false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleVerify2FA = async () => {
|
||
|
|
loading.value = true;
|
||
|
|
error.value = '';
|
||
|
|
try {
|
||
|
|
const response = await apiClient.post('../auth/verify-2fa', {
|
||
|
|
temp_token: tempToken.value,
|
||
|
|
otp: otp.value
|
||
|
|
});
|
||
|
|
localStorage.setItem('ovpmon_token', response.data.token);
|
||
|
|
localStorage.setItem('ovpmon_user', response.data.username);
|
||
|
|
router.push('/');
|
||
|
|
} catch (err) {
|
||
|
|
error.value = err.response?.data?.error || 'Verification failed.';
|
||
|
|
} finally {
|
||
|
|
loading.value = false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const resetLogin = () => {
|
||
|
|
requires2FA.value = false;
|
||
|
|
tempToken.value = '';
|
||
|
|
otp.value = '';
|
||
|
|
error.value = '';
|
||
|
|
};
|
||
|
|
</script>
|
||
|
|
|