question

Jackie Youwakim avatar image
Jackie Youwakim asked

Send email verification when registering account

Hello,

I am trying to send an email to new users when they create an account so that confirm their email address before being able to login. I tried following the documentation, this one here specifically, but the code seems to be outdated.

I have a put a comment where I get the error. In the documentation it says AddOrUpdateContactEmailRequest only requires an email, which kind of makes the first function useless.

I still tried it and removed PlayFabId = playFabId, but it gave me this PlayFabError: claim was not allowed to perform the requested action based on the entity's access policy. Policy comment: By default, all requests are denied. If you expected this request to succeed, you may be missing a policy. See the permissions APIs in PlayFab's Admin Api to add a permission. I did set the CreateAccount to false and I put the players title ID in the Custom ID.

So I tried only using the second function directly instead of passing through the first one which gave me a PlayFabError HTTP/1.1 409 Conflict.

It seems I can't update the contact email and because my players don't have a contact email, I can't send them any kind of emails.

void AddContactEmailToPlayer()
{
    var loginReq = new LoginWithCustomIDRequest
    {
        CustomId = "SomeCustomID", // replace with your own Custom ID
        CreateAccount = true // otherwise this will create an account with that ID
    };


    var emailAddress = "testaddress@example.com"; // Set this to your own email


    PlayFabClientAPI.LoginWithCustomID(loginReq, loginRes =>
    {
        Debug.Log("Successfully logged in player with PlayFabId: " + loginRes.PlayFabId);
        AddOrUpdateContactEmail(loginRes.PlayFabId, emailAddress);
    }, FailureCallback);
}


void AddOrUpdateContactEmail(string playFabId, string emailAddress)
{
    var request = new AddOrUpdateContactEmailRequest
    {
        PlayFabId = playFabId, // This line here gives an error, so I removed it
        EmailAddress = emailAddress
    };
    PlayFabClientAPI.AddOrUpdateContactEmail(request, result =>
    {
        Debug.Log("The player's account has been updated with a contact email");
    }, FailureCallback);
}


void FailureCallback(PlayFabError error)
{
    Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
    Debug.LogError(error.GenerateErrorReport());
}
Player DataTitle Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

Firstly, this feature that "confirm their email address before being able to log in." is not integrated into PlayFab. No matter whether the contact email address is verified, registered players can log in. You need to restrict the permissions of no-verified players on your own.

Secondly, this documentation may have been outdated. The “PlayFabId = playFabId” in line 25 needs to be removed. But we need to remain the first function so that the second function would work. In our testing, this sample can work fine after we removed the “PlayFabId = playFabId”. As the API reference AddOrUpdateContactEmail mentions, this API request requires the field SessionTicket in its request headers. And the SessionTicket would be returned in the response of API call LoginWithCustomID. PlayFab SDK would help you fill the SessionTicket to the PlayFab settings automatically. It’s necessary to call the login API before you call any client API.

Thirdly, the CustomID shouldn’t be PlayerTitleId. CustomID is a string that title developers customize for the unique identity of a player. PlayerTitleId is a player identity allocated by PlayFab. The doc -- Quickstart: PlayFab Client library for C# in Unity shows an example of login with customId, you can check the doc for details. Besides, we would suggest you learn about the PlayFab login before you call any client API. Besides, if you keep the “CreateAccount” as “true”, when the CustomID is a new one, a player account would be automatically generated. If this CustomID has linked with an existed PlayFab account, this API would log this account in. So you can remain it as “true.”

About the PlayFab error you mentioned, generally, it would contain the name of the corresponding API call, for example “/Group/ListGroupMembers: The claim was not allowed to perform the requested action based on the entity's access policy. .....” Could you please provide a complete one?

It’s my testing code and the log.

using PlayFab;
using PlayFab.ClientModels;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddContactEmailWhenRegister : MonoBehaviour
{

    void Start()
    {
        AddContactEmailToPlayer();
    }

    void AddContactEmailToPlayer()
    {
        var loginReq = new LoginWithCustomIDRequest
        {
            CustomId = "SomeCustomID",
            CreateAccount = true
        };

        var emailAddress = "testaddress@example.com";

        PlayFabClientAPI.LoginWithCustomID(loginReq, loginRes =>
        {
            Debug.Log("Successfully logged in player with PlayFabId: " + loginRes.PlayFabId);
            AddOrUpdateContactEmail(emailAddress);
        }, FailureCallback);
    }

    void AddOrUpdateContactEmail(string emailAddress)
    {
        var request = new AddOrUpdateContactEmailRequest
        {
            //PlayFabId = playFabId,
            EmailAddress = emailAddress
        };
        PlayFabClientAPI.AddOrUpdateContactEmail(request, result =>
        {
            Debug.Log("The player's account has been updated with a contact email");
        }, FailureCallback);
    }

    void FailureCallback(PlayFabError error)
    {
        Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
        Debug.LogError(error.GenerateErrorReport());
    }
}

The log in the console.

Successfully logged in player with PlayFabId: 7**************D
The player's account has been updated with a contact email
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jackie Youwakim avatar image Jackie Youwakim commented ·

Thank you for the detailed answer!

First, here is the complete error I get /Object/SetObjects: The claim was not allowed to perform the requested... and it happens even when the CreateAccount = true. I am not sure if the cause is elsewhere in the code, but this error only occurs if I call AddContactEmailToPlayer().

I am using LoginWithEmailAddress and users have to register before accessing the platform, so they don't have a CustomID when creating their account. I am currently looking at how to add a CustomID when you register an account, because otherwise it creates 2 players in the Game Manager for one user when someone registers, which is not ideal. (Though if it will do, if that's the only way)

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Jackie Youwakim commented ·

It would be elsewhere in the code. AddOrUpdateContactEmail API has nothing to do with SetObjects. Could you try to search the SetObjects method in the code?

If you want to link a CustomId to an account, please use this API -- LinkCustomID.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.