aws-samples / aws-sdk-unity-samples

This repository has samples that demonstrate various aspects of the AWS Mobile SDK for Unity, you can get the SDK source on Github (https://github.com/aws/aws-sdk-net)
Other
161 stars 107 forks source link

Add a Cognito Sync Developer Authenticated Identities Sample #13

Closed tobischulz closed 11 months ago

tobischulz commented 8 years ago

Hi,

please add a "Cognito Sync Developer Authenticated Identities Sample"! I can´t get this to work from the this doc: https://docs.aws.amazon.com/cognito/devguide/identity/developer-authenticated-identities/

My DeveloperAuthenticatedCredentials RefreshIdentity() is never get called and Unity is hanging to death. Have to kill unity to work again.

The "ContactProvider" it self works good for me, i get my valid token. But it will never get called by the DeveloperAuthenticatedCredentials by construct.

`

using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Threading; using System.Text; using ThirdParty.Json.LitJson;

using Amazon; using Amazon.CognitoSync; using Amazon.Runtime; using Amazon.CognitoIdentity; using Amazon.CognitoIdentity.Model; using Amazon.CognitoSync.SyncManager;

public class DeveloperAuthenticatedCredentials : CognitoAWSCredentials {

const string PROVIDER_NAME = "my.login.provider.lambdaauth";
const string IDENTITY_POOL = "eu-west-1:**********************241d67a79a";
static readonly RegionEndpoint REGION = RegionEndpoint.EUWest1;
private string email = "demo@demo.com";
private string password = "testpassword";
private string login = null;

public DeveloperAuthenticatedCredentials(string loginAlias)
    : base(IDENTITY_POOL, REGION)
{
    login = loginAlias;
}

protected override IdentityState RefreshIdentity()
{
    IdentityState state = null;
    ManualResetEvent waitLock = new ManualResetEvent(false);
    MainThreadDispatcher.ExecuteCoroutineOnMainThread(ContactProvider((s) =>
        {
            state = s;
            waitLock.Set();
        }));
    waitLock.WaitOne();
    return state;
}

IEnumerator ContactProvider(Action<IdentityState> callback)
{
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers.Add ("Content-Type", "application/json");

    string postData = "{\"email\":\"" + email + "\", \"password\":\"" + password + "\"}";
    Debug.Log (postData);

    byte[] pData = Encoding.ASCII.GetBytes (postData.ToCharArray());

    WWW www = new WWW("https://****.execute-api.eu-west-1.amazonaws.com/dev/login", pData, headers);
    yield return www;
    string response = www.text;

    Debug.Log ("Response Text: " + response);

    JsonData json = JsonMapper.ToObject(response);

    //The backend has to send us back an Identity and a OpenID token
    string identityId = json["identityId"].ToString();
    string token = json["token"].ToString();

    Debug.Log ("Token: " + token);

    IdentityState state = new IdentityState(identityId, PROVIDER_NAME, token, false);
    callback(state);
}

} `

Cognito Sync Unity3d Sample edited: ` public class MyOwnCognitoSyncManager : MonoBehaviour {

    private Dataset playerInfo;
    private string playerName;
    private string statusMessage = "";
    public string IdentityPoolId = "";

    private DeveloperAuthenticatedCredentials _credentials;
    private DeveloperAuthenticatedCredentials Credentials
    {
        get
        {
            if (_credentials == null)
                _credentials = new DeveloperAuthenticatedCredentials("will be manual set in ContactProvider");
            return _credentials;
        }
    }

    private CognitoSyncManager _syncManager;
    private CognitoSyncManager SyncManager
    {
        get
        {
            if (_syncManager == null)
            {
                _syncManager = new CognitoSyncManager(Credentials, new AmazonCognitoSyncConfig { RegionEndpoint = RegionEndpoint.EUWest1 });
            }
            return _syncManager;
        }
    }

    void Start() {
        playerInfo = SyncManager.OpenOrCreateDataset ("PlayerInfo");

        playerName = string.IsNullOrEmpty (playerInfo.Get ("playerName")) ? "Spieler" : playerInfo.Get ("playerName");

        playerInfo.OnSyncSuccess += this.HandleSyncSuccess; // OnSyncSucess uses events/delegates pattern
        playerInfo.OnSyncFailure += this.HandleSyncFailure; // OnSyncFailure uses events/delegates pattern
        playerInfo.OnSyncConflict = this.HandleSyncConflict;
        playerInfo.OnDatasetMerged = this.HandleDatasetMerged;
        playerInfo.OnDatasetDeleted = this.HandleDatasetDeleted;
    }

`

This would be very helpful for everyone. It´s so hard to get simple email & password auth with aws...

PLEASE! <3