Skip to content

Commit e2ee7b9

Browse files
committed
Finalize Blazor Server Sample
1 parent 25a0c7c commit e2ee7b9

25 files changed

Lines changed: 551 additions & 143 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
@using CodeBeam.UltimateAuth.Credentials.Contracts
2+
@using CodeBeam.UltimateAuth.Users.Contracts
3+
@inject IUAuthClient UAuthClient
4+
@inject ISnackbar Snackbar
5+
6+
<MudDialog>
7+
<TitleContent>
8+
<MudText Typo="Typo.h6">Create User</MudText>
9+
</TitleContent>
10+
11+
<DialogContent>
12+
<MudForm @ref="_form">
13+
<MudStack Spacing="2">
14+
<MudTextField @bind-Value="_username" Label="Username" Variant="Variant.Outlined" Required="true" />
15+
<MudTextField @bind-Value="_email" Label="Email" Variant="Variant.Outlined" Required="true" />
16+
<MudPasswordField @bind-Value="_password" Label="Password" Variant="Variant.Outlined" Required="true" />
17+
<MudPasswordField @bind-Value="_passwordCheck" Label="Password (Again)" Variant="Variant.Outlined" Required="true" Validation="@(() => PasswordMatch(_password))" />
18+
<MudTextField @bind-Value="_displayName" Label="Display Name" Variant="Variant.Outlined" />
19+
</MudStack>
20+
</MudForm>
21+
</DialogContent>
22+
23+
<DialogActions>
24+
<MudButton OnClick="Cancel">Cancel</MudButton>
25+
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="CreateUserAsync">Create</MudButton>
26+
</DialogActions>
27+
</MudDialog>
28+
29+
@code {
30+
private MudForm _form = null!;
31+
private string? _username;
32+
private string? _email;
33+
private string? _password;
34+
private string? _passwordCheck;
35+
private string? _displayName;
36+
37+
[CascadingParameter]
38+
private IMudDialogInstance MudDialog { get; set; } = default!;
39+
40+
private async Task CreateUserAsync()
41+
{
42+
await _form.Validate();
43+
44+
if (!_form.IsValid)
45+
return;
46+
47+
if (_password != _passwordCheck)
48+
{
49+
Snackbar.Add("Passwords do not match", Severity.Error);
50+
return;
51+
}
52+
53+
var request = new CreateUserRequest
54+
{
55+
UserName = _username,
56+
Email = _email,
57+
DisplayName = _displayName,
58+
Password = _password
59+
};
60+
61+
var result = await UAuthClient.Users.CreateAdminAsync(request);
62+
63+
if (!result.IsSuccess)
64+
{
65+
Snackbar.Add(result.GetErrorText ?? "User creation failed", Severity.Error);
66+
return;
67+
}
68+
69+
Snackbar.Add("User created successfully", Severity.Success);
70+
MudDialog.Close(DialogResult.Ok(true));
71+
}
72+
73+
private string PasswordMatch(string? arg) => _password != arg ? "Passwords don't match" : string.Empty;
74+
75+
private void Cancel() => MudDialog.Cancel();
76+
}

