I have App.razor file looking like this:
@using MyFirstServerSideBlazor.Pages
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeView>
<Authorized>
<RouteView DefaultLayout="@typeof(MainHeaderLayout)" RouteData="@routeData" />
</Authorized>
<NotAuthorized>
<RouteView DefaultLayout="@typeof(LoginBody)" RouteData="@routeData" />
</NotAuthorized>
</AuthorizeView>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<p role="alert">Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
</CascadingAuthenticationState>
Is there some way I can specify what routes I want to show, for Unauthenticated user ?
Lets say I want to let Unauthenticated users to navigate to /Login & /SignUp pages.
I could probable use attributes or something on pages to specify that only authenticated user can view them, but this seems to be inconvenient. I’m wondering is there some better solution ?
I could also make component that shows different pages, but Im unable to change URI routes that way, example would be:
<NotAuthorized>
<ComponentForUnauthorizedUsers/>
</NotAuthorized>
And then in that component have some logic like this:
@using MyFirstServerSideBlazor.Componenets.Forms
@inject NavigationManager navigation
@{
if (ShowLogin)
{
<PageTitle>Login</PageTitle>
}
else
{
<PageTitle>Sign Up</PageTitle>
}
}
@{
if (ShowLogin)
{
<LoginForm ChangePageCallback="ChangeToSignUp"/>
}
else
{
<SignUpComponent ChangePageCallback="ChangeToLogin"/>
}
}
@code{
public bool ShowLogin { get; set; } = true;
public void ChangeToSignUp()
{
ShowLogin = false;
StateHasChanged();
}
public void ChangeToLogin()
{
ShowLogin = true;
StateHasChanged();
}
}
this also seems wrong, and this way I cannot use Routing.
2
Answers
Okay with few tweaks I managed to make it Work, In my App.Razor:
Then in UnauthorizedLayout I have this:
In UnauthorizedViewControler component I have this logic:
And for pages themselves :
,
And this works, I have routing for /Login and /SignUp.
Code, could use some cleaning up, but in principle this works.
There’s a handful of options here and I wish I knew the best one myself. Here’s what I’m currently doing in my app:
App.Razor
Deciding which pages require auth
If a page does not require authorization, then I just write the page as normal. If it does require authorization, then I drop the Authorize attribute into the
MyPage.razor
file.Different components
As you mentioned, you can show different links or something if you want to based on whether a user is logged in (authorized?) or not.