-
Notifications
You must be signed in to change notification settings - Fork 298
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
chore(clerk-js): Reorganize cookies code and fix TokenUpdate event #3362
Conversation
🦋 Changeset detectedLatest commit: f731bfe The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
c1f4859
to
62a7170
Compare
|
||
// TODO: make SessionCookieService singleton since it handles updating cookies using a poller | ||
// and we need to avoid updating them concurrently. | ||
export class SessionCookieService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hard to see the diff of this file as it was moved...can you outline the changes made here briefly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to rename the service to AuthCookieService
. My intention is to create an interface that can be used both for Browser / Native apps and use 2 different implementations. The SessionCookieService
will be used for Browser.
Changes:
- move code cookies related with auth to this service
- introduce auth related helper methods that use auth cookies to avoid exposing cookies handling outside of this module
- apply changes due to refactoring cookies exposed interface
if (!inBrowser()) { | ||
return; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ since this class will only be initialized in standard browsers, there is no need to guard for non browsers.
if (!this.clerk.session) { | ||
return; | ||
} | ||
|
||
try { | ||
this.updateSessionCookie(await this.clerk.session?.getToken()); | ||
await this.clerk.session.getToken(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Since the this.clerk.session.getToken()
triggers the events.TokenUpdate
event which updates the session cookie there is no need to invoke this.updateSessionCookie
.
return setSessionCookie(rawToken); | ||
} | ||
return removeSessionCookie(); | ||
private updateSessionCookie(token: string | null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ simplified the function signature (dropped the TokenResource
) since it's being used internally this is not a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! Approving so you can merge tomorrow, but would like to see some inline comments added based on my initial review.
Nice cleanup!
62a7170
to
0e9fb89
Compare
4c7a342
to
d9d1b7f
Compare
@brkalow Could you take a look at the following commits (PR comments are addressed): |
@nikosdouvlis We removed the return statement because we didn't use it and it would make the CookieHandler method signatures used in clerk-js return values the same as the |
…ers for consistency There is also a fix for duplicate TokenUpdate event and for cleaning the token in sign-out flow.
… method of auth service
This change was applied to keep the existing behaviour: - clerk.user is used when clerk is loaded - clientUat cookie is used when clerk is NOT loaded
d9d1b7f
to
f731bfe
Compare
Description
Re-organize cookie codebase into a central place, fix
TokenUpdate
event to be triggered on sign-out and drop duplicate event on refreshing token.Checklist
npm test
runs as expected.npm run build
runs as expected.Type of change