Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip - enable logout #202

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"];
LittleLittleCloud marked this conversation as resolved.
Show resolved Hide resolved
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);
LittleLittleCloud marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
}
}