| | | 1 | | @using Microsoft.AspNetCore.Components.Authorization |
| | | 2 | | @using Microsoft.AspNetCore.Components.Routing |
| | | 3 | | @using Microsoft.AspNetCore.Components.Rendering |
| | | 4 | | @using Microsoft.AspNetCore.Authorization |
| | | 5 | | |
| | | 6 | | @* |
| | | 7 | | ChronicisRouteView - Custom route view that prevents layout flicker |
| | | 8 | | |
| | | 9 | | This component intelligently determines which layout to use based on: |
| | | 10 | | 1. Explicit @layout directive on the page (public pages with PublicLayout) |
| | | 11 | | 2. [Authorize] attribute (authenticated pages get AuthenticatedLayout) |
| | | 12 | | 3. Default to PublicLayout for pages without explicit layout or auth |
| | | 13 | | |
| | | 14 | | This prevents the "flash" of AuthenticatedLayout on public pages. |
| | | 15 | | |
| | | 16 | | It also provides a styled "Authorizing" screen instead of the default |
| | | 17 | | unstyled "Authorizing..." text. |
| | | 18 | | *@ |
| | | 19 | | |
| | | 20 | | <CascadingAuthenticationState> |
| | | 21 | | @RenderPageWithLayout() |
| | | 22 | | </CascadingAuthenticationState> |
| | | 23 | | |
| | | 24 | | @code { |
| | | 25 | | [Parameter] |
| | | 26 | | public RouteData RouteData { get; set; } = default!; |
| | | 27 | | |
| | 4 | 28 | | private RenderFragment RenderPageWithLayout() => builder => |
| | 4 | 29 | | { |
| | 4 | 30 | | var pageType = RouteData.PageType; |
| | 4 | 31 | | var layoutType = DetermineLayoutType(pageType); |
| | 4 | 32 | | var requiresAuth = pageType.GetCustomAttributes(typeof(AuthorizeAttribute), true).Any(); |
| | 4 | 33 | | |
| | 4 | 34 | | // DetermineLayoutType always returns a concrete layout. |
| | 4 | 35 | | builder.OpenComponent<LayoutView>(0); |
| | 4 | 36 | | builder.AddAttribute(1, "Layout", layoutType); |
| | 4 | 37 | | builder.AddAttribute(2, "ChildContent", (RenderFragment)(contentBuilder => |
| | 4 | 38 | | { |
| | 4 | 39 | | if (requiresAuth) |
| | 4 | 40 | | { |
| | 4 | 41 | | // Use AuthorizeRouteView for pages with [Authorize] |
| | 4 | 42 | | contentBuilder.OpenComponent<AuthorizeRouteView>(0); |
| | 4 | 43 | | contentBuilder.AddAttribute(1, "RouteData", RouteData); |
| | 4 | 44 | | contentBuilder.AddAttribute(2, "Authorizing", (RenderFragment)(authorizingBuilder => |
| | 4 | 45 | | { |
| | 4 | 46 | | authorizingBuilder.OpenComponent<AuthorizingScreen>(0); |
| | 4 | 47 | | authorizingBuilder.CloseComponent(); |
| | 4 | 48 | | })); |
| | 4 | 49 | | contentBuilder.AddAttribute(3, "NotAuthorized", (RenderFragment<AuthenticationState>)(authState => |
| | 4 | 50 | | { |
| | 4 | 51 | | return notAuthBuilder => |
| | 4 | 52 | | { |
| | 4 | 53 | | notAuthBuilder.OpenComponent<RedirectToLogin>(0); |
| | 4 | 54 | | notAuthBuilder.CloseComponent(); |
| | 4 | 55 | | }; |
| | 4 | 56 | | })); |
| | 4 | 57 | | contentBuilder.CloseComponent(); |
| | 4 | 58 | | } |
| | 4 | 59 | | else |
| | 4 | 60 | | { |
| | 4 | 61 | | // Render public page directly |
| | 4 | 62 | | RenderPageComponent(contentBuilder, pageType, RouteData); |
| | 4 | 63 | | } |
| | 4 | 64 | | })); |
| | 4 | 65 | | builder.CloseComponent(); |
| | 4 | 66 | | }; |
| | | 67 | | |
| | | 68 | | private void RenderPageComponent(RenderTreeBuilder builder, Type pageType, RouteData routeData) |
| | | 69 | | { |
| | 2 | 70 | | builder.OpenComponent(0, pageType); |
| | 6 | 71 | | foreach (var kvp in routeData.RouteValues) |
| | | 72 | | { |
| | 1 | 73 | | builder.AddAttribute(1, kvp.Key, kvp.Value); |
| | | 74 | | } |
| | 2 | 75 | | builder.CloseComponent(); |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | private Type DetermineLayoutType(Type pageType) |
| | | 79 | | { |
| | | 80 | | // Check if page has explicit @layout directive |
| | 7 | 81 | | var layoutAttribute = pageType.GetCustomAttributes(typeof(LayoutAttribute), true) |
| | 7 | 82 | | .OfType<LayoutAttribute>() |
| | 7 | 83 | | .FirstOrDefault(); |
| | | 84 | | |
| | 7 | 85 | | if (layoutAttribute != null) |
| | | 86 | | { |
| | 5 | 87 | | return layoutAttribute.LayoutType; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // Check if page requires authentication |
| | 2 | 91 | | var requiresAuth = pageType.GetCustomAttributes(typeof(AuthorizeAttribute), true).Any(); |
| | | 92 | | |
| | | 93 | | // Default to AuthenticatedLayout for pages requiring auth, PublicLayout otherwise |
| | 2 | 94 | | return requiresAuth ? typeof(AuthenticatedLayout) : typeof(PublicLayout); |
| | | 95 | | } |
| | | 96 | | } |