Skip to content

Commit

Permalink
[commerce-sdk-react] Bug Fix Cookie expires time (@W-16626227@) (#1994)
Browse files Browse the repository at this point in the history
* Fix Cookie expires converting seconds to Date

* Update CHANGELOG.md

* Add tests

* PR Feedback

* Update packages/commerce-sdk-react/CHANGELOG.md

Co-authored-by: Kevin He <[email protected]>
Signed-off-by: Adam Raya <[email protected]>

* PR Feedback convert seconds to date in auth

* Bump version

* Bump retail-react-app

* Update convertSecondsToDate logic

* Support expires not defined

---------

Signed-off-by: Adam Raya <[email protected]>
Co-authored-by: Kevin He <[email protected]>
  • Loading branch information
adamraya and kevinxh authored Sep 4, 2024
1 parent 7a47e6b commit 1fef72f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions packages/commerce-sdk-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v3.0.1 (Sep 03, 2024)
- Fixed an issue where the `expires` attribute in cookies, ensuring it uses seconds instead of days. [#1994](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1994)

## v3.0.0 (Aug 07, 2024)
- Add `meta.displayName` to queries. It can be used to identify queries in performance metrics or logs. [#1895](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1895)
- Upgrade to commerce-sdk-isomorphic v3.0.0 [#1914](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1914)
Expand Down
4 changes: 2 additions & 2 deletions packages/commerce-sdk-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/commerce-sdk-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@salesforce/commerce-sdk-react",
"version": "3.0.0",
"version": "3.0.1",
"description": "A library that provides react hooks for fetching data from Commerce Cloud",
"homepage": "https://github.com/SalesforceCommerceCloud/pwa-kit/tree/develop/packages/ecom-react-hooks#readme",
"bugs": {
Expand Down
20 changes: 19 additions & 1 deletion packages/commerce-sdk-react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ class Auth {
store.delete(key)
}

/**
* Converts a duration in seconds to a Date object.
* This function takes a number representing seconds and returns a Date object
* for the current time plus the given duration.
*
* @param {number} seconds - The number of seconds to add to the current time.
* @returns {Date} A Date object for the expiration time.
*/
private convertSecondsToDate(seconds: number): Date {
if (typeof seconds !== 'number') {
throw new Error('The refresh_token_expires_in seconds parameter must be a number.')
}
return new Date(Date.now() + seconds * 1000)
}

/**
* This method stores the TokenResponse object retrived from SLAS, and
* store the data in storage.
Expand All @@ -377,9 +392,12 @@ class Auth {
this.set('customer_type', isGuest ? 'guest' : 'registered')

const refreshTokenKey = isGuest ? 'refresh_token_guest' : 'refresh_token_registered'
const expiresDate = res.refresh_token_expires_in
? this.convertSecondsToDate(res.refresh_token_expires_in)
: undefined

this.set(refreshTokenKey, res.refresh_token, {
expires: res.refresh_token_expires_in
expires: expiresDate
})
}

Expand Down
3 changes: 3 additions & 0 deletions packages/template-retail-react-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v4.0.1 (Sept 4, 2024)
- Updated @salesforce/commerce-sdk-react to 3.0.1 to fix an issue with the expires attribute of cookies, ensuring it uses seconds instead of days [#1994](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/1994)

## v4.0.0 (Aug 7, 2024)

### New Features
Expand Down
4 changes: 2 additions & 2 deletions packages/template-retail-react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/template-retail-react-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@salesforce/retail-react-app",
"version": "4.0.0",
"version": "4.0.1",
"license": "See license in LICENSE",
"author": "[email protected]",
"ccExtensibility": {
Expand Down Expand Up @@ -45,7 +45,7 @@
"@lhci/cli": "^0.11.0",
"@loadable/component": "^5.15.3",
"@peculiar/webcrypto": "^1.4.2",
"@salesforce/commerce-sdk-react": "3.0.0",
"@salesforce/commerce-sdk-react": "3.0.1",
"@salesforce/pwa-kit-dev": "3.7.0",
"@salesforce/pwa-kit-react-sdk": "3.7.0",
"@salesforce/pwa-kit-runtime": "3.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-commerce-sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"devDependencies": {
"@loadable/component": "^5.15.3",
"@salesforce/commerce-sdk-react": "3.0.0",
"@salesforce/commerce-sdk-react": "3.0.1",
"@salesforce/pwa-kit-dev": "3.7.0",
"@salesforce/pwa-kit-react-sdk": "3.7.0",
"@salesforce/pwa-kit-runtime": "3.7.0",
Expand Down

0 comments on commit 1fef72f

Please sign in to comment.