Add User Data to a Post Creation in Remix

Ian Jones
InstructorIan Jones
Share this video with your friends

Social Share Links

Send Tweet

Finally, you want to associate users to the posts that they make. To do this you will grab the user data from authenticator.isAuthenticated API to pass to the post creation. This data does not need to be validated because it’s coming from the session storage which has already been validated.

Instructor: [0:00] Here in the Prisma schema, you can see that a post has an authorId. When you created the model, you added this feature, but you haven't been able to add users to the posts until authentication was completed.

[0:14] In the index action, you can see that a bad user ID is being passed. You only want authenticated users to be able to create posts. You can call authenticator.isAuthenticated inside of the action to accomplish this.

[0:31] This will protect the index route, as well as provide the authorId you need for the post. You don't need to validate this piece of data because it is provided by the session and not the user input.

[0:45] Next, you can pass user.id as the authorId when creating a post. Head on over to the browser and you can see, when you create a post, the user information is on it.

[1:05] To review, you pulled the user ID off of the call to authenticator.isAuthenticated. Then, you pass the user ID to the createPost function. You didn't have to validate this data because it is being provided by the session and not the user input.