Skip to content

Commit bd0a6c9

Browse files
wip - enable logout (#202)
## Purpose <!-- Describe the intention of the changes being proposed. What problem does it solve or functionality does it add? --> * ... ## Does this introduce a breaking change? <!-- Mark one with an "x". --> ``` [ ] Yes [ ] No ``` ## Pull Request Type What kind of change does this Pull Request introduce? <!-- Please check the one that applies to this PR using "x". --> ``` [ ] Bugfix [ ] Feature [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [ ] Documentation content changes [ ] Other... Please describe: ``` ## How to Test * Get the code ``` git clone [repo-address] cd [repo-name] git checkout [branch-name] npm install ``` * Test the code <!-- Add steps to run the tests suite and/or manually test --> ``` ``` ## What to Check Verify that the following are valid * ... ## Other Information <!-- Add any other helpful information that may be needed here. -->
1 parent 7e2f1c5 commit bd0a6c9

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

app/backend/Extensions/WebApplicationExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@ internal static WebApplication MapApi(this WebApplication app)
2323
// Get DALL-E image result from prompt
2424
api.MapPost("images", OnPostImagePromptAsync);
2525

26+
api.MapGet("enableLogout", OnGetEnableLogout);
27+
2628
return app;
2729
}
2830

31+
private static IResult OnGetEnableLogout(HttpContext context)
32+
{
33+
var header = context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"];
34+
var enableLogout = !string.IsNullOrEmpty(header);
35+
36+
return TypedResults.Ok(enableLogout);
37+
}
38+
2939
private static async IAsyncEnumerable<ChatChunkResponse> OnPostChatPromptAsync(
3040
PromptRequest prompt,
3141
OpenAIClient client,

app/backend/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@
7676

7777
app.UseHttpsRedirection();
7878
app.UseOutputCache();
79+
app.UseRouting();
80+
app.UseStaticFiles();
7981
app.UseCors();
8082
app.UseBlazorFrameworkFiles();
81-
app.UseStaticFiles();
82-
app.UseRouting();
8383
app.MapRazorPages();
8484
app.MapControllers();
8585
app.MapFallbackToFile("index.html");

app/frontend/Services/ApiClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public sealed class ApiClient
2020
return await response.Content.ReadFromJsonAsync<ImageResponse>();
2121
}
2222

23+
public async Task<bool> ShowLogoutButtonAsync()
24+
{
25+
var response = await _httpClient.GetAsync("api/enableLogout");
26+
response.EnsureSuccessStatusCode();
27+
28+
return await response.Content.ReadFromJsonAsync<bool>();
29+
}
30+
2331
public async Task<UploadDocumentsResponse> UploadDocumentsAsync(
2432
IReadOnlyList<IBrowserFile> files,
2533
long maxAllowedSize)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@inject NavigationManager Nav
2+
@inject ILogger<LogoutDisplay> Logger
3+
@inject ApiClient ApiClient
4+
5+
@if (ShowLogoutButton)
6+
{
7+
<MudIconButton Icon="@Icons.Material.Filled.Logout" Color="Color.Inherit" Size="Size.Large"
8+
Title="Logout of the app." OnClick="SignOut" />
9+
}
10+
11+
@code {
12+
[Parameter] public bool ShowLogoutButton { get; set; } = true;
13+
14+
protected override async Task OnInitializedAsync()
15+
{
16+
await base.OnInitializedAsync();
17+
try
18+
{
19+
Logger.LogInformation("start retrieve logout button visibility.");
20+
ShowLogoutButton = await ApiClient.ShowLogoutButtonAsync();
21+
}
22+
catch(Exception e)
23+
{
24+
Logger.LogError(e.Message);
25+
}
26+
}
27+
28+
private void SignOut()
29+
{
30+
Logger.LogInformation("User start logged out.");
31+
Nav.NavigateTo(".auth/logout", true);
32+
}
33+
}

app/frontend/Shared/MainLayout.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<MudIconButton Icon="@Icons.Custom.Brands.GitHub" Color="Color.Inherit" Size="Size.Large"
4242
Title="Visit the Azure Samples: GitHub repository for this app."
4343
Href="https://github.com/Azure-Samples/azure-search-openai-demo-csharp" Target="_blank" />
44+
<LogoutDisplay />
4445
</MudAppBar>
4546
<MudDrawer @bind-Open="_drawerOpen" Elevation="5" id="drawer">
4647
<MudDrawerHeader>
@@ -64,4 +65,4 @@
6465
<SettingsPanel @ref="_settingsPanel" @bind-Open="@_settingsOpen" />
6566
</MudMainContent>
6667
</MudLayout>
67-
</MudRTLProvider>
68+
</MudRTLProvider>

app/frontend/wwwroot/service-worker.published.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
self.importScripts('./service-worker-assets.js');
55
self.addEventListener('install', event => event.waitUntil(onInstall(event)));
66
self.addEventListener('activate', event => event.waitUntil(onActivate(event)));
7-
self.addEventListener('fetch', event => event.respondWith(onFetch(event)));
7+
self.addEventListener('fetch', () => { });
88

99
const cacheNamePrefix = 'offline-cache-';
1010
const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
@@ -30,19 +30,4 @@ async function onActivate(event) {
3030
await Promise.all(cacheKeys
3131
.filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName)
3232
.map(key => caches.delete(key)));
33-
}
34-
35-
async function onFetch(event) {
36-
let cachedResponse = null;
37-
if (event.request.method === 'GET') {
38-
// For all navigation requests, try to serve index.html from cache
39-
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
40-
const shouldServeIndexHtml = event.request.mode === 'navigate';
41-
42-
const request = shouldServeIndexHtml ? 'index.html' : event.request;
43-
const cache = await caches.open(cacheName);
44-
cachedResponse = await cache.match(request);
45-
}
46-
47-
return cachedResponse || fetch(event.request);
48-
}
33+
}

0 commit comments

Comments
 (0)