Loading
Current section: User Sessions 5 exercises
solution

Handling User Authentication and Session Management with Prisma

Loading solution

Transcript

00:00 All righty, let's go to the root and right in here we need to load the user so we're going to get our cookie session from await session storage from our util.getSession from the request.headers.get

00:15 cookie, the cookie header from the request. And then we can get the user id from the cookie session.get user id and with that if there is a user id then we can grab the user. So our user

00:31 is going to equal user id and if that exists then it'll be yeah something like that. Yeah that's that's good enough. Let's grab Prisma right there and so we're going to find unique where the id

00:44 is equal to the user id. Now I am definitely a proponent of select and so we're going to add a select statement here and we will say id true and then we'll let TypeScript kind of let us know the

00:59 missing pieces here in a bit. So let's add our user right here and then down in our UI we've got this spot right here where we have the user so we can say data.user and from there we're going

01:16 to get TypeScript saying whoa whoa whoa we're missing a bunch of properties so we're going to need the username name and yeah that looks like that's it. Let's give that a whirl and see username true and name true and we've got one more and I know what it is it's the image so let's grab image

01:35 and that's a subselect because that's a separate model and grab the images id and that should do it. Oh my goodness there it is Cody you've never looked so great so much better and then we go here we're not logged in no cookies of any kind and boom we are seeing the login here let's just

01:52 try it Cody yada yada and we've got our cookie super duper. So this is all like this should actually be kind of review for what we've done so far with previous exercises is we literally just

02:06 we've got some mechanism for storing some session data we and we do that in the login so we grab that session storage we set a value and then we set the cookie and then in our root we read that

02:20 cookie read that value from the session and then we depending on whether that value exists we're going to read that user and then send that user along and then you can do all your UI stuff that you want to with that data. Pretty swell!