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

[commerce-sdk-react] Bug Fix Cookie expires time (@W-16626227@) #1994

Merged
merged 10 commits into from
Sep 4, 2024

Conversation

adamraya
Copy link
Collaborator

@adamraya adamraya commented Sep 3, 2024

Description

The PR fixes the commerce-sdk-react bug #1991.

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • Convert the expire number to Date object.

How to Test-Drive This PR

  1. Checkout this branch: fix-cookie-expires-time and run npm ci
  2. Start the template-retail-react-app: npm start
  3. Open Chrome DevTools Application > Storage > Cookie and verify the Expires time for the cc-nx-g_RefArchGlobal cookie is 30 days.

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)

@adamraya adamraya requested a review from a team as a code owner September 3, 2024 19:50
beforeEach(() => {
storage = new CookieStorage()
jest.useFakeTimers()
Date.now = jest.fn(() => new Date('2024-09-03T21:00:00Z').getTime())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prototype pollution! Date.now is changed for the entire node process for the jest tests. I think ya may need to revert the implementation of Date.now after these tests are done

Comment on lines 29 to 33
const cookieOptions = {
...defaultAttributes,
...options,
expires: this.convertSecondsToDate(options?.expires)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this is the right place to making tis fix. Expires is a date and we are using seconds (ttl). We should be converting the ttl in seconds to a date before calling set.

// auth/indx.ts line 381
this.set(refreshTokenKey, res.refresh_token, {
        expires: res.refresh_token_expires_in // This needs to be updated here.
    })

Comment on lines 365 to 368
if (typeof seconds === 'number') {
return new Date(Date.now() + seconds * 1000)
}
return undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on throwing if the parameter isn't a number instead of returning a different type object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theundefined value is valid for expires cookie attribute in js-cookie. Creates a session cookie that expires when the browser is closed if the expires is undefined.
https://github.com/js-cookie/js-cookie/tree/latest?tab=readme-ov-file#expires

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.. I figured that was the case. Maybe I'm just being a little nit picky but the name of your function implies that you are going to return a date hence the comment. I suppose you can return undefined, just make sure you describe this functions behaviour in a js doc comment so others will understand how it works.

@adamraya adamraya changed the base branch from release-commerce-sdk-react-3.0.1 to release-3.7.x September 4, 2024 17:33
Comment on lines +395 to +397
const expiresDate = res.refresh_token_expires_in
? this.convertSecondsToDate(res.refresh_token_expires_in)
: undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned because our tests aren't all properly passing tokens it might be too much work at the moment to fix them, so we are just falling back to allowing the passing of undefined to the set function. We should look at fixing this in the future and we need to create a ticket for this to place in the backlog.

@adamraya adamraya merged commit 1fef72f into release-3.7.x Sep 4, 2024
28 checks passed
@adamraya adamraya deleted the fix-cookie-expires-time branch September 4, 2024 23:32
adamraya added a commit that referenced this pull request Sep 5, 2024
… (#1996)

* Fix Cookie expires converting seconds to Date

* Update CHANGELOG.md

* Add tests

* PR Feedback

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




* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants