Refactor auth flow