‎samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Dialogs/IdentifierDialog.razor‎

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
<MudDialog Class="mud-width-full" ContentClass="uauth-dialog">
88
<TitleContent>
99
<MudText>Identifier Management</MudText>
10-
<MudText Typo="Typo.subtitle2" Color="Color.Primary">User: @AuthState?.Identity?.DisplayName</MudText>
10+
<MudText Typo="Typo.subtitle2" Color="Color.Primary">
11+
@if (UserKey is null)
12+
{
13+
<text>User: @AuthState?.Identity?.DisplayName</text>
14+
}
15+
else
16+
{
17+
<text>UserKey: @UserKey.Value</text>
18+
}
19+
</MudText>
1120
</TitleContent>
1221
<DialogContent>
1322
<MudDataGrid @ref="@_grid" T="UserIdentifierDto" ServerData="LoadServerData" Hover="true" Bordered="true" Striped="true" Dense="true" Elevation="0"
@@ -23,33 +32,41 @@
2332
<PropertyColumn Property="x => x.Id" Title="Id" Editable="false" />
2433
<PropertyColumn Property="x => x.Type" Title="Type" Editable="false" />
2534
<PropertyColumn Property="x => x.Value" Title="Value" />
26-
<PropertyColumn Property="x => x.IsPrimary" Title="Primary" Editable="false" />
27-
<PropertyColumn Property="x => x.IsVerified" Title="Verified" Editable="false" />
35+
<PropertyColumn Property="x => x.IsPrimary" Title="Primary" Editable="false">
36+
<CellTemplate>
37+
<MudIcon Size="Size.Small" Icon="@(context.Item.IsPrimary? Icons.Material.Filled.Done : Icons.Material.Filled.Close)" />
38+
</CellTemplate>
39+
</PropertyColumn>
40+
<PropertyColumn Property="x => x.IsVerified" Title="Verified" Editable="false">
41+
<CellTemplate>
42+
<MudIcon Size="Size.Small" Icon="@(context.Item.IsVerified? Icons.Material.Filled.Done : Icons.Material.Filled.Close)" />
43+
</CellTemplate>
44+
</PropertyColumn>
2845
<TemplateColumn Title="Actions" Editable="false">
2946
<CellTemplate>
3047
<MudStack Style="min-width: 120px" Row="true" Spacing="2" Wrap="Wrap.Wrap">
3148
<MudTooltip Text="Edit" Color="Color.Primary" Delay="300" ShowOnFocus="false">
32-
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Edit" OnClick="@context.Actions.StartEditingItemAsync" />
49+
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@context.Actions.StartEditingItemAsync" />
3350
</MudTooltip>
3451
@if (context.Item.IsPrimary)
3552
{
3653
<MudTooltip Text="Unset Primary" Color="Color.Primary" Delay="300" ShowOnFocus="false">
37-
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.KeyOff" OnClick="@(() => UnsetPrimaryAsync(context.Item.Id))" />
54+
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.KeyOff" Size="Size.Small" OnClick="@(() => UnsetPrimaryAsync(context.Item.Id))" />
3855
</MudTooltip>
3956
}
4057
else
4158
{
4259
<MudTooltip Text="Set Primary" Color="Color.Primary" Delay="300" ShowOnFocus="false">
43-
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Key" OnClick="@(() => SetPrimaryAsync(context.Item.Id))" />
60+
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Key" Size="Size.Small" OnClick="@(() => SetPrimaryAsync(context.Item.Id))" />
4461
</MudTooltip>
4562
}
4663

4764
<MudTooltip Text="Verify" Color="Color.Primary" Delay="300" ShowOnFocus="false">
48-
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Verified" OnClick="@(() => VerifyAsync(context.Item.Id))" />
65+
<MudIconButton Color="Color.Primary" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Verified" Size="Size.Small" OnClick="@(() => VerifyAsync(context.Item.Id))" />
4966
</MudTooltip>
5067

5168
<MudTooltip Text="Delete" Color="Color.Error" Delay="300" ShowOnFocus="false">
52-
<MudIconButton Color="Color.Error" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Delete" OnClick="@(() => DeleteIdentifier(context.Item.Id))" />
69+
<MudIconButton Color="Color.Error" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Delete" Size="Size.Small" OnClick="@(() => DeleteIdentifier(context.Item.Id))" />
5370
</MudTooltip>
5471
</MudStack>
5572
</CellTemplate>
@@ -85,7 +102,6 @@
85102

86103
<DialogActions>
87104
<MudButton OnClick="Cancel">Cancel</MudButton>
88-
<MudButton Color="Color.Primary" OnClick="Submit">OK</MudButton>
89105
</DialogActions>
90106
</MudDialog>
91107

@@ -103,6 +119,9 @@
103119
[Parameter]
104120
public UAuthState AuthState { get; set; } = default!;
105121

122+
[Parameter]
123+
public UserKey? UserKey { get; set; }
124+
106125
protected override async Task OnAfterRenderAsync(bool firstRender)
107126
{
108127
await base.OnAfterRenderAsync(firstRender);
@@ -130,7 +149,16 @@
130149
Descending = sort?.Descending ?? false
131150
};
132151

