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

Feature/MongoDB Session Functions #1142

Conversation

SimonGuethler
Copy link

@SimonGuethler SimonGuethler commented Aug 23, 2022

Issue

The MongoDB session store service is missing some functions, which we needed in a past project. In that, we were using FoalTS with MongoDB, and we extended those functionalities. This PR is supposed to contribute them back.

Solution and steps

  • Add getSessionIDsOf function
  • Add getSessionsOf function (a new one which returns all whole session objects for a user)
  • Add getAuthenticatedUserIds function
  • Add destroyAllSessionsOf function

Checklist

  • Add function docs
  • Add tests
  • Update foal docs Session Tokens ("This feature is only available with the TypeORM store." comments)

@LoicPoullain LoicPoullain added this to Work In Progress in Issue tracking via automation Aug 23, 2022
@codecov-commenter
Copy link

codecov-commenter commented Aug 23, 2022

Codecov Report

Merging #1142 (a945b4a) into master (a193330) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1142   +/-   ##
=======================================
  Coverage   98.94%   98.94%           
=======================================
  Files          96       96           
  Lines        1712     1712           
  Branches      404      404           
=======================================
  Hits         1694     1694           
  Misses         18       18           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@LoicPoullain LoicPoullain self-requested a review August 29, 2022 05:50
Copy link
Member

@LoicPoullain LoicPoullain left a comment

Choose a reason for hiding this comment

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

Hi @SimonGuethler 👋

Thank you for your PR. These methods were definitively missing for NoSQL databases.

I added some quick notes on the PR 👍

* @param user
*/
async destroyAllSessionsOf(user: { id: string }): Promise<void> {
await this.collection.deleteMany({ 'state.userId': user.id.toString() });
Copy link
Member

Choose a reason for hiding this comment

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

Why calling the toString() function? Isn't user.id sufficient in this case?

Copy link
Member

Choose a reason for hiding this comment

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

I notice that if the ID of the user is _id, the functions won't work. Maybe we should change both functions of the MongoDB store and functions in TypeORM store in v3 to take the ID as parameter and not user objects.

Copy link
Author

Choose a reason for hiding this comment

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

Yes that's correct, that's a little weird and needs a better solution. The problem lays in the missing ObjectID type, at least if it's dedicated to MongoDB and TypeORM. We could only take the id as a string as parameter which differs from the way Sessions are used with other databases in foal, but it would make it to be usable more universally across ORMs for MongoDB.

Copy link
Member

Choose a reason for hiding this comment

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

After working with TypeORM v0.3 for Foal version 3, it appears that the ORM badly supports the use of id instead of _id for MongoDB with this version.

Yes, I think that we should then let the user use the form of their choice and thus take only the ID as parameter and not the user object. I updated the code of TypeORMStore accordingly for v3 to make the methods consistent across the framework (event though the ID type is different): #1152

it('should destroy all sessions of a user', async () => {
const user = { id: 'asdf' };
await store.destroyAllSessionsOf(user);
const sessions = await store.getSessionsOf(user);
Copy link
Member

Choose a reason for hiding this comment

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

I think we could use the MongoDB driver here and not the method getSessionsOf in order to make unit tests more independent from each other. If at some point the getSessionsOf method fails or is removed, this test won't fail and won't need a change.

Copy link
Author

Choose a reason for hiding this comment

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

That makes sense. I'll implement that.

await createSessionTestData();
});

it('should return all distinct user ids', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should add another test or extend this one here to test the case that, when a user is not authenticated (i.e userId === null), the method does not return a null value in its returned array.

Copy link
Author

Choose a reason for hiding this comment

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

I'll add that.

@LoicPoullain LoicPoullain mentioned this pull request Sep 3, 2022
1 task
@LoicPoullain
Copy link
Member

Hi @SimonGuethler 👋

Did you get a chance to move foward on this PR? 😊

@LoicPoullain
Copy link
Member

Hi @SimonGuethler 👋

This PR is a year old, so I'm going to close it. If you want to still work on it, don't hesitate to reopen it. 👍

Issue tracking automation moved this from Work In Progress to Done / Closed This Release Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Issue tracking
  
Done / Closed This Release
Development

Successfully merging this pull request may close these issues.

None yet

3 participants