الملفات
codepill-sfu/public/js/Login.js
2025-08-17 13:25:39 +02:00

145 أسطر
5.1 KiB
JavaScript

'use strict';
console.log(window.location);
const usernameInput = document.getElementById('username');
const passwordInput = document.getElementById('password');
const loginForm = document.getElementById('loginForm');
const loginBtn = document.getElementById('loginButton');
const joinRoomForm = document.getElementById('joinRoomForm');
const selectRoom = document.getElementById('selectRoom');
const joinSelectRoomBtn = document.getElementById('joinSelectRoomButton');
usernameInput.onkeyup = (e) => {
if (e.keyCode === 13) {
e.preventDefault();
login();
}
};
passwordInput.onkeyup = (e) => {
if (e.keyCode === 13) {
e.preventDefault();
login();
}
};
loginBtn.onclick = (e) => {
login();
};
joinSelectRoomBtn.onclick = (e) => {
join();
};
function login() {
const username = filterXSS(document.getElementById('username').value);
const password = filterXSS(document.getElementById('password').value);
// http://localhost:3010/join/?room=test
// http://localhost:3010/join/?room=test&roomPassword=0&name=admin&audio=0&video=0&screen=0&notify=0
const qs = new URLSearchParams(window.location.search);
const room = filterXSS(qs.get('room'));
// http://localhost:3010/join/test
const pathParts = window.location.pathname.split('/');
const roomPath = filterXSS(pathParts[pathParts.length - 1]);
if (username && password) {
axios
.post('/login', {
username: username,
password: password,
})
.then(function (response) {
console.log(response);
// Store in session
const token = response.data.message;
window.sessionStorage.peer_token = token;
// User (has access to some room)
const allowedRooms = response.data.allowedRooms;
if (allowedRooms && !allowedRooms.includes('*')) {
console.log('User detected with limited join room access!', allowedRooms);
loginForm.style.display = 'none';
joinRoomForm.style.display = 'block';
allowedRooms.forEach((room) => {
const option = document.createElement('option');
option.value = room;
option.text = room;
selectRoom.appendChild(option);
});
return;
}
// Admin (has access to all rooms)
if (allowedRooms && allowedRooms.includes('*')) {
console.log('User detected with admin access!', allowedRooms);
loginForm.style.display = 'none';
joinRoomForm.style.display = 'block';
selectRoom.innerHTML = '';
const input = document.createElement('input');
input.type = 'text';
input.id = 'customRoomInput';
input.placeholder = 'Enter room name';
input.className = 'form-control mb-2';
selectRoom.parentNode.insertBefore(input, selectRoom);
selectRoom.style.display = 'none';
joinSelectRoomBtn.onclick = () => {
const roomName = filterXSS(document.getElementById('customRoomInput').value);
if (roomName) {
window.location.href =
'/join/?room=' +
roomName +
'&name=' +
username +
'&token=' +
window.sessionStorage.peer_token;
} else {
popup('warning', 'Room name required');
}
};
return;
}
if (room) {
window.location.href = '/join/' + window.location.search;
return;
}
if (roomPath && roomPath !== 'login') {
window.location.href = '/join/' + roomPath;
return;
}
window.location.href = '/logged';
return;
})
.catch(function (error) {
console.error(error);
popup('warning', 'Invalid credentials. Please try again.');
});
return;
}
if (!username && !password) {
popup('warning', 'Username and Password required');
return;
}
if (!username) {
popup('warning', 'Username required');
return;
}
if (!password) {
popup('warning', 'Password required');
return;
}
}
function join() {
//window.location.href = '/join/' + selectRoom.value;
const username = filterXSS(document.getElementById('username').value);
const roomId = filterXSS(document.getElementById('selectRoom').value);
window.location.href = '/join/?room=' + roomId + '&name=' + username + '&token=' + window.sessionStorage.peer_token;
}