133-
var res = await UAuthClient.Identifiers.GetMyIdentifiersAsync(req);
152+
UAuthResult<PagedResult<UserIdentifierDto>> res;
153+
154+
if (UserKey is null)
155+
{
156+
res = await UAuthClient.Identifiers.GetMyIdentifiersAsync(req);
157+
}
158+
else
159+
{
160+
res = await UAuthClient.Identifiers.GetUserIdentifiersAsync(UserKey.Value, req);
161+
}
134162

135163
if (!res.IsSuccess || res.Value is null)
136164
{
@@ -190,7 +218,18 @@
190218
Id = item.Id,
191219
NewValue = item.Value
192220
};
193-
var result = await UAuthClient.Identifiers.UpdateSelfAsync(updateRequest);
221+
222+
UAuthResult result;
223+
224+
if (UserKey is null)
225+
{
226+
result = await UAuthClient.Identifiers.UpdateSelfAsync(updateRequest);
227+
}
228+
else
229+
{
230+
result = await UAuthClient.Identifiers.UpdateAdminAsync(UserKey.Value, updateRequest);
231+
}
232+
194233
if (result.IsSuccess)
195234
{
196235
Snackbar.Add("Identifier updated successfully", Severity.Success);
@@ -219,7 +258,17 @@
219258
IsPrimary = _newIdentifierPrimary
220259
};
221260

222-
var result = await UAuthClient.Identifiers.AddSelfAsync(request);
261+
UAuthResult result;
262+
263+
if (UserKey is null)
264+
{
265+
result = await UAuthClient.Identifiers.AddSelfAsync(request);
266+
}
267+
else
268+
{
269+
result = await UAuthClient.Identifiers.AddAdminAsync(UserKey.Value, request);
270+
}
271+
223272
if (result.IsSuccess)
224273
{
225274
Snackbar.Add("Identifier added successfully", Severity.Success);
@@ -252,7 +301,17 @@
252301
}
253302

