|
11 | 11 | use craft\base\Component; |
12 | 12 | use craft\helpers\ArrayHelper; |
13 | 13 | use craft\helpers\Json; |
| 14 | +use craft\helpers\StringHelper; |
14 | 15 | use craft\log\MonologTarget; |
15 | 16 | use craft\shopify\events\DefineGqlFieldsEvent; |
16 | 17 | use craft\shopify\events\DefineGqlQueryArgumentsEvent; |
@@ -658,14 +659,23 @@ public function getAccessToken(?string $code = null, ?string $shop = null, bool |
658 | 659 | /** @var AccessToken $record */ |
659 | 660 | $record = AccessToken::find()->one() ?? new AccessToken(); |
660 | 661 |
|
661 | | - $success = true; |
662 | | - try { |
663 | | - $configService->setDotEnvVar(self::API_ACCESS_TOKEN_ENV_VAR, $body['access_token']); |
664 | | - } catch (\Throwable $e) { |
665 | | - $success = false; |
666 | | - Craft::error('Couldn\'t save the Shopify Access Token in the .env file. ' . $e->getMessage(), __METHOD__); |
| 662 | + // If there isn't a `.env` file, let's not try and save it there in case that is by design |
| 663 | + $dotEnvPath = $configService->getDotEnvPath(); |
| 664 | + $hasDotEnv = $dotEnvPath !== '' && file_exists($dotEnvPath); |
| 665 | + |
| 666 | + $isSavedToFile = true; |
| 667 | + if (!$hasDotEnv) { |
| 668 | + $isSavedToFile = false; |
| 669 | + } else { |
| 670 | + try { |
| 671 | + $configService->setDotEnvVar(self::API_ACCESS_TOKEN_ENV_VAR, $body['access_token']); |
| 672 | + } catch (\Throwable $e) { |
| 673 | + $isSavedToFile = false; |
| 674 | + Craft::error('Couldn\'t save the Shopify Access Token in the .env file. ' . $e->getMessage(), __METHOD__); |
| 675 | + } |
667 | 676 | } |
668 | | - $record->accessToken = $success ? '$' . self::API_ACCESS_TOKEN_ENV_VAR : Craft::$app->getSecurity()->encryptByKey($body['access_token']); |
| 677 | + |
| 678 | + $record->accessToken = $isSavedToFile ? '$' . self::API_ACCESS_TOKEN_ENV_VAR : StringHelper::encenc($body['access_token']); |
669 | 679 |
|
670 | 680 | if (!$record->save()) { |
671 | 681 | // Get the first error message from the record, if available: |
|
0 commit comments