'How do I successfully generate and send email verification link to new users that just signed-up using Firebase in xamarin.forms?

I am using a dependency service to create new users with their email and password as follows in my signupviewmodel:

token = await DependencyService.Get<IFirebaseAuthenticator>().SignUpWithEmailPasswordAsync(Email, Password);

But, I want to be able to send an email for verification to each new user so that if their email has not been verified, I will not allow them to access the app.

I have already initialized the firebase app in MainActivity.cs as follows:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static FirebaseAuth Auth;

    public event EventHandler<ActivityResultEventArgs> ActivityResult = delegate { };
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        
        InitFirebaseAuth(); //Create an instance of FirebaseAuth

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.RecordAudio) != Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.RecordAudio }, 1);
        }

        FormsVideoPlayer.Init();
    }

But, after successfully creating the new user and when I try to generate the email verification link, the app throws up this error: System.NullReferenceException:** 'Object reference not set to an instance of an object.'

This is the code I am implementing in SignUpViewModel:

                var token = await DependencyService.Get<IFirebaseAuthenticator>().SignUpWithEmailPasswordAsync(Email, Password);

            if (token != null)
            {
                var auth = FirebaseAuth.DefaultInstance;
                var token2 = await auth.GenerateEmailVerificationLinkAsync(Email); 

                if (token2 != null) 
                { 
                  //SendEmailVerificationLink to New User
                }
            }

So, how do I get the FirebaseApp I initialized in MainActivity.cs to be referenced in the SignUpViewModel?

This is how the InitFirebaseAuth() was coded in MainActivity.cs:

public static FirebaseAuth Auth;
        private void InitFirebaseAuth()
    {

        var options = new FirebaseOptions.Builder()
            .SetProjectId("MY_PROJECT_ID")
            .SetApplicationId("MY_APPLICATION_ID")
            .SetApiKey("MY_FIREBASE_API_KEY")
            .SetDatabaseUrl("MY_DATABASEURL")
            .SetStorageBucket("MY_STORAGEBUCKET")
            .Build();

        var _fireApp = FirebaseApp.InitializeApp(this, options);

        Auth = FirebaseAuth.GetInstance(_fireApp);
    }


Solution 1:[1]

You must traverse the visual tree to get the element. Based on this example (How to: Find DataTemplate-Generated Elements), you can either lookup the element by its type or, after some minor modification, by its name. This recursive algorithm allows you to retrieve any element without any knowledge of the exact visual tree composition.

// The parameter 'obj' is the parent element.
// In your case this is the DockPanel.
private FrameworkElement FindVisualChildByName(DependencyObject obj, string elementName)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child != null 
          && child is FrameworkElement element
          && element.Name.Equals(elementName, StringComparison.OrdinalIgnoreCase))
        {
            return element;
        }
        else
        {
            FrameworkElement childOfElement = FindVisualChild(element, elementName);
            if (childOfElement != null)
                return childOfChild;
        }
    }
    return null;
}

Example

var textBlock = FindVisualChildByName(this.fastcard, "fastcolor") as TextBlock;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1