254303
VerifyUserIdentifierRequest request = new() { IdentifierId = id };
255-
var result = await UAuthClient.Identifiers.VerifySelfAsync(request);
304+
UAuthResult result;
305+
306+
if (UserKey is null)
307+
{
308+
result = await UAuthClient.Identifiers.VerifySelfAsync(request);
309+
}
310+
else
311+
{
312+
result = await UAuthClient.Identifiers.VerifyAdminAsync(UserKey.Value, request);
313+
}
314+
256315
if (result.IsSuccess)
257316
{
258317
Snackbar.Add("Identifier verified successfully", Severity.Success);
@@ -268,7 +327,17 @@
268327
private async Task SetPrimaryAsync(Guid id)
269328
{
270329
SetPrimaryUserIdentifierRequest request = new() { IdentifierId = id };
271-
var result = await UAuthClient.Identifiers.SetPrimarySelfAsync(request);
330+
UAuthResult result;
331+
332+
if (UserKey is null)
333+
{
334+
result = await UAuthClient.Identifiers.SetPrimarySelfAsync(request);
335+
}
336+
else
337+
{
338+
result = await UAuthClient.Identifiers.SetPrimaryAdminAsync(UserKey.Value, request);
339+
}
340+
272341
if (result.IsSuccess)
273342
{
274343
Snackbar.Add("Primary identifier set successfully", Severity.Success);
@@ -284,7 +353,17 @@
284353
private async Task UnsetPrimaryAsync(Guid id)
285354
{
286355
UnsetPrimaryUserIdentifierRequest request = new() { IdentifierId = id };
287-
var result = await UAuthClient.Identifiers.UnsetPrimarySelfAsync(request);
356+
UAuthResult result;
357+
358+
if (UserKey is null)
359+
{
360+
result = await UAuthClient.Identifiers.UnsetPrimarySelfAsync(request);
361+
}
362+
else
363+
{
364+
result = await UAuthClient.Identifiers.UnsetPrimaryAdminAsync(UserKey.Value, request);
365+
}
366+
288367
if (result.IsSuccess)
289368
{
290369
Snackbar.Add("Primary identifier unset successfully", Severity.Success);
@@ -300,7 +379,17 @@
300379
private async Task DeleteIdentifier(Guid id)
301380
{
302381
DeleteUserIdentifierRequest request = new() { IdentifierId = id };
303-
var result = await UAuthClient.Identifiers.DeleteSelfAsync(request);
382+
UAuthResult result;
383+
384+
if (UserKey is null)
385+
{
386+
result = await UAuthClient.Identifiers.DeleteSelfAsync(request);
387+
}
388+
else
389+
{
390+
result = await UAuthClient.Identifiers.DeleteAdminAsync(UserKey.Value, request);
391+
}
392+
304393
if (result.IsSuccess)
305394
{
306395
Snackbar.Add("Identifier deleted successfully", Severity.Success);
@@ -313,7 +402,5 @@
313402
}
314403
}
315404

316-
private void Submit() => MudDialog.Close(DialogResult.Ok(true));
317-
318405
private void Cancel() => MudDialog.Cancel();
319406
}

‎samples/blazor-server/CodeBeam.UltimateAuth.Sample.BlazorServer/Components/Dialogs/ProfileDialog.razor‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
<MudDialog Class="mud-width-full" ContentClass="uauth-dialog">
88
<TitleContent>
99
<MudText>Identifier Management</MudText>
10-
<MudText Typo="Typo.subtitle2" Color="Color.Primary">User: @AuthState?.Identity?.DisplayName</MudText>
10+
<MudText Typo="Typo.subtitle2" Color="Color.Primary">
11+
@if (UserKey is null)
12+
{
13+
<text>User: @AuthState?.Identity?.DisplayName</text>
14+
}
15+
else
16+
{
17+
<text>UserKey: @UserKey.Value</text>
18+
}
19+
</MudText>
1120
</TitleContent>
1221
<DialogContent>
1322
<MudForm @ref="_form">
@@ -85,17 +94,13 @@
8594
</MudDialog>
8695

8796
@code {
88-
8997
private MudForm? _form;
90-
9198
private string? _firstName;
9299
private string? _lastName;
93100
private string? _displayName;
94-
95101
private DateTime? _birthDate;
96102
private string? _gender;
97103
private string? _bio;
98-
99104
private string? _language;
100105
private string? _timeZone;
101106
private string? _culture;
@@ -106,9 +111,21 @@
106111
[Parameter]
107112
public UAuthState AuthState { get; set; } = default!;
108113

114+
[Parameter]
115+
public UserKey? UserKey { get; set; }
116+
109117
protected override async Task OnInitializedAsync()
110118
{
111-
var result = await UAuthClient.Users.GetMeAsync();
119+
UAuthResult<UserView> result;
120+
121+
if (UserKey is null)
122+
{
123+
result = await UAuthClient.Users.GetMeAsync();
124+
}
125+
else
126+
{
127+
result = await UAuthClient.Users.GetProfileAsync(UserKey.Value);
128+
}
112129

113130
if (result.IsSuccess && result.Value is not null)
114131
{
@@ -156,7 +173,16 @@
156173
Culture = _culture
157174
};
158175

159-
var result = await UAuthClient.Users.UpdateMeAsync(request);
176+
UAuthResult result;
177+
178+
if (UserKey is null)
179+
{
180+
result = await UAuthClient.Users.UpdateMeAsync(request);
181+
}
182+
else
183+
{
184+
result = await UAuthClient.Users.UpdateProfileAsync(UserKey.Value, request);
185+
}
160186

161187
if (result.IsSuccess)
162188
{
@@ -165,10 +191,9 @@
165191
}
166192
else
167193
{
168-
Snackbar.Add(result?.Problem?.Detail ?? result?.Problem?.Title ?? "Failed to update profile", Severity.Error);
194+
Snackbar.Add(result.GetErrorText ?? "Failed to update profile", Severity.Error);
169195
}
170196
}
171197

172198
private void Cancel() => MudDialog.Cancel();
173-
174199
}

0 commit comments

Comments
 (0)