diff --git a/CHANGELOG.md b/CHANGELOG.md index 93cde28..b66b977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- Authentication yo queries and mutation added + ## [1.16.3] - 2024-01-22 ## [1.16.2] - 2024-01-10 diff --git a/dotnet/GraphQL/Mutation.cs b/dotnet/GraphQL/Mutation.cs index 9c3a749..a299009 100644 --- a/dotnet/GraphQL/Mutation.cs +++ b/dotnet/GraphQL/Mutation.cs @@ -5,6 +5,11 @@ using WishList.Models; using WishList.Services; +// using Newtonsoft.Json; +using System; +using System.Linq; +using System.Net; + namespace WishList.GraphQL { [GraphQLMetadata("Mutation")] @@ -14,7 +19,7 @@ public Mutation(IWishListService wishListService) { Name = "Mutation"; - Field( + FieldAsync( "addToList", arguments: new QueryArguments( new QueryArgument> { Name = "listItem" }, @@ -22,8 +27,20 @@ public Mutation(IWishListService wishListService) new QueryArgument { Name = "name" }, new QueryArgument { Name = "public" } ), - resolve: context => + resolve: async context => { + + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + var listItem = context.GetArgument("listItem"); string shopperId = context.GetArgument("shopperId"); string listName = context.GetArgument("name"); @@ -32,15 +49,28 @@ public Mutation(IWishListService wishListService) return wishListService.SaveItem(listItem, shopperId, listName, isPublic); }); - Field( + FieldAsync( "removeFromList", arguments: new QueryArguments( new QueryArgument> { Name = "id" }, new QueryArgument> { Name = "shopperId" }, new QueryArgument { Name = "name" } ), - resolve: context => + resolve: async context => { + + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + int id = context.GetArgument("id"); string shopperId = context.GetArgument("shopperId"); string listName = context.GetArgument("name"); diff --git a/dotnet/GraphQL/Query.cs b/dotnet/GraphQL/Query.cs index 7c11d50..a2301d3 100644 --- a/dotnet/GraphQL/Query.cs +++ b/dotnet/GraphQL/Query.cs @@ -28,6 +28,18 @@ public Query(IWishListService wishListService) ), resolve: async context => { + + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + string shopperId = context.GetArgument("shopperId"); string name = context.GetArgument("name"); int from = context.GetArgument("from"); @@ -84,6 +96,18 @@ public Query(IWishListService wishListService) ), resolve: async context => { + + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + string shopperId = context.GetArgument("shopperId"); int from = context.GetArgument("from"); int to = context.GetArgument("to"); @@ -136,6 +160,7 @@ public Query(IWishListService wishListService) return resultLists; } + ); FieldAsync( @@ -147,6 +172,18 @@ public Query(IWishListService wishListService) ), resolve: async context => { + + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } + string shopperId = context.GetArgument("shopperId"); string productId = context.GetArgument("productId"); string sku = context.GetArgument("sku"); @@ -205,6 +242,16 @@ public Query(IWishListService wishListService) ), resolve: async context => { + HttpStatusCode isValidAuthUser = await wishListService.IsValidAuthUser(); + if (isValidAuthUser != HttpStatusCode.OK) + { + context.Errors.Add(new ExecutionError(isValidAuthUser.ToString()) + { + Code = isValidAuthUser.ToString() + }); + + return null; + } string shopperId = context.GetArgument("shopperId"); ResponseListWrapper allLists = await wishListService.GetLists(shopperId); IList listItemsWrappers = allLists.ListItemsWrapper; diff --git a/dotnet/Services/WishListService.cs b/dotnet/Services/WishListService.cs index c7d5048..c55c511 100644 --- a/dotnet/Services/WishListService.cs +++ b/dotnet/Services/WishListService.cs @@ -237,7 +237,8 @@ public async Task ValidateUserToken(string token) public async Task IsValidAuthUser() { - if (string.IsNullOrEmpty(_context.Vtex.AdminUserAuthToken)) + + if (string.IsNullOrEmpty(_context.Vtex.StoreUserAuthToken)) { return HttpStatusCode.Unauthorized; } @@ -245,7 +246,7 @@ public async Task IsValidAuthUser() ValidatedUser validatedUser = null; try { - validatedUser = await ValidateUserToken(_context.Vtex.AdminUserAuthToken); + validatedUser = await ValidateUserToken(_context.Vtex.StoreUserAuthToken); } catch (Exception ex) { diff --git a/react/AddProductBtn.tsx b/react/AddProductBtn.tsx index f6fa0cd..d9190c5 100644 --- a/react/AddProductBtn.tsx +++ b/react/AddProductBtn.tsx @@ -8,7 +8,7 @@ import React, { } from 'react' import { useMutation, useLazyQuery } from 'react-apollo' import { defineMessages, useIntl } from 'react-intl' -import { useProduct } from 'vtex.product-context' +import { ProductContext } from 'vtex.product-context' import { Button, ToastContext } from 'vtex.styleguide' import { useRuntime, NoSSR } from 'vtex.render-runtime' import { useCssHandles } from 'vtex.css-handles' @@ -153,17 +153,14 @@ const AddBtn: FC = ({ toastURL = '/account/#wishlist' }) => { const { push } = usePixel() const handles = useCssHandles(CSS_HANDLES) const { showToast } = useContext(ToastContext) - const productContext = useProduct() - const { selectedItem, product } = productContext + const { selectedItem, product } = useContext(ProductContext) as any const sessionResponse: any = useSessionResponse() const [handleCheck, { data, loading, called }] = useLazyQuery(checkItem) const [productId] = String(product?.productId).split('-') - const sku = product?.items?.[0]?.itemId + const sku = product?.sku?.itemId wishListed = JSON.parse(localStore.getItem('wishlist_wishlisted')) ?? [] - const productContextScoped = useProduct() - const toastMessage = (messsageKey: string, linkWishlist: string) => { let action: any if (messsageKey === 'notLogged') { @@ -307,14 +304,12 @@ const AddBtn: FC = ({ toastURL = '/account/#wishlist' }) => { }) pixelEvent.event = 'removeToWishlist' } else { - const { selectedItem: selectedItemScoped } = productContextScoped - addProduct({ variables: { listItem: { productId, title: product.productName, - sku: selectedItemScoped.itemId, + sku: selectedItem.itemId, }, shopperId, name: defaultValues.LIST_NAME,