generated from templates/ai-develops
Add front-end login demo #4
51
static/js/login.js
Normal file
51
static/js/login.js
Normal file
@@ -0,0 +1,51 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const form = document.getElementById('loginForm');
|
||||
const username = document.getElementById('username');
|
||||
const password = document.getElementById('password');
|
||||
const remember = document.getElementById('remember');
|
||||
const error = document.getElementById('error');
|
||||
|
||||
function showError(msg){
|
||||
error.textContent = msg;
|
||||
}
|
||||
|
||||
function validate() {
|
||||
if (!username.value || username.value.trim().length < 3) {
|
||||
showError('用户名至少 3 个字符');
|
||||
return false;
|
||||
}
|
||||
if (!password.value || password.value.length < 6) {
|
||||
showError('密码至少 6 个字符');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
showError('');
|
||||
if (!validate()) return;
|
||||
|
||||
// 本地模拟认证:示例用户名/密码对
|
||||
const validUsers = { 'alice':'password123', 'bob':'secret123', 'beppeb':'demoPass' };
|
||||
const inputUser = username.value.trim();
|
||||
const inputPass = password.value;
|
||||
|
||||
if (validUsers[inputUser] && validUsers[inputUser] === inputPass) {
|
||||
// 登录成功:写入本地 token(示例)并跳转首页
|
||||
const token = 'demo-token-' + Math.random().toString(36).slice(2);
|
||||
if (remember.checked) {
|
||||
localStorage.setItem('demo_auth_token', token);
|
||||
} else {
|
||||
sessionStorage.setItem('demo_auth_token', token);
|
||||
// keep a copy in localStorage to let index.html detect login; it's demo-only
|
||||
localStorage.setItem('demo_auth_token', token);
|
||||
}
|
||||
// 简短提示后跳转
|
||||
showError('登录成功,正在跳转……');
|
||||
setTimeout(() => { window.location.href = '/'; }, 700);
|
||||
} else {
|
||||
showError('用户名或密码不正确(此示例使用本地模拟账号)。');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user