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

ProductList/AddToCart: Navigate to Login, if not authorized. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 14 additions & 13 deletions src/BlazorShop.Web/Client/App.razor
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<Login />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<CascadingAuthenticationState>
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<Login />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>

<LayoutView Layout="@typeof(MainLayout)">
<NotFound404 />
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
</NotFound>
</Router>
</CascadingAuthenticationState>
12 changes: 11 additions & 1 deletion src/BlazorShop.Web/Client/Pages/Products/ProductsList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ else
}

@code {
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }

private readonly ProductsSearchRequestModel model = new ProductsSearchRequestModel();

private ProductsSearchResponseModel searchResponse;
Expand Down Expand Up @@ -350,6 +353,13 @@ else

private async Task AddToCart(int id)
{
var user = (await authenticationStateTask).User;
if (!user.Identity.IsAuthenticated)
{
this.NavigationManager.NavigateTo("/account/login");
return;
}

var cartRequest = new ShoppingCartRequestModel
{
ProductId = id,
Expand Down Expand Up @@ -401,4 +411,4 @@ else
this.NavigationManager.NavigateTo($"/products/page/{this.model.Page}");
}
}
}
}