Refactoring js function to create a new user key