Refactoring Login Logic and Implementing GitHub Login
00:00 Let's head over to login first to do this refactor. So right down here in our action, we've got all of this logic that involves after we know that this user is who they say they are, and we've got a session ready for them. This involves the whole process of getting them logged in.
00:19 So we're going to take this and we're going to cut it out. We're going to return handle new session, which will pass the request session, session, remember, and redirect to. Now, we can create that function and
00:37 this should work exactly as it did before. So if we come up here, we export a function called handle new session, and paste all this stuff in. Then we just have to take those arguments. So our request, our session, the remember, and the redirect to, and we can type those.
00:54 Copilot is pretty good at all this stuff, and redirect to. The one thing Copilot missed was we do also need the user ID, which is a string. But other than that, good job, Copilot. So that's just like straight up refactor. Let's see what this is complaining about. We've got the remember and redirect to.
01:13 Both of those should be optional. So Copilot didn't quite get that, but that's okay. Then for remember, let's just default that to false. There we go. With that, our login should still work. In fact, let's just test it out. Cody loves you, and yep, login still works as it did before.
01:32 But now, Cody has a connection, and when I click on this button, that should be connected to Cody. So let's go make that work, and then we just follow through with this step. So we go to our provider callback, and right here, we've already checked for some error cases and stuff. So we just need to determine that if there is a connection,
01:52 and there's not a user, so we're already checking if there is a connection and there's a user. So if there is a connection, then there's not a user. So the user is not logged in. So if there is an existing connection, then we're going to create a session. Await Prisma session create,
02:10 and here we're going to say our data has expiration date, and get session expiration date there, and our user ID will be the existing connection.userid. Then we want to select the ID, true, and expiration date,
02:27 true, and also user ID, true. With that now, we can just simply return handle new session from the login, and we'll pass the request and session, and for us, we'll say remember is true.
02:44 We can deal with that at a later time. With that, we should be able to click on this login with GitHub and poof, I'm logged in as Cody. So that worked out nicely. Let's just quickly review, and then we can wrap this up. So we took all of this logic that we had in
03:23 So we're going to create a session for them and handle that new session. If you want to, you can go ahead and enable two-factor auth for Cody and test the whole thing out. The two-factor auth will still work because we handle all of that right here, where we say, should request to UFA, then we'll go through the verify session and
03:42 take them to the verify page to verify. So all that logic is still shared and it works, and it's awesome. Good job on finally making it so login with GitHub actually logs in with GitHub.