I heard the Messenger of Allah ﷺ say: “Action is but by intention and each person will have but that which he intended. So whoever migrated for the sake of Allah and His Messenger, then his migration was for the sake of Allah and His Messenger, whoever migrated for some worldly purpose or for the sake of a woman whom he could marry, his migration was for the purpose for which he migrated.” (Using translation from Aḥmad 300)
عَنِ النَّبِيِّ ﷺ قَالَ «إِنَّمَا الْأَعْمَالُ بِالنِّيَّةِ وَإِنَّمَا لِامْرِئٍ مَا نَوَى فَمَنْ كَانَتْ هِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ فَهِجْرَتُهُ إِلَى اللهِ وَرَسُولِهِ وَمَنْ كَانَتْ هِجْرَتُهُ إِلَى دُنْيَا يُصِيبُهَا أَوِ امْرَأَةٍ يَتَزَوَّجُهَا فَهِجْرَتُهُ إِلَى مَا هَاجَرَ إِلَيْهِ»