Current section: Connection Management 4 exercises

Connecting Existing Accounts with an Auth Provider

Loading solution


00:00 Let's go over to our auth provider callback. And right here, if there is a user ID, that means that they're logged in. So once we get to this point, we know that it's not a existing connection and a user ID. So if we get here and we say, if there's a user ID, then we know there's no existing connection.

00:19 So we can make the connection. So if user ID, then we can await PrismaConnection.create, create a connection for our user with the provider name that was parsed from the URL search, or from the route params, the provider ID from the profile

00:39 and the user ID of the users who's currently logged in. And then we can throw await redirect with toast to the profile connection page. We can give it the title of connected and description has been connected and type of success and we're golden. So check this out.

00:57 Let's open up that GitHub server util right here. So we can make changes to this. Hit connect with GitHub and boom, we're connected. Awesome. If I do another one, save that. Boom, connected again. And let's do another one here for connect. Boom, connected again. You can connect as many as you like.

01:16 And then of course you can delete these as well, which is pretty fancy. So in review, all that we had to do was if the user is logged in and there's no existing connection already, then we'll create a connection for them and then we'll redirect them back where they were and say, hey, good job, your account has been connected.

01:35 And that is connecting existing accounts.