Skip to content

Commit

Permalink
[call-me] - add username validator
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Jan 4, 2025
1 parent 850cff8 commit 18b4bf1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
31 changes: 31 additions & 0 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ app.get('/join/', (req, res) => {
return unauthorized(res);
}

const isValidUser = isValidUsername(user);
console.log('isValidUser', { user: user, valid: isValidUser });
if (!isValidUser) {
return unauthorized(res);
}

const isValidCall = isValidUsername(user);
console.log('isValidCall', { call: call, valid: isValidCall });
if (!isValidCall) {
return unauthorized(res);
}

if (user || (user && call)) {
return res.sendFile(HOME);
}
Expand Down Expand Up @@ -306,6 +318,19 @@ function handleConnection(socket) {
// Function to handle user sign-in request
function handleSignIn(data) {
const { name } = data;

const isValidName = isValidUsername(name);
console.log('isValidName', { username: name, valid: isValidName });
if (!isValidName) {
sendMsgTo(socket, {
type: 'signIn',
success: false,
message:
'Invalid username.<br/> Allowed letters, numbers, underscores, periods, hyphens, and @. Length: 3-36 characters.',
});
return;
}

if (!users.has(name)) {
users.set(name, socket);
socket.username = name;
Expand Down Expand Up @@ -377,6 +402,12 @@ function handleConnection(socket) {
}
}

// Allow letters, numbers, underscores, periods, hyphens, and @. Length: 3-36 characters
function isValidUsername(username) {
const usernamePattern = /^[a-zA-Z0-9_.-@]{3,36}$/;
return usernamePattern.test(username);
}

// Function to get all connected users
function getConnectedUsers() {
return Array.from(users.keys());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "call-me",
"version": "1.0.42",
"version": "1.0.43",
"description": "Your Go-To for Instant Video Calls",
"author": "Miroslav Pejic - miroslav.pejic.85@gmail.com",
"license": "AGPLv3",
Expand Down
12 changes: 7 additions & 5 deletions public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ document.addEventListener('DOMContentLoaded', function () {
// Handle config
const elementsToHide = [
{ condition: !app.showGithub, element: githubDiv },
{ condition: !app.attribution, element: attribution }
{ condition: !app.attribution, element: attribution },
];

elementsToHide.forEach(({ condition, element }) => {
Expand Down Expand Up @@ -478,10 +478,12 @@ function handleNotFound(data) {

// Handle sign-in response from the server
function handleSignIn(data) {
const { success } = data;
const { success, message } = data;
if (!success) {
handleError('Username already in use.<br/>Please try a different one.');
setTimeout(handleHangUpClick, 3000);
handleError(message);
if (!message.startsWith('Invalid username')) {
setTimeout(handleHangUpClick, 3000);
}
} else {
githubDiv.style.display = 'none';
attribution.style.display = 'none';
Expand Down Expand Up @@ -670,7 +672,7 @@ function handleLeave() {
}

// Handle and display errors
function handleError(message, error = false, position = 'center', timer = 4000) {
function handleError(message, error = false, position = 'center', timer = 6000) {
if (error) console.error(error);
sound('notify');
Swal.fire({
Expand Down

0 comments on commit 18b4bf1

Please sign in to comment.