Umar bin Khattab reported,

The Messenger of Allah ﷺ said, "Actions are judged by intentions, and every person will have what they intended. Whoever migrates for the sake of Allah and His Messenger, his migration is for Allah and His Messenger. Whoever migrates for some worldly gain he seeks or a woman he wishes to marry, his migration is for whatever he migrated toward."

Bukhari 54

Sahih

حدثنا عبد الله بن مسلمة قال أخبرنا مالك عن يحيى بن سعيد عن محمد بن إبراهيم عن علقمة بن وقاص

عن عمر

أن رسول الله ﷺ قال الأعمال بالنية ولكل امرئ ما نوى فمن كانت هجرته إلى الله ورسوله فهجرته إلى الله ورسوله ومن كانت هجرته لدنيا يصيبها أو امرأة يتزوجها فهجرته إلى ما هاجر إليه

البخاري 54صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Faith
Chapter
Deeds being judged by intention and sincerity, and that every person gets what they intended
Numbering
Darussalam 54
Bukhari 54