Enhanced security by ensuring users are redirected to a 404 error page if they pass an incorrect roomId path, either intentionally or unintentionally. This is a critical security mechanism that should be included in our MVP. Let's discuss extracting hardcoded elements, such as lengths or the separator, into proper constants to improve code maintainability. I was concerned that this might make the code harder to read, it could enhance clarity and reusability in the long term. I prefer exposing the roomIdRegex from the same location where we generate IDs. However, this increases the responsibility of that file. Lmk if you have any suggestion for a better organization. Additionally, the current 404 error page displays a 'Page not found' message for invalid room IDs. Should we update this message to 'Invalid room name' to provide more context to the user?
31 lines
815 B
TypeScript
31 lines
815 B
TypeScript
|
|
const getRandomChar = () => {
|
|
// Google Meet uses only letters in a room identifier
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
const charactersLength = characters.length;
|
|
return characters.charAt(Math.floor(Math.random() * charactersLength))
|
|
}
|
|
|
|
const generateSegment = (length: number): string => {
|
|
let segment = '';
|
|
for (let i = 0; i < length; i++) {
|
|
segment += getRandomChar();
|
|
}
|
|
return segment;
|
|
};
|
|
|
|
export const generateRoomId = () => {
|
|
// Generates a unique room identifier following the Google Meet format
|
|
const shortLength = 3;
|
|
const longLength = 4;
|
|
const parts = [
|
|
generateSegment(shortLength),
|
|
generateSegment(longLength),
|
|
generateSegment(shortLength)
|
|
];
|
|
return parts.join('-');
|
|
}
|
|
|
|
export const roomIdRegex = /^[/](?<roomId>[a-z]{3}-[a-z]{4}-[a-z]{3})$/;
|
|
|