# Unity AuthenticateCustomAsync Error

**URL:** <https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864>\
**Category:** Local Setup\
**Created:** [December 10, 2024, 11:46pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864 "2024-12-10T23:46:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 10, 2024, 11:46pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/1 "2024-12-10T23:46:33Z")

</div>

I am setting up a Nakama connection in Unity.  
When I do AuthenticateDeviceAsync it works but when I try to use AuthenticateCustomAsync I get the error “Authentication failed: Exceeded max retry attempts.”.  
When I use this same function in UE5 it works so idk what the problem is

using UnityEngine;  
using Nakama;  
using System.Threading.Tasks;

public class NakamaService  
: MonoBehaviour  
{  
[SerializeField] private NakamaConfig nakamaConfig;

```
public static IClient Client;
public static ISession Session;
public static ISocket Socket;

private async void Start()
{
    Debug.Log("Initializing Nakama...");

    string scheme = nakamaConfig.UseSSL ? "https" : "http";
    Client = new Client(
        scheme,
        nakamaConfig.Host,
        nakamaConfig.Port,
        nakamaConfig.ServerKey
    );

    Client.Timeout = nakamaConfig.Timeout;
    Client.Logger = new UnityLogger();
    

    try
    { 
        Session = await AuthenticateCustomAsync();
    }
    catch (System.TimeoutException)
    {
        Debug.LogError("Connection timed out. Could not authenticate.");
    }
    catch (System.Exception ex)
    {
        Debug.LogError($"An error occurred: {ex.Message}");
    }
    
    await InitializeSocket();
}

private async Task<ISession> AuthenticateCustomAsync()
{
    string customId = System.Guid.NewGuid().ToString(); // Generate unique ID
    Client.Logger.InfoFormat("Authenticating with custom ID: {0}", customId);
    try
    {
        ISession session = await Client.AuthenticateCustomAsync(customId);
        Debug.Log($"Authenticated with Nakama! User ID: {session.UserId}");
        return session;
    }
    catch (System.Exception ex)
    {
        Debug.LogError($"Authentication failed: {ex.Message}");
        throw;
    }
}

 private async Task InitializeSocket()
{
    Socket = Client.NewSocket();

    Socket.Connected += () => Debug.Log("Socket connected!");
    Socket.Closed += () => Debug.Log("Socket disconnected!");

    try
    {
        await Socket.ConnectAsync(Session, true);
        Debug.Log("Socket connection successful!");
    }
    catch (System.Exception ex)
    {
        Debug.LogError($"Failed to connect socket: {ex.Message}");
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 11, 2024, 1:17pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/2 "2024-12-11T13:17:00Z")

</div>

Hello @Doglightning,

Are there no server logs that may indicate the issue?

Best.

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 11, 2024, 4:36pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/3 "2024-12-11T16:36:39Z")

</div>

Here is my log for AuthenticateDeviceAsync

2024-12-11 11:33:08 {“level”:“info”,“ts”:“2024-12-11T16:33:08.508Z”,“caller”:“server/session\_ws.go:81”,“msg”:“New WebSocket session connected”,“uid”:“4a3686ba-ddc5-461f-82ff-7b67b5a1f97a”,“sid”:“9ad50ac0-b7dd-11ef-9a26-006100a0eb06”,“format”:0}  
2024-12-11 11:34:13 {“level”:“info”,“ts”:“2024-12-11T16:34:13.454Z”,“caller”:“server/session\_ws.go:520”,“msg”:“Closed client connection”,“uid”:“4a3686ba-ddc5-461f-82ff-7b67b5a1f97a”,“sid”:“9ad50ac0-b7dd-11ef-9a26-006100a0eb06”}

There are no logs for AuthenticateCustomAsync

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 11, 2024, 5:30pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/4 "2024-12-11T17:30:58Z")

</div>

I think the client logs what’s causing the requests to retry and cause the failure, can you paste them here please?

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 11, 2024, 6:01pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/5 "2024-12-11T18:01:03Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/4/45bb0b9c73032fcae7bdd494b87d4283c2467cc6.png)

Here is the log for the POST

Send: method=‘POST’, uri=‘[http://localhost:7350/v2/account/authenticate/custom?create=true&](http://localhost:7350/v2/account/authenticate/custom?create=true&)’, body=‘System.Byte’  
UnityEngine.Debug:LogFormat (string,object)  
Nakama.UnityLogger:InfoFormat (string,object) (at Assets/Packages/Nakama/Runtime/UnityLogger.cs:31)  
Nakama.HttpRequestAdapter/d\_\_8:MoveNext ()  
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<string>:Start<Nakama.HttpRequestAdapter/<SendAsync>d__8> (Nakama.HttpRequestAdapter/<SendAsync>d__8&) Nakama.HttpRequestAdapter:SendAsync (string,System.Uri,System.Collections.Generic.IDictionary`2\<string, string\>,byte,int,System.Nullable`1<System.Threading.CancellationToken>) Nakama.ApiClient/<AuthenticateCustomAsync>d__12:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:Start\<Nakama.ApiClient/d\_\_12\> (Nakama.ApiClient/d\_\_12&)  
Nakama.ApiClient:AuthenticateCustomAsync (string,string,Nakama.ApiAccountCustom,System.Nullable`1<bool>,string,System.Nullable`1\<System.Threading.CancellationToken\>)  
Nakama.Client/\<\>c\_\_DisplayClass45\_0:b\_\_0 ()  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:Start\<Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>> (Nakama.RetryInvoker/<InvokeWithRetry>d__2`1\<Nakama.IApiSession\>&)  
Nakama.RetryInvoker:InvokeWithRetry\<Nakama.IApiSession\> (System.Func`1<System.Threading.Tasks.Task`1\<Nakama.IApiSession\>\>,Nakama.RetryHistory)  
Nakama.Client/d\_\_45:MoveNext ()  
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Nakama.ISession>:Start<Nakama.Client/<AuthenticateCustomAsync>d__45> (Nakama.Client/<AuthenticateCustomAsync>d__45&) Nakama.Client:AuthenticateCustomAsync (string,string,bool,System.Collections.Generic.Dictionary`2\<string, string\>,Nakama.RetryConfiguration,System.Threading.CancellationToken)  
NakamaService/d\_\_18:MoveNext () (at Assets/Scripts/NakamaService/NakamaService.cs:65)  
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.ISession\>:Start\<NakamaService/d\_\_18\> (NakamaService/d\_\_18&)  
NakamaService:AuthenticateDeviceAsync ()  
NakamaService/d\_\_17:MoveNext () (at Assets/Scripts/NakamaService/NakamaService.cs:33)  
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start\<NakamaService/d\_\_17\> (NakamaService/d\_\_17&)  
NakamaService:Start ()

Here is the retry error  
Authentication failed: Exceeded max retry attempts.  
UnityEngine.Debug:LogError (object)  
NakamaService/d\_\_18:MoveNext () (at Assets/Scripts/NakamaService/NakamaService.cs:71)  
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Nakama.ISession>:SetException (System.Exception) Nakama.Client/<AuthenticateCustomAsync>d__45:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.RetryInvoker/d\_\_2`1<Nakama.IApiSession>:MoveNext () System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1\<Nakama.IApiSession\>:SetException (System.Exception)  
Nakama.ApiClient/d\_\_12:MoveNext ()  
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetException (System.Exception)  
Nakama.HttpRequestAdapter/d\_\_8:MoveNext ()  
UnityEngine.UnitySynchronizationContext:ExecuteTasks ()

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 11, 2024, 7:08pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/6 "2024-12-11T19:08:46Z")

</div>

Are you sure that the config values that are passed to the client constructor are correct?

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 11, 2024, 7:49pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/7 "2024-12-11T19:49:48Z")

