forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerAuthentication.cs
More file actions
92 lines (80 loc) · 3.76 KB
/
Copy pathPlayerAuthentication.cs
File metadata and controls
92 lines (80 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using Unity.Collections;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
using UnityEngine.Networking;
namespace Samples.HelloNetcode
{
public class PlayerAuthentication : MonoBehaviour
{
void Start()
{
if (!ConnectionApprovalData.PlayerAuthenticationEnabled.Data)
return;
// We'll only reach here on client worlds with auth enabled so this will never be done on servers
SignIn();
}
async void SignIn()
{
try
{
await UnityServices.InitializeAsync();
AuthenticationService.Instance.SignedIn += () =>
{
Debug.Log("Sign in anonymously succeeded!");
ConnectionApprovalData.ApprovalPayload.Data.Payload.Append(AuthenticationService.Instance.PlayerId);
ConnectionApprovalData.ApprovalPayload.Data.Payload.Append(':');
ConnectionApprovalData.ApprovalPayload.Data.Payload.Append(AuthenticationService.Instance
.AccessToken);
};
AuthenticationService.Instance.SignInFailed += errorResponse =>
{
Debug.LogError($"Sign in anonymously failed with error code: {errorResponse.ErrorCode}");
};
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
catch (Exception e)
{
Debug.LogError($"Exception when trying to sign in: {e.Message}");
}
}
IEnumerator GetPlayerInfo(PendingApproval pendingApproval)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get($"https://player-auth.services.api.unity.com/v1/users/{pendingApproval.PlayerId}"))
{
webRequest.SetRequestHeader("ProjectId", Application.cloudProjectId);
webRequest.SetRequestHeader("Authorization", $"Bearer {pendingApproval.AccessToken}");
yield return webRequest.SendWebRequest();
bool success = false;
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
Debug.LogError($"GetPlayerInfo ConnectionError: {webRequest.error}");
break;
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError($"GetPlayerInfo DataProcessingError: {webRequest.error}");
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError($"GetPlayerInfo ProtocolError: {webRequest.error}");
break;
case UnityWebRequest.Result.Success:
success = true;
break;
}
ConnectionApprovalData.ApprovalResults.Data.Enqueue(new ApprovalResult(){ Success = success, Payload = pendingApproval.Payload, ConnectionEntity = pendingApproval.ConnectionEntity});
}
}
void Update()
{
// NOTE: This will fetch all queued player approval requests but will only work for up to 15 players at a time as then
// this particular service rate limit will be reached. Would need to fetch the rest in batches after limit is cleared.
// See https://services.docs.unity.com/player-auth/v1/ for more information.
while (ConnectionApprovalData.PendingApprovals.Data.IsCreated && ConnectionApprovalData.PendingApprovals.Data.TryDequeue(out var pendingApproval))
{
StartCoroutine(GetPlayerInfo(pendingApproval));
}
}
}
}