131 lines
4.0 KiB
HTML
131 lines
4.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en" data-bs-theme="auto">
|
|
|
|
<head>
|
|
<%- header %>
|
|
</head>
|
|
|
|
<body id="app" v-cloak>
|
|
<main role="main" style="max-width: 1200px; margin: 1em auto">
|
|
<div class="d-flex justify-content-center">
|
|
<div class="card p-2">
|
|
<header>
|
|
<h1 class="mb-0">
|
|
<img src="/images/logo-apollo-45.png" height="45" alt="">
|
|
{{ $t('welcome.greeting') }}
|
|
</h1>
|
|
</header>
|
|
<form @submit.prevent="login" class="mt-4">
|
|
<div class="mb-2">
|
|
<label for="usernameInput" class="form-label">{{ $t('_common.username') }}</label>
|
|
<input type="text" class="form-control" id="usernameInput" autocomplete="username"
|
|
v-model="passwordData.username" required autofocus/>
|
|
</div>
|
|
<div class="mb-2">
|
|
<label for="passwordInput" class="form-label">{{ $t('_common.password') }}</label>
|
|
<input type="password" class="form-control" id="passwordInput" autocomplete="password"
|
|
v-model="passwordData.password" required />
|
|
</div>
|
|
<div class="mb-3 form-check">
|
|
<label for="savePassword" class="form-check-label">{{ $t('login.save_password') }}</label>
|
|
<input type="checkbox" class="form-check-input" id="savePassword" v-model="savePassword"/>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100 mb-2" v-bind:disabled="loading">
|
|
{{ $t('welcome.login') }}
|
|
</button>
|
|
<div class="alert alert-danger" v-if="error"><b>{{ $t('_common.error') }}</b> {{error}}</div>
|
|
<div class="alert alert-success" v-if="success">
|
|
<b>{{ $t('_common.success') }}</b> {{ $t('welcome.login_success') }}
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</body>
|
|
|
|
<script type="module">
|
|
import { createApp, ref, onMounted } from "vue"
|
|
import { loadAutoTheme } from './theme'
|
|
import { initApp } from './init'
|
|
|
|
let app = createApp({
|
|
setup() {
|
|
loadAutoTheme()
|
|
|
|
const error = ref('');
|
|
const success = ref(false);
|
|
const loading = ref(false);
|
|
const savePassword = ref(false);
|
|
|
|
const data = {
|
|
error,
|
|
success,
|
|
loading,
|
|
savePassword,
|
|
passwordData: {
|
|
username: '',
|
|
password: ''
|
|
}
|
|
}
|
|
|
|
const savedPasswordStr = localStorage.getItem('login')
|
|
if (savedPasswordStr) {
|
|
try {
|
|
const { username, password } = JSON.parse(savedPasswordStr);
|
|
savePassword.value = true;
|
|
data.passwordData = {
|
|
username,
|
|
password
|
|
}
|
|
} catch (e) {
|
|
console.error('Reading saved password failed!', e);
|
|
}
|
|
}
|
|
|
|
return data
|
|
},
|
|
methods: {
|
|
login() {
|
|
this.error = null;
|
|
this.loading = true;
|
|
if (!this.savePassword) {
|
|
localStorage.removeItem('login')
|
|
}
|
|
fetch("./api/login", {
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
method: 'POST',
|
|
body: JSON.stringify(this.passwordData),
|
|
}).then((res) => {
|
|
this.loading = false;
|
|
if (res.status === 200) {
|
|
this.success = true;
|
|
if (this.savePassword) {
|
|
localStorage.setItem('login', JSON.stringify(this.passwordData));
|
|
}
|
|
const url = new URL(window.location);
|
|
const redirectUrl = url.searchParams.get('redir');
|
|
const hash = url.hash;
|
|
if (redirectUrl && redirectUrl.startsWith('.')) {
|
|
location.href = redirectUrl + hash;
|
|
} else {
|
|
location.href = './' + hash;
|
|
}
|
|
} else {
|
|
if (res.status === 401) {
|
|
throw new Error('Please check your username and password')
|
|
} else {
|
|
throw new Error(`Server returned ${res.status}`);
|
|
}
|
|
}
|
|
}).catch((e) => {
|
|
this.error = `Login failed: ${e.message}`;
|
|
});
|
|
},
|
|
},
|
|
});
|
|
|
|
initApp(app);
|
|
</script>
|