Skip to content

Commit 7b90af6

Browse files
authored
Feat/update state manager (#778)
* feat(TeamStateManager): add support for WASM and Server modes - Implemented ITeamStateManager interface for managing team states. - Added registration logic for team state manager in ServiceCollectionExtensions.cs. - Created ServerTeamStateManager and WasmTeamStateManager to handle team state management in respective environments. - Updated User.razor to utilize the new team state manager for fetching user team information. * feat:update WasmTeamStateManager refresh token * fix:navigate page * fix(WasmTeamStateManager): update token refresh logging Updated the log message for token refresh failure and removed the null check for newToken. The logic now directly navigates to the page refresh on token refresh failure, simplifying the code. * feat: Remove team state retrieval methods Removed the methods for retrieving and clearing the current team ID from the ITeamStateManager interface and its implementations in ServerTeamStateManager and WasmTeamStateManager. The User.razor file was updated to directly use MasaUser.CurrentTeamId instead of relying on the authentication state for the current team ID.
1 parent 4378062 commit 7b90af6

File tree

4 files changed

+3
-83
lines changed

4 files changed

+3
-83
lines changed

src/Masa.Stack.Components/Infrastructure/Identity/ITeamStateManager.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,4 @@ public interface ITeamStateManager
1111
/// <param name="teamId">团队ID</param>
1212
/// <returns></returns>
1313
Task SetCurrentTeamAsync(Guid teamId);
14-
15-
/// <summary>
16-
/// 获取当前团队ID
17-
/// </summary>
18-
/// <returns>团队ID,如果没有设置则返回 Guid.Empty</returns>
19-
Task<Guid> GetCurrentTeamAsync();
20-
21-
/// <summary>
22-
/// 清除团队状态
23-
/// </summary>
24-
/// <returns></returns>
25-
Task ClearTeamStateAsync();
26-
}
14+
}

src/Masa.Stack.Components/Infrastructure/Identity/ServerTeamStateManager.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,4 @@ public async Task SetCurrentTeamAsync(Guid teamId)
2424
{
2525
await _authenticationStateManager.UpsertClaimAsync(IdentityClaimConsts.CURRENT_TEAM, teamId.ToString());
2626
}
27-
28-
/// <summary>
29-
/// 从身份验证状态中获取当前团队ID
30-
/// </summary>
31-
public async Task<Guid> GetCurrentTeamAsync()
32-
{
33-
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
34-
var teamIdClaim = authState.User.FindFirst(IdentityClaimConsts.CURRENT_TEAM);
35-
36-
if (teamIdClaim != null && Guid.TryParse(teamIdClaim.Value, out var teamId))
37-
{
38-
return teamId;
39-
}
40-
41-
return Guid.Empty;
42-
}
43-
44-
/// <summary>
45-
/// 清除团队状态
46-
/// </summary>
47-
public async Task ClearTeamStateAsync()
48-
{
49-
await _authenticationStateManager.UpsertClaimAsync(IdentityClaimConsts.CURRENT_TEAM, Guid.Empty.ToString());
50-
}
51-
}
27+
}

src/Masa.Stack.Components/Infrastructure/Identity/WasmTeamStateManager.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,4 @@ public async Task SetCurrentTeamAsync(Guid teamId)
7777
_logger.LogError(ex, "切换团队时发生错误,团队ID: {TeamId}", teamId);
7878
}
7979
}
80-
81-
/// <summary>
82-
/// 从当前身份验证状态中获取团队ID
83-
/// </summary>
84-
public async Task<Guid> GetCurrentTeamAsync()
85-
{
86-
try
87-
{
88-
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
89-
var teamIdClaim = authState.User.FindFirst(IdentityClaimConsts.CURRENT_TEAM);
90-
91-
if (teamIdClaim != null && Guid.TryParse(teamIdClaim.Value, out var teamId))
92-
{
93-
return teamId;
94-
}
95-
}
96-
catch (Exception ex)
97-
{
98-
_logger.LogError(ex, "获取当前团队ID时发生错误");
99-
}
100-
101-
return Guid.Empty;
102-
}
103-
104-
/// <summary>
105-
/// 清除团队状态
106-
/// </summary>
107-
public async Task ClearTeamStateAsync()
108-
{
109-
await SetCurrentTeamAsync(Guid.Empty);
110-
}
11180
}

src/Masa.Stack.Components/Shared/Layouts/Components/User.razor

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,11 @@
150150
{
151151
_teams = await AuthClient.TeamService.GetUserTeamsAsync();
152152

153-
// 从身份验证状态中获取当前团队信息(在 WASM 模式下来自 token)
154-
var currentTeamId = await TeamStateManager.GetCurrentTeamAsync();
155-
if (currentTeamId != Guid.Empty && _teams.Any(t => t.Id == currentTeamId))
156-
{
157-
// 如果从身份验证状态中找到有效的团队信息,使用它
158-
MasaUser.CurrentTeamId = currentTeamId;
159-
GlobalConfig.CurrentTeamId = currentTeamId;
160-
}
161-
else if (MasaUser.CurrentTeamId == Guid.Empty && _teams.Any())
153+
if (MasaUser.CurrentTeamId == Guid.Empty && _teams.Any())
162154
{
163155
// 如果没有团队信息,则使用第一个团队
164156
await CurrentTeamChanged(_teams.First().Id);
165157
}
166-
else if (MasaUser.CurrentTeamId != Guid.Empty && _teams.Any(t => t.Id == MasaUser.CurrentTeamId))
167-
{
168-
// 如果 MasaUser 中有有效的团队信息,使用它
169-
GlobalConfig.CurrentTeamId = MasaUser.CurrentTeamId;
170-
}
171158
}
172159
catch (Exception e)
173160
{

0 commit comments

Comments
 (0)