| | | 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> |
| | 0 | 21 | | @RenderPageWithLayout() |
| | | 22 | | </CascadingAuthenticationState> |
| | | 23 | | |
| | | 24 | | @code { |
| | | 25 | | [Parameter] |
| | 0 | 26 | | public RouteData RouteData { get; set; } = default!; |
| | | 27 | | |
| | 0 | 28 | | private RenderFragment RenderPageWithLayout() => builder => |
| | 0 | 29 | | { |
| | 0 | 30 | | var pageType = RouteData.PageType; |
| | 0 | 31 | | var layoutType = DetermineLayoutType(pageType); |
| | 0 | 32 | | var requiresAuth = pageType.GetCustomAttributes(typeof(AuthorizeAttribute), true).Any(); |
| | 0 | 33 | | |
| | 0 | 34 | | if (layoutType != null) |
| | 0 | 35 | | { |
| | 0 | 36 | | // Render with layout |
| | 0 | 37 | | builder.OpenComponent<LayoutView>(0); |
| | 0 | 38 | | builder.AddAttribute(1, "Layout", layoutType); |
| | 0 | 39 | | builder.AddAttribute(2, "ChildContent", (RenderFragment)(contentBuilder => |
| | 0 | 40 | | { |
| | 0 | 41 | | if (requiresAuth) |
| | 0 | 42 | | { |
| | 0 | 43 | | // Use AuthorizeRouteView for pages with [Authorize] |
| | 0 | 44 | | contentBuilder.OpenComponent<AuthorizeRouteView>(0); |
| | 0 | 45 | | contentBuilder.AddAttribute(1, "RouteData", RouteData); |
| | 0 | 46 | | contentBuilder.AddAttribute(2, "Authorizing", (RenderFragment)(authorizingBuilder => |
| | 0 | 47 | | { |
| | 0 | 48 | | authorizingBuilder.OpenComponent<AuthorizingScreen>(0); |
| | 0 | 49 | | authorizingBuilder.CloseComponent(); |
| | 0 | 50 | | })); |
| | 0 | 51 | | contentBuilder.AddAttribute(3, "NotAuthorized", (RenderFragment<AuthenticationState>)(authState => |
| | 0 | 52 | | { |
| | 0 | 53 | | return notAuthBuilder => |
| | 0 | 54 | | { |
| | 0 | 55 | | notAuthBuilder.OpenComponent<RedirectToLogin>(0); |
| | 0 | 56 | | notAuthBuilder.CloseComponent(); |
| | 0 | 57 | | }; |
| | 0 | 58 | | })); |
| | 0 | 59 | | contentBuilder.CloseComponent(); |
| | 0 | 60 | | } |
| | 0 | 61 | | else |
| | 0 | 62 | | { |
| | 0 | 63 | | // Render public page directly |
| | 0 | 64 | | RenderPageComponent(contentBuilder, pageType, RouteData); |
| | 0 | 65 | | } |
| | 0 | 66 | | })); |
| | 0 | 67 | | builder.CloseComponent(); |
| | 0 | 68 | | } |
| | 0 | 69 | | else |
| | 0 | 70 | | { |
| | 0 | 71 | | // No layout specified |
| | 0 | 72 | | if (requiresAuth) |
| | 0 | 73 | | { |
| | 0 | 74 | | builder.OpenComponent<AuthorizeRouteView>(0); |
| | 0 | 75 | | builder.AddAttribute(1, "RouteData", RouteData); |
| | 0 | 76 | | builder.AddAttribute(2, "Authorizing", (RenderFragment)(authorizingBuilder => |
| | 0 | 77 | | { |
| | 0 | 78 | | authorizingBuilder.OpenComponent<AuthorizingScreen>(0); |
| | 0 | 79 | | authorizingBuilder.CloseComponent(); |
| | 0 | 80 | | })); |
| | 0 | 81 | | builder.AddAttribute(3, "NotAuthorized", (RenderFragment<AuthenticationState>)(authState => |
| | 0 | 82 | | { |
| | 0 | 83 | | return notAuthBuilder => |
| | 0 | 84 | | { |
| | 0 | 85 | | notAuthBuilder.OpenComponent<RedirectToLogin>(0); |
| | 0 | 86 | | notAuthBuilder.CloseComponent(); |
| | 0 | 87 | | }; |
| | 0 | 88 | | })); |
| | 0 | 89 | | builder.CloseComponent(); |
| | 0 | 90 | | } |
| | 0 | 91 | | else |
| | 0 | 92 | | { |
| | 0 | 93 | | RenderPageComponent(builder, pageType, RouteData); |
| | 0 | 94 | | } |
| | 0 | 95 | | } |
| | 0 | 96 | | }; |
| | | 97 | | |
| | | 98 | | private void RenderPageComponent(RenderTreeBuilder builder, Type pageType, RouteData routeData) |
| | | 99 | | { |
| | 0 | 100 | | builder.OpenComponent(0, pageType); |
| | 0 | 101 | | foreach (var kvp in routeData.RouteValues) |
| | | 102 | | { |
| | 0 | 103 | | builder.AddAttribute(1, kvp.Key, kvp.Value); |
| | | 104 | | } |
| | 0 | 105 | | builder.CloseComponent(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | private Type? DetermineLayoutType(Type pageType) |
| | | 109 | | { |
| | | 110 | | // Check if page has explicit @layout directive |
| | 0 | 111 | | var layoutAttribute = pageType.GetCustomAttributes(typeof(LayoutAttribute), true) |
| | 0 | 112 | | .OfType<LayoutAttribute>() |
| | 0 | 113 | | .FirstOrDefault(); |
| | | 114 | | |
| | 0 | 115 | | if (layoutAttribute != null) |
| | | 116 | | { |
| | 0 | 117 | | return layoutAttribute.LayoutType; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Check if page requires authentication |
| | 0 | 121 | | var requiresAuth = pageType.GetCustomAttributes(typeof(AuthorizeAttribute), true).Any(); |
| | | 122 | | |
| | | 123 | | // Default to AuthenticatedLayout for pages requiring auth, PublicLayout otherwise |
| | 0 | 124 | | return requiresAuth ? typeof(AuthenticatedLayout) : typeof(PublicLayout); |
| | | 125 | | } |
| | | 126 | | } |