54 lines
2.1 KiB
JavaScript
54 lines
2.1 KiB
JavaScript
document.getElementById('loginForm').addEventListener('submit', function(event) {
|
|
event.preventDefault();
|
|
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
|
|
const indicatorBackground = document.getElementById('indicator-background');
|
|
indicatorBackground.style.display = 'flex';
|
|
|
|
fetch('/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ username, password })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
indicatorBackground.style.display = 'none';
|
|
|
|
if (data.success) {
|
|
// alert('로그인이 성공했습니다!');
|
|
fetch(`/getGroups?username=${username}`)
|
|
.then(response => response.json())
|
|
.then(groupData => {
|
|
console.log('그룹 정보:', groupData);
|
|
|
|
// 그룹 데이터에서 name이 'administrators'인 그룹이 있는지 확인
|
|
const isAdmin = groupData.some(group => group.name === 'administrators');
|
|
|
|
if (isAdmin) {
|
|
alert('관리자 계정으로 접속하였습니다.');
|
|
window.location.href = '/admin'; // admin 페이지로 리다이렉트
|
|
} else {
|
|
alert('로그인에 성공하였습니다.');
|
|
window.location.href = '/'; // 일반 사용자 페이지로 리다이렉트
|
|
}
|
|
|
|
// 그룹 정보를 웹 페이지에 표시하거나 처리하는 로직 추가
|
|
})
|
|
.catch(error => {
|
|
console.error('그룹 정보를 가져오는 중 오류 발생:', error);
|
|
alert('그룹 정보를 가져오는 중 오류가 발생했습니다.');
|
|
});
|
|
} else {
|
|
alert('아이디 또는 비밀번호가 잘못되었습니다.');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
indicatorBackground.style.display = 'none';
|
|
alert('로그인 중 오류가 발생했습니다.');
|
|
});
|
|
}); |