</div>

Yes, It works with Client.AuthenticateDeviceAsync(deviceId) using the exact same Client that fails with AuthenticateCustomAsync

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 11, 2024, 8:09pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/8 "2024-12-11T20:09:44Z")

</div>

What versions of the client and server are you using? Do you have any custom hooks in place on `AuthenticateCustom` ?

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 11, 2024, 8:19pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/9 "2024-12-11T20:19:30Z")

</div>

Client: 3.12.0  
Server: 3.22.0  
No custom hooks

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 12, 2024, 3:56pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/10 "2024-12-12T15:56:18Z")

</div>

Have you tried invoking the REST API directly using cURL?

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 12, 2024, 4:41pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/11 "2024-12-12T16:41:48Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/3/30d1ed6d37a42814145efa73f0c0aad3e38ccb55.png)

I am not 100% sure how to do the API call as it says my key is invalid but this is the key used on my unity project that works

---

<div class="post-metadata">

**Author:** ![Doglightning](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/doglightning/32/2412_2.png) [@Doglightning](https://forum.heroiclabs.com/u/Doglightning)\
**Post date:** [December 12, 2024, 4:42pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/12 "2024-12-12T16:42:21Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/2/21201736e6ca13bc7670a9aa31ef8a167f8fb6d2.png)

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [December 13, 2024, 2:33pm UTC](https://forum.heroiclabs.com/t/unity-authenticatecustomasync-error/5864/13 "2024-12-13T14:33:38Z")

</div>

For authentication APIs you must set the request auth to Basic with username `defaultkey` and the body should be something like:

```auto
{"id": "some-custom-id"}

```
