How would I create a 'Join Code' like on Google Classroom?

On sites like https://classroom.google.com/u/0/h , you can join your educator’s class with a code of scattered numbers and lowercase letters, and it’s about 7 characters long, no spaces. For example:

sauf4sg
w78fh32
a970nb2

How would I create a system where a certain member could create a page, have a code (like above) for that page, then other members could insert that code somewhere, and then they would join that page? Also, no two codes should ever be the same.

To whoever solves this, thank you very much for the help and your time.

Try this->

$w.onReady(function () { 
   console.log(ransomstr());
});

function ransomstr() { 
 function s4(size) { 
  return Math.floor((1 + Math.random()) * 0x10000)       .toString(36)     
    .substring(1, size);  
     }
       return  s4(4) + '-' + s4(3) + '-' + s4(4) ;
        }

After getting the code, check if the code aldready exist in the code database or not