GTBitsOfGood / canine-assistants

Educating the Dogs Who Change the World
https://dev--canine-assistants.netlify.app
MIT License
4 stars 0 forks source link

[FE/BE] Refactor how we invite users #167

Closed afazio1 closed 6 months ago

afazio1 commented 7 months ago

Description

Problem: If an invited users tries to sign in with google, our auth will not let them sign in and give a "OAuthAccountNotLinked" error. This happens when a user with the specified email is in the db, but their oauth access token (Google in this case) is not connected to their account.

Currently, when we invite a user, we add a new user document to the Users collection in our database with email and role fields. We use NextAuth's MongoDBAdapter, which automatically creates users in the Users collection and links their Oauth account (Google) by adding it to the Accounts collection on signup. However, since the partial user already exists in our db (bc we invited them) the MongoDBAdapter thinks there is an account link problem, but really the account just needs to be linked for the first time. Since account linking is not something that we want to do manually for security reasons, we must refactor how we invite users.

Solution: Instead of inserting invited users into the Users collection, add them to a MongoDB collection called InvitedUsers. So when the NextAuth MongoDBAdapter implicitly looks inside the Users collection, it will not find the invited user and thus it will create a new user and link the Google account.

Refactors:

Sign in / Sign up Flow

signin-flow

Other Notes