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
Merged
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` flag of the cookie was mistakenly set using days instead of the correct unit, seconds. [#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
10 changes: 9 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,13 @@ class Auth {
store.delete(key)
}

private convertSecondsToDate(seconds: number | undefined): Date | undefined {
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.

}

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

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

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

Expand Down
Loading