Current section: Managed Sessions 6 exercises

Updating Login and Signup Routes to Use Sessions instead of User IDs

Loading solution


00:00 Let's go to the auth server first, and we're going to rename this variable from UserIDKey to SessionKey. And there, that should update everything in this file. And actually, thanks to TypeScript being amazing, we're also going to get that update here.

00:17 So SessionKey is right there. So that's awesome. So now, instead of worrying about the session or the UserIDKey variable being a funny name, now it's SessionKey. But we also have to update a couple other things. So this should be a Session, not a User, right here.

00:36 So now we're relying on Sessions instead of Users. This returns a Session, not a User, so we can call that Session. And down here, this will be a Session, not a User. And then down here, we're going to check for the Session, if the Session exists.

00:54 And this is going to be a Session as well. All that we're dealing with is Sessions. We set the SessionKey to the Session.id. And then the expiration date, actually, will come from the Session expiration date. And that is that.

01:10 We do the exact same thing on the signup, so let's just do that really quick. We've got User right here. This is a Session now. Session and Session. All that is done and done. We've got Session right here.

01:29 And this will be a Session and a Session. And there we go. Oh, and finally, the expiration date, again, comes from the Session. And there we go. Let's just make sure we can actually log in. Cody. Cody loves you. And there we go.

01:48 We're logged in. So that is updating our login and our signup routes to handle the new Session stuff. Turns out it's actually not, like, superly complicated. The biggest thing with doing managed Sessions is just the database update,

02:05 because now you have to put your Session data in the database. And then everything else is pretty much the same as with the User ID. So there is a little bit of a drawback because now we have to hit the database every time we make these requests to make sure that their Session is still valid and all that stuff.

02:24 But I think that's actually fine, especially since we're using SQLite. That's going to be very fast anyway. And everything's indexed. It's fast. It's no problem. So in review, all we did here was update the Session key and then update everything that was referring to Users and stuff to be Session. And that's it.

02:43 Just a little bit of a refactor. Well done.