Loading
Current section: Connection Management 4 exercises
solution

Automatically Connecting User Profiles with Existing Accounts

Loading solution

Transcript

00:00 Let's go to our provider callback. Right here, after we've established that, oh, there's already an existing connection, let's make that session. So all that stuff that we were doing before, we put into this handy little utility. The reason is because we need that utility for what we're about to do.

00:18 We're going to do exactly the same thing. So what we need to determine now is, okay, there's not an existing connection, but if there is a profile that already has the same profile email, then let's make that connection and then log them in.

00:34 So let's find the user, await prisma user.find unique, where the email address equals the profile.email. And if that's the case, let's also just select the ID. That's all we need, ID true. And if there is a user like that,

00:55 then we're going to create the connection. So await prisma connection create. And here's the data for that connection, our provider name, and it's going to be the provider name that we parsed from the URL. The provider ID is going to be the profile ID,

01:15 and then the user ID is going to be the user ID. And from there now, we have that connection. So in the future, they'll be able to fall into here and then just log in. And now we can just return make session, make session, there we go, with the request and the user ID, and that should do it.

01:36 But we do have some extra credit. So let's go ahead and do some extra credit here. Now our make session utility allows us to pass a redirect to and to also pass a response init as part of this handle new session. So this will take us into our response init right here,

01:53 which ultimately means that we can set the status code and set headers. And that's specifically the thing that we want to do is set some headers and the headers are necessary for us to be able to perform this toast. So we can go ahead and do the redirect and everything, but we don't have to redirect with headers.

02:12 We can use this create toast headers utility. Okay, so with that, then we've got headers and we can await create toast headers from our toast server and then our type can be success and our title can be connected and our description can be, yeah,

02:31 profile the username GitHub account has been connected. And then of course, we also want to specify a redirect too. So this should go to settings, profile, connections. That way they go straight over there and they say, oh, I just connected my account. I was unaware of this and they can delete it if they don't want it to be connected.

02:51 Maybe they were signed into the wrong account or something like this. Okay, great. So with that, we should be able to just click on login with GitHub, your GitHub account has been connected. Hooray, and there it is. And then we can delete it if we want to. So let's, yeah, let's quickly review what we did here.

03:10 So first of all, it's important to know that for us to be able to test this, our seed was updated so that we don't make the connection for Cody. And that way we can have a user that already exists that matches the email address for an existing user. Now you could go into the user's JSON file

03:30 and add a couple others if you want to, to test those out again. But yeah, that's how that's set up right from the get-go. And with that established now, we can check for the user based on the profile email. And if we do have a user with that profile email, then we'll create the connection so that next time they'll fall into here.

03:50 And then we'll make a session for the user. We'll redirect them to their connections page. And we also create toast headers so that they'll have the cookies so that the toast message pops up. And that is getting a user automatically connected when their user profile on GitHub or wherever

04:09 matches an existing profile that we've got.