Skip to content

Commit 5ebafc5

Browse files
authored
fix: relogin when auth token is stale [MTT-3253] (#604)
* Added relogin functionality It is triggered before any of the UI's do lobby api calls. * AuthServiceFacade now has EnsurePlayerIsAuthorized that returns bools lobby ui adjusted accrodingly, to handle the failures to re-authorize and to not do further calls if such failure occurs
1 parent ae785df commit 5ebafc5

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

Assets/BossRoom/Scripts/Client/Game/State/ClientMainMenuState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServi
6363
unityAuthenticationInitOptions.SetProfile(profile);
6464
}
6565

66-
await authServiceFacade.SignInAsync(unityAuthenticationInitOptions);
66+
await authServiceFacade.InitializeAndSignInAsync(unityAuthenticationInitOptions);
6767
OnAuthSignIn();
6868
}
6969
catch (Exception)

Assets/BossRoom/Scripts/Client/UI/Lobby/LobbyUIMediator.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
23
using TMPro;
34
using Unity.Multiplayer.Samples.BossRoom.Client;
45
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
@@ -21,6 +22,7 @@ public class LobbyUIMediator : MonoBehaviour
2122
[SerializeField] TextMeshProUGUI m_PlayerNameLabel;
2223
[SerializeField] GameObject m_LoadingSpinner;
2324

25+
AuthenticationServiceFacade m_AuthenticationServiceFacade;
2426
LobbyServiceFacade m_LobbyServiceFacade;
2527
LocalLobbyUser m_LocalUser;
2628
LocalLobby m_LocalLobby;
@@ -32,6 +34,7 @@ public class LobbyUIMediator : MonoBehaviour
3234

3335
[Inject]
3436
void InjectDependenciesAndInitialize(
37+
AuthenticationServiceFacade authenticationServiceFacade,
3538
LobbyServiceFacade lobbyServiceFacade,
3639
LocalLobbyUser localUser,
3740
LocalLobby localLobby,
@@ -40,6 +43,7 @@ void InjectDependenciesAndInitialize(
4043
ClientGameNetPortal clientGameNetPortal
4144
)
4245
{
46+
m_AuthenticationServiceFacade = authenticationServiceFacade;
4347
m_NameGenerationData = nameGenerationData;
4448
m_LocalUser = localUser;
4549
m_LobbyServiceFacade = lobbyServiceFacade;
@@ -72,6 +76,14 @@ public async void CreateLobbyRequest(string lobbyName, bool isPrivate, int maxPl
7276

7377
BlockUIWhileLoadingIsInProgress();
7478

79+
bool playerIsAuthorized = await m_AuthenticationServiceFacade.EnsurePlayerIsAuthorized();
80+
81+
if (!playerIsAuthorized)
82+
{
83+
UnblockUIAfterLoadingIsComplete();
84+
return;
85+
}
86+
7587
var lobbyCreationAttempt = await m_LobbyServiceFacade.TryCreateLobbyAsync(lobbyName, maxPlayers, isPrivate);
7688

7789
if (lobbyCreationAttempt.Success)
@@ -102,6 +114,14 @@ public async void QueryLobbiesRequest(bool blockUI)
102114
BlockUIWhileLoadingIsInProgress();
103115
}
104116

117+
bool playerIsAuthorized = await m_AuthenticationServiceFacade.EnsurePlayerIsAuthorized();
118+
119+
if (!playerIsAuthorized)
120+
{
121+
UnblockUIAfterLoadingIsComplete();
122+
return;
123+
}
124+
105125
await m_LobbyServiceFacade.RetrieveAndPublishLobbyListAsync();
106126
UnblockUIAfterLoadingIsComplete();
107127
}
@@ -110,6 +130,14 @@ public async void JoinLobbyWithCodeRequest(string lobbyCode)
110130
{
111131
BlockUIWhileLoadingIsInProgress();
112132

133+
bool playerIsAuthorized = await m_AuthenticationServiceFacade.EnsurePlayerIsAuthorized();
134+
135+
if (!playerIsAuthorized)
136+
{
137+
UnblockUIAfterLoadingIsComplete();
138+
return;
139+
}
140+
113141
var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(null, lobbyCode);
114142

115143
if (result.Success)
@@ -126,6 +154,14 @@ public async void JoinLobbyRequest(LocalLobby lobby)
126154
{
127155
BlockUIWhileLoadingIsInProgress();
128156

157+
bool playerIsAuthorized = await m_AuthenticationServiceFacade.EnsurePlayerIsAuthorized();
158+
159+
if (!playerIsAuthorized)
160+
{
161+
UnblockUIAfterLoadingIsComplete();
162+
return;
163+
}
164+
129165
var result = await m_LobbyServiceFacade.TryJoinLobbyAsync(lobby.LobbyID, lobby.LobbyCode);
130166

131167
if (result.Success)
@@ -142,6 +178,14 @@ public async void QuickJoinRequest()
142178
{
143179
BlockUIWhileLoadingIsInProgress();
144180

181+
bool playerIsAuthorized = await m_AuthenticationServiceFacade.EnsurePlayerIsAuthorized();
182+
183+
if (!playerIsAuthorized)
184+
{
185+
UnblockUIAfterLoadingIsComplete();
186+
return;
187+
}
188+
145189
var result = await m_LobbyServiceFacade.TryQuickJoinLobbyAsync();
146190

147191
if (result.Success)

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Auth/AuthenticationServiceFacade.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void InjectDependencies(IPublisher<UnityServiceErrorMessage> unityServiceErrorMe
1818
m_UnityServiceErrorMessagePublisher = unityServiceErrorMessagePublisher;
1919
}
2020

21-
public async Task SignInAsync(InitializationOptions initializationOptions)
21+
public async Task InitializeAndSignInAsync(InitializationOptions initializationOptions)
2222
{
2323
try
2424
{
@@ -35,8 +35,35 @@ public async Task SignInAsync(InitializationOptions initializationOptions)
3535
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
3636
throw;
3737
}
38+
}
3839

40+
public async Task<bool> EnsurePlayerIsAuthorized()
41+
{
42+
if (AuthenticationService.Instance.IsAuthorized)
43+
{
44+
return true;
45+
}
3946

47+
try
48+
{
49+
await AuthenticationService.Instance.SignInAnonymouslyAsync();
50+
return true;
51+
}
52+
catch (AuthenticationException e)
53+
{
54+
var reason = $"{e.Message} ({e.InnerException?.Message})";
55+
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
56+
//not rethrowing for authentication exceptions - any failure to authenticate is considered "handled failure"
57+
return false;
58+
}
59+
catch (Exception e)
60+
{
61+
//all other exceptions should still bubble up as unhandled ones
62+
var reason = $"{e.Message} ({e.InnerException?.Message})";
63+
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
64+
throw;
65+
}
4066
}
67+
4168
}
4269
}

0 commit comments

Comments
 (0)