Files
Apollo/src_assets/common/assets/web/login.html
2024-09-19 23:42:26 +08:00

118 lines
3.7 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="save" 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 } from "vue"
import { initApp } from './init'
let app = createApp({
setup() {
const savedPasswordStr = localStorage.getItem('login')
if (savedPasswordStr) {
try {
const { username, password } = JSON.parse(savedPasswordStr);
return {
error: null,
success: false,
loading: false,
savePassword: true,
passwordData: {
username,
password
}
}
} catch (e) {
console.error('Reading saved password failed!', e);
}
}
return {
error: null,
success: false,
loading: false,
savePassword: false,
passwordData: {
username: "",
password: ""
},
}
},
methods: {
save() {
this.error = null;
this.loading = true;
fetch("/api/login", {
method: "POST",
body: JSON.stringify(this.passwordData),
}).then((res) => {
this.loading = false;
if (res.status === 200) {
this.success = true;
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) {
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>