step-up-labs / firebase-authentication-dotnet

C# library for Firebase Authentication
MIT License
376 stars 129 forks source link

Email Verification Implementation Help #203

Open koddek opened 1 year ago

koddek commented 1 year ago

Ok so I am attempting to implement Email Verification to this project. So far I have the REST url, and some classes:

internal static class Endpoints
{
    // ...
    public const string GoogleEmailUrl = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key={0}";
}

public class VerifyEmailRequest
{
    public const string EmailVerificationType = "VERIFY_EMAIL";

    public VerifyEmailRequest()
    {
        this.RequestType = EmailVerificationType;
    }

    public string IdToken { get; set; }

    public string RequestType { get; set; }
}

public class VerifyEmailResponse
{
    public string Email { get; set; }
}

public class VerifyEmail : FirebaseRequestBase<VerifyEmailRequest, VerifyEmailResponse>
{
    public VerifyEmail(FirebaseAuthConfig config) : base(config)
    {
    }

    protected override string UrlFormat => Endpoints.GooglePasswordUrl;
}

Where do I go from here? I would like to have a method in the FirebaseAuthClient class that says VerifyEmail(email), where an email is sent to the provided email address with a link that verifies the user when they click on it.

asarr22 commented 1 year ago

Hi, you can add a method to your project to send an activation email to the email address after Signing Up . When Signing in, FirebaseAuthClient has a method to verify if the user has activated their email with:

var userInfo = FirebaseAuthHelper.GetUserInfo(Your FirebaseAuthClient);
             bool flag = userInfo.IsEmailVerified;
             if(flag)
            {
                  //if email is verified
            }     

A method that can send a verification email to a signed up email address:

using System.Net.Http.Headers;

        private const string FirebaseApiKey = "Your API KEY;
        private const string RequestUri = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=" + FirebaseApiKey;

        public static async Task SendVerificationEmailAsync(string USER TOKEN)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var content = new StringContent("{\"requestType\":\"VERIFY_EMAIL\",\"idToken\":\"" + idToken + "\"}");
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var response = await client.PostAsync(RequestUri, content);
                response.EnsureSuccessStatusCode();
            }
        }