Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Allow plain objects in Custom loadSession method #126

Merged
merged 4 commits into from
Mar 8, 2021

Conversation

thecodepixi
Copy link
Contributor

@thecodepixi thecodepixi commented Mar 5, 2021

WHY are these changes introduced?

Previously, we always expected the return from the loadCallback to already be an instanceof Session, but this can cause some issues in developing the loadCallback method for a custom storage solution, due to having to do some extra object manipulation.

WHAT is this pull request doing?

CustomSessionStorage.loadSession will now allow the loadCallback to return either an instanceof Session or a plain object. If a plain object is returned, it will be used to instantiate a new Session which will then be returned.

Type of change

  • Patch: Bug (non-breaking change which fixes an issue)
    - [ ] Minor: New feature (non-breaking change which adds functionality)
    - [ ] Major: Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above
  • I have added/updated tests for this change
  • I have documented new APIs/updated the documentation for modified APIs (for public APIs)

NOTE: I have updated the type on the current CustomSessionStorage docs under issues.md, but will be adding to and updating this docs for CustomSessionStorage more thoroughly in a separate PR.

@thecodepixi thecodepixi changed the title [WIP] Allow plain objects in Custom loadSession method Allow plain objects in Custom loadSession method Mar 5, 2021
@thecodepixi thecodepixi marked this pull request as ready for review March 5, 2021 22:01
@thecodepixi thecodepixi requested a review from a team as a code owner March 5, 2021 22:01
@paulomarg paulomarg mentioned this pull request Mar 8, 2021
1 task
@@ -35,6 +35,10 @@ export class CustomSessionStorage implements SessionStorage {
if (result) {
if (result instanceof Session) {
return result;
} else if (result instanceof Object && 'id' in result) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this but I wonder if it wouldn't be simpler to simply take in the JSON string? We could allow both but that seems a bit much.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I have strong feeling for or against that, it would just require more checking. One thing it does raise in my mind is that we're maybe reaching into their storage implementation by doing that? The custom storage solution should handle its own data in the loadCallback and then give it to us to essentially "pass through" loadSession. By offering to manipulate stringified objects (or potentially non-parseable strings) we're combining their solution with our solution, in a way. But if parsing doesn't work, that's their own error, not ours.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair - at some point we have to translate the external object to ours, but it does make it more flexible if we don't assume it will be JSON.

Even though I suspect JSON will be used in the vast majority of the cases, there's no reason not to support other methods as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there's no reason not to support other methods as well.

I can't tell if we're on the same page here? Do you mean we should add support for further data types in this method? My philosophy is "please make your data into this shape first" and have them pass in at least a JSON object. I think the manipulation of their data should be more delegated to their custom callback than to the library, personally. But since the library is what stipulates "make this a Session" it makes sense that we handle that step for them.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I was agreeing with you - as long as they give us an object that maps to what our session looks like, apps can do whatever they want with their objects. By 'other formats' I meant in how they would possibly serialize and de-serialize the object to store it in their database (like say a MongoDB object that's NOT converted to JSON, or something).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok yes! Yeah, I think this way we're being as flexible as possible, because we don't really care what they do to store/access the data, as long as it comes back to us in the right shape.

Copy link
Contributor

@paulomarg paulomarg left a comment

Choose a reason for hiding this comment

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

Minor nits but I'm good with it being merged as is.

CHANGELOG.md Outdated Show resolved Hide resolved
src/auth/session/storage/custom.ts Outdated Show resolved Hide resolved
@thecodepixi thecodepixi merged commit 475f414 into main Mar 8, 2021
@thecodepixi thecodepixi deleted the fix-custom-session branch March 8, 2021 21:29
@paulomarg paulomarg temporarily deployed to production March 16, 2021 17:41 Inactive
@carmelal carmelal mentioned this pull request Apr 23, 2021
4 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants