refactor authenticate method