From 9284a8ddfd525a269df9075c1fcd956de305f3e9 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sat, 18 Nov 2023 14:04:51 +0100 Subject: [PATCH] added: base layout and authorization to razor components --- Components/App.razor | 2 ++ Components/Layout/MainLayout.razor | 10 +++++++++- Components/Partials/FooterPartial.razor | 8 ++++++++ Components/Partials/HeaderPartial.razor | 15 +++++++++++++++ Components/Partials/LoginPartial.razor | 9 +++++++++ Components/Routes.razor | 14 ++++++++------ Components/_imports.razor | 1 + JustShortIt.csproj | 1 + Program.cs | 6 +++++- 9 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 Components/Partials/FooterPartial.razor create mode 100644 Components/Partials/HeaderPartial.razor create mode 100644 Components/Partials/LoginPartial.razor diff --git a/Components/App.razor b/Components/App.razor index 79636e8..f43919a 100644 --- a/Components/App.razor +++ b/Components/App.razor @@ -18,7 +18,9 @@ + + diff --git a/Components/Layout/MainLayout.razor b/Components/Layout/MainLayout.razor index 53a4e95..5c1c644 100644 --- a/Components/Layout/MainLayout.razor +++ b/Components/Layout/MainLayout.razor @@ -1,3 +1,11 @@ @inherits LayoutComponentBase -@Body \ No newline at end of file +@using JustShortIt.Components.Partials + + + +
+ @Body +
+ + diff --git a/Components/Partials/FooterPartial.razor b/Components/Partials/FooterPartial.razor new file mode 100644 index 0000000..384b705 --- /dev/null +++ b/Components/Partials/FooterPartial.razor @@ -0,0 +1,8 @@ + diff --git a/Components/Partials/HeaderPartial.razor b/Components/Partials/HeaderPartial.razor new file mode 100644 index 0000000..87ac463 --- /dev/null +++ b/Components/Partials/HeaderPartial.razor @@ -0,0 +1,15 @@ +
+ +
diff --git a/Components/Partials/LoginPartial.razor b/Components/Partials/LoginPartial.razor new file mode 100644 index 0000000..4b5658c --- /dev/null +++ b/Components/Partials/LoginPartial.razor @@ -0,0 +1,9 @@ + + + @(context.User.Identity?.Name ?? "err_username_unknown") + Logout + + + Login + + diff --git a/Components/Routes.razor b/Components/Routes.razor index eb2c867..6d98bce 100644 --- a/Components/Routes.razor +++ b/Components/Routes.razor @@ -1,6 +1,8 @@ - - - - - - + + + + + + + + diff --git a/Components/_imports.razor b/Components/_imports.razor index d7fd106..b21210a 100644 --- a/Components/_imports.razor +++ b/Components/_imports.razor @@ -3,6 +3,7 @@ @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web +@using Microsoft.AspNetCore.Components.Authorization @using static Microsoft.AspNetCore.Components.Web.RenderMode @using Microsoft.AspNetCore.Components.Web.Virtualization @using Microsoft.JSInterop diff --git a/JustShortIt.csproj b/JustShortIt.csproj index 386847e..d84de90 100644 --- a/JustShortIt.csproj +++ b/JustShortIt.csproj @@ -9,6 +9,7 @@ + diff --git a/Program.cs b/Program.cs index 3b1bce1..f45d01d 100644 --- a/Program.cs +++ b/Program.cs @@ -2,6 +2,8 @@ using JustShortIt.Model; using JustShortIt.Service; using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.Server; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddEnvironmentVariables("JSI_"); @@ -43,7 +45,7 @@ } // Add Authentication -builder.Services.AddSingleton(_ => new AuthenticationService(user)); +builder.Services.AddScoped(_ => new AuthenticationService(user)); builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => { options.ExpireTimeSpan = TimeSpan.FromHours(24); options.SlidingExpiration = true; @@ -51,6 +53,8 @@ options.LoginPath = "/Login"; options.LogoutPath = "/Logout"; }); +builder.Services.AddScoped(); +builder.Services.AddCascadingAuthenticationState(); var app = builder.Build();