Skip to content

Commit

Permalink
wip - enable logout (#202)
Browse files Browse the repository at this point in the history
## 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. -->
  • Loading branch information
LittleLittleCloud authored Oct 23, 2023
1 parent 7e2f1c5 commit bd0a6c9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
10 changes: 10 additions & 0 deletions app/backend/Extensions/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ internal static WebApplication MapApi(this WebApplication app)
// Get DALL-E image result from prompt
api.MapPost("images", OnPostImagePromptAsync);

api.MapGet("enableLogout", OnGetEnableLogout);

return app;
}

private static IResult OnGetEnableLogout(HttpContext context)
{
var header = context.Request.Headers["X-MS-CLIENT-PRINCIPAL-ID"];
var enableLogout = !string.IsNullOrEmpty(header);

return TypedResults.Ok(enableLogout);
}

private static async IAsyncEnumerable<ChatChunkResponse> OnPostChatPromptAsync(
PromptRequest prompt,
OpenAIClient client,
Expand Down
4 changes: 2 additions & 2 deletions app/backend/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@

app.UseHttpsRedirection();
app.UseOutputCache();
app.UseRouting();
app.UseStaticFiles();
app.UseCors();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
Expand Down
8 changes: 8 additions & 0 deletions app/frontend/Services/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public sealed class ApiClient
return await response.Content.ReadFromJsonAsync<ImageResponse>();
}

public async Task<bool> ShowLogoutButtonAsync()
{
var response = await _httpClient.GetAsync("api/enableLogout");
response.EnsureSuccessStatusCode();

return await response.Content.ReadFromJsonAsync<bool>();
}

public async Task<UploadDocumentsResponse> UploadDocumentsAsync(
IReadOnlyList<IBrowserFile> files,
long maxAllowedSize)
Expand Down
33 changes: 33 additions & 0 deletions app/frontend/Shared/LogoutDisplay.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@inject NavigationManager Nav
@inject ILogger<LogoutDisplay> Logger
@inject ApiClient ApiClient

@if (ShowLogoutButton)
{
<MudIconButton Icon="@Icons.Material.Filled.Logout" Color="Color.Inherit" Size="Size.Large"
Title="Logout of the app." OnClick="SignOut" />
}

@code {
[Parameter] public bool ShowLogoutButton { get; set; } = true;

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
try
{
Logger.LogInformation("start retrieve logout button visibility.");
ShowLogoutButton = await ApiClient.ShowLogoutButtonAsync();
}
catch(Exception e)
{
Logger.LogError(e.Message);
}
}

private void SignOut()
{
Logger.LogInformation("User start logged out.");
Nav.NavigateTo(".auth/logout", true);
}
}
3 changes: 2 additions & 1 deletion app/frontend/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<MudIconButton Icon="@Icons.Custom.Brands.GitHub" Color="Color.Inherit" Size="Size.Large"
Title="Visit the Azure Samples: GitHub repository for this app."
Href="https://github.com/Azure-Samples/azure-search-openai-demo-csharp" Target="_blank" />
<LogoutDisplay />
</MudAppBar>
<MudDrawer @bind-Open="_drawerOpen" Elevation="5" id="drawer">
<MudDrawerHeader>
Expand All @@ -64,4 +65,4 @@
<SettingsPanel @ref="_settingsPanel" @bind-Open="@_settingsOpen" />
</MudMainContent>
</MudLayout>
</MudRTLProvider>
</MudRTLProvider>
19 changes: 2 additions & 17 deletions app/frontend/wwwroot/service-worker.published.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
self.importScripts('./service-worker-assets.js');
self.addEventListener('install', event => event.waitUntil(onInstall(event)));
self.addEventListener('activate', event => event.waitUntil(onActivate(event)));
self.addEventListener('fetch', event => event.respondWith(onFetch(event)));
self.addEventListener('fetch', () => { });

const cacheNamePrefix = 'offline-cache-';
const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
Expand All @@ -30,19 +30,4 @@ async function onActivate(event) {
await Promise.all(cacheKeys
.filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName)
.map(key => caches.delete(key)));
}

async function onFetch(event) {
let cachedResponse = null;
if (event.request.method === 'GET') {
// For all navigation requests, try to serve index.html from cache
// If you need some URLs to be server-rendered, edit the following check to exclude those URLs
const shouldServeIndexHtml = event.request.mode === 'navigate';

const request = shouldServeIndexHtml ? 'index.html' : event.request;
const cache = await caches.open(cacheName);
cachedResponse = await cache.match(request);
}

return cachedResponse || fetch(event.request);
}
}

0 comments on commit bd0a6c9

Please sign in to comment.