Skip to content

Commit

Permalink
Fix multi-users login
Browse files Browse the repository at this point in the history
  • Loading branch information
lounsen authored Apr 4, 2024
1 parent 5d08330 commit 93d3a70
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,37 @@ <h1>User Login and Points Query</h1>
fetch('https://ewyagq4wdfvwshsh3.neiwangyun.net/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',

},
body: JSON.stringify({ username, password })
})
.then(response => response.json())
.then(data => {
console.log(data.message);
console.log(data);
if (data.message === 'Login successful') {
document.getElementById('loginForm').style.display = 'none';
document.getElementById('queryForm').style.display = 'block';
document.getElementById('userInfo').style.display = 'block';
document.getElementById('userInfo').innerText = `Welcome, ${data.user.username}! `; // 显示用户信息
// 获取服务器返回的Session ID
const sessionId = data.sessionId;
// 存储Session ID,这里使用LocalStorage
localStorage.setItem('sessionId', sessionId);
document.getElementById('loginForm').style.display = 'none';
document.getElementById('queryForm').style.display = 'block';
document.getElementById('userInfo').style.display = 'block';
document.getElementById('userInfo').innerText = `Welcome, ${username}! `; // 显示用户信息
} else {
alert('Invalid username or password');
alert('Invalid username or password');
}
});
});

document.getElementById('queryBtn').addEventListener('click', function() {
//console.log(document.cookie);
fetch('https://ewyagq4wdfvwshsh3.neiwangyun.net/points')
fetch('https://ewyagq4wdfvwshsh3.neiwangyun.net/points',{
headers: {
'Content-Type': 'application/json',
'session-id': localStorage.sessionId
}
})
.then(response => response.json())
.then(data => {
document.getElementById('points').innerText = `Points: ${data.points}`;
Expand Down

0 comments on commit 93d3a70

Please sign in to comment.