Loading
Current section: Provider Errors 4 exercises
solution

Handling Existing Connections in AuthProvider Callback

Loading solution

Transcript

00:00 Let's go to our AuthProvider callback and right here we know that the user's already gone through the GitHub Auth flow and so by the time they get here we can look at that profile and know that whoever's making this request has access to this profile. Let's go see if there's an existing

00:15 connection. So an existing connection will come from await Prisma dot connection and it'll be actually a find unique where the provider name and provider id uniquely match the provider name

00:32 which that's coming from parsing the param for provider so that's going to be all the providers we support which right now it's just the one it's just going to be GitHub and we can see that in the types which is quite nice and then the provider id is going to come from the profile that GitHub said

00:49 yes whoever's making this request does own that profile and then we also want to get the user id which is going to be get user id from the request so we know that whoever it is on our side that is

01:04 making this connection. All right so with that now if there is an existing connection and there's a user id then we know that something's wrong either they're trying to connect their account a second time like they're logged in they're trying to connect that same account or they're trying to

01:20 connect an account that's already connected to somebody else which also is not okay and so basically we just need to let them know throw await a redirect with toast we'll send them back

01:31 to the connections page and we will say the type is ever and then the title is going to be already connected and then the description can depend on whether the existing connection dot user id which

01:50 of course yeah we want to select the just the user id is all we need we don't need anything else there so the user id is equal to the user id then in that case we can say you've already connected your account and this would not be you have already connected but that's someone else's

02:09 account there you go that works however your product manager wants that to be worded and with that now if i'm logged in and right now connect with github if we take a look at our github

02:22 provider right in here we set this up to have the code mock github kodi code code kodi and that one we have a user's local right here that is going to be for this code which is connected to that

02:39 users so that is the case right here we're going to try and connect ourselves to a github user that is already connected to ourselves so if i click on that we're already connected to your github account now if i log out and log in as somebody else so here we go let's go here log

02:59 out and then log in as this user and then we go to my profile and manage connections and try to connect that's someone else's account to github so we don't want to let people connect to the

03:14 same account to multiple user accounts and so we've just prevented that now if we want to test this out and be like oh well what if i'm not someone else's github account well then we can change this to something else this is our our mock that's running in line and now if i say

03:30 connect with github it's going to send me back to the home page the reason that it's doing that is because we haven't finished implementing actually making connections yet so it's redirecting me to log in but then login redirects me to home because i'm already logged in so that's what's going on there we'll get to the rest of this soon but the the point was we need to

03:50 handle some edge cases or which of course edge cases at scale become common cases but yeah we want to just make sure that if there's an existing connection and the user's logged in then they cannot connect it it's already been connected and they're not just trying to log in they're

04:06 trying to connect and yeah they just can't do that so that's how you handle that type of edge case