Establishing Connections and Seed Script in Prisma for GitHub Authentication
00:00 Let's go over to our schema here, and right down at the bottom, we're going to add a connection model. So model, connection, and it's going to have all the typical properties that we have. So we've got the ID just like all the others. We have a provider name. So this would be GitHub, or Google,
00:18 or whatever other service that you're authenticating with. If you're authenticating with an enterprise and doing single sign-on or something like that, that would be their company name or whatever. Then you'd have the provider ID. So some unique identifier for
00:35 that provider to identify that account on that provider. Then of course, created at and updated out, always useful fields to have, and then our relationship with the user. So the user ID field references, the ID field on the user model, and when the user is deleted or updated, that cascades down to this connection.
00:54 Then we're going to add a non-unique foreign key index on that user ID because as a default, it's just a good index to have. We'll make lookups on connections a lot faster and all that. Then our unique on the provider name and provider ID. The reason for that is because there should only be
01:12 one provider or provider ID on the provider's platform or whatever. So if two people try to create a connection for the same account, that would not be good. So we're going to add the unique constraint for that. Then if I save this, of course, we'll come up here to make sure that
01:30 the extension didn't mess this up on our user model, which is right here. Yeah, it did. We don't want it capitalized and we want it to be plural. So there we go. We've got our connections there now. With that now, we can just run npx prisma migrate dev.
01:48 We can give it a name of connections, and that will run our migrations. Awesome. Then we go to our seed script, and inside of our seed script down right around, here we go. We have this insert GitHub user function that is from our mocks. So we'll bring that in.
02:07 We're going to pass in the argument here is a code. So the code is how we identify different users, and that's the code that's sent back as part of the auth flow. So the code that we want this user to have is mock GitHub code Kodi.
02:27 So this is going to be our GitHub user. This is async because it's going to actually save a file to disk. So here's our GitHub user. We're going to add our connections right here. We're going to create a new connection, and the provider name is going to be GitHub, and the provider ID is going to be
02:46 the GitHub user.profile.id. That is what we need to do there. So now we run npx prisma db seed, and then we come over here to our fixtures. Once that's done, there we go.
03:04 We have our code for mock GitHub code Kodi, and that will send that all through for saving that into the database. So that profile ID is going to be in the database. We can take a look at that now too. Actually, if we look in Prisma DataDB, this was seeded, so we look in this connection,
03:23 and boom, there it is, our mock user ready for the whole auth flow to log you in as Kodi because that connection is already established once you implement that particular feature. Then we're finished. That's it. So well done. Really quick, let's just wrap this up with a quick review.
03:43 So we've got our connection, has the ID, the provider name and provider ID to identify a specific user on a specific provider. And then we have our relationships, our created at, updated at, our unique constraints and our indexes. All of that stuff is good. We update our seed script so that we create a GitHub user,
04:02 which will end up in this gitignored file so that we can do some local testing of GitHub users so we don't actually have to talk to GitHub. And our GitHub API mocks will reference this file to know, oh, there already is a user here, so I'll just send that token
04:22 and here's that user's profile, all of that stuff. And then we're all, we connect that GitHub user to our Kodi user, and we're all set to start actually responding to different scenarios for this login with GitHub, which we'll do next. But for now, you should just pat yourself on the back
04:40 because you did a good job.