Refactoring js auth