Refactor authenticate method.