Browse Source

Allow newsletter subscription without requiring a logged-in customer.

Co-authored-by: Cursor <cursoragent@cursor.com>
chengwl 1 week ago
parent
commit
379c06b010

+ 2 - 2
packages/Webkul/BagistoApi/src/Models/Newsletter.php

@@ -31,8 +31,8 @@ use ApiPlatform\OpenApi\Model\Operation;
             description: 'Subscribe to newsletter',
             description: 'Subscribe to newsletter',
             openapi: new Operation(
             openapi: new Operation(
                 tags: ['Newsletter'],
                 tags: ['Newsletter'],
-                summary: 'Subscribe the authenticated customer to the newsletter',
-                description: 'Requires Bearer token. Creates a newsletter subscription for the authenticated customer on the current channel.',
+                summary: 'Subscribe an email to the newsletter',
+                description: 'Creates a newsletter subscription for the given email on the current channel. Authentication is optional; a Bearer token links the subscription to that customer.',
                 requestBody: new \ApiPlatform\OpenApi\Model\RequestBody(
                 requestBody: new \ApiPlatform\OpenApi\Model\RequestBody(
                     required: true,
                     required: true,
                     content: new \ArrayObject([
                     content: new \ArrayObject([

+ 1 - 12
packages/Webkul/BagistoApi/src/State/Processor/NewsletterSubscriptionProcessor.php

@@ -7,7 +7,6 @@ use ApiPlatform\State\ProcessorInterface;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Support\Facades\Validator;
 use Webkul\BagistoApi\Dto\SubscribeToNewsletterInput;
 use Webkul\BagistoApi\Dto\SubscribeToNewsletterInput;
-use Webkul\BagistoApi\Exception\AuthorizationException;
 use Webkul\BagistoApi\Exception\InvalidInputException;
 use Webkul\BagistoApi\Exception\InvalidInputException;
 use Webkul\Core\Models\SubscribersListProxy;
 use Webkul\Core\Models\SubscribersListProxy;
 
 
@@ -44,22 +43,12 @@ class NewsletterSubscriptionProcessor implements ProcessorInterface
         try {
         try {
             $customer = Auth::guard('sanctum')->user();
             $customer = Auth::guard('sanctum')->user();
 
 
-            if (! $customer) {
-                throw new AuthorizationException(__('bagistoapi::app.graphql.logout.unauthenticated'));
-            }
-
-            $token = $customer->currentAccessToken();
-
-            if (! $token) {
-                throw new AuthorizationException(__('bagistoapi::app.graphql.logout.token-not-found-or-expired'));
-            }
-
             SubscribersListProxy::create([
             SubscribersListProxy::create([
                 'email'         => $data->customerEmail,
                 'email'         => $data->customerEmail,
                 'channel_id'    => $data?->channelId ?? core()->getCurrentChannel()->id,
                 'channel_id'    => $data?->channelId ?? core()->getCurrentChannel()->id,
                 'is_subscribed' => 1,
                 'is_subscribed' => 1,
                 'token'         => uniqid(),
                 'token'         => uniqid(),
-                'customer_id'   => $customer ? $customer?->id : null,
+                'customer_id'   => $customer?->id,
             ]);
             ]);
 
 
             return (object) [
             return (object) [