Refactor auth logic