It is narrated from Sayyiduna Umar bin al-Khattab (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) said: "Actions depend upon intention. Indeed, a person shall have only what he intended. So whoever's emigration was for Allah and His Messenger (peace and blessings be upon him), his emigration is for Allah and His Messenger (peace and blessings be upon him); and whoever emigrated with the intention of some worldly gain that he would attain, or with the intention of a woman whom he would marry, his emigration is for that for which he emigrated."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب قسم الفيء والغنيمة / 12907