In this task, we will update the UI components to hide the βEditβ and βDeleteβ buttons for posts that are not created by the logged-in user. We will also update the API calls to include the session cookie in the request. Finally, we will add toast messages to prompt the user to sign in or create an account when they try to create a post or comment without being logged in.
Step 1: Define the User Type
Add a new UserType type to the web/src/data/types.ts file:
Notice that the PostType and CommentType types now include an author field of type UserType.
Step 2: Author Component
Create a new Author component in the web/src/components/shared/author.tsx file:
This component displays the authorβs name and username. You can later customize it to include the authorβs profile picture.
Step 3: Update the Post Component
Letβs update the Post component to use the Author component. Open the web/src/components/post/post.tsx file and update it as follows:
Run the app and check if the authorβs name and username are displayed correctly.
Step 4: Update the Post Actions Component
Currently, the PostActions component displays the βEditβ and βDeleteβ buttons on all posts. We want to show these buttons only if the logged-in user is the author of the post. Open the web/src/components/post/post-actions.tsx file and update it as follows:
Notice that we use the useAuth hook to get the logged-in user. We then check if the logged-in user is the author of the post to show the βEditβ and βDeleteβ buttons.
Run the app and check if the βEditβ and βDeleteβ buttons are displayed only for the author of the post.
Step 5: Update the Header Component
We have two filters in the header: βMy Postsβ and βAll Posts.β We want to show the βMy Postsβ filter only if the user is logged in. Open the web/src/components/layout/header.tsx file and update it as follows:
Try the app now. You should see the βMy Postsβ and βAll Postsβ buttons in the header. The βMy Postsβ button should be hidden if the user is not logged in.
Move the enableFilter State to a Global Store
Letβs move the enableFilter state to a global store. Update the web/src/lib/store.ts file by including the following code:
Now update the web/src/components/layout/header.tsx file to use the global store:
Now the enableFilter state is managed by the global store.
Step 6: Update API Calls
We need to update the API calls to include the session cookie in the request. This is necessary for the server to authenticate the user.
Add the following settings to all the fetch calls in web/src/data/api.ts:
For example:
This setting to ensure that the browser sends the cookies with the request. This is necessary for the server to authenticate the user.
Next, we will update the fetchPosts and fetchComments functions to accept an optional username parameter. This will allow us to fetch posts/comments for a specific user.
With these changes, we can now fetch posts and comments for a specific user.
Step 7: Update the useQueryPosts Hook
Update the useQueryPosts hook in web/src/hooks/use-query-posts.tsx to include the enableFilter state from the global store. This will allow us to fetch posts for a specific user when the βMy Postsβ filter is enabled.
Step 8: Update the Posts Component
Update the Posts component in web/src/components/post/posts.tsx to include the enableFilter state from the global store. This will allow us to reset the infinite scroll state when the βMy Postsβ filter is enabled.
Adding a key prop to the InfiniteScroll component. This ensures that the component is re-mounted when the filter changes, which resets the infinite scroll state.
Step 9: Update the useQueryComments Hook
Update the useQueryComments hook in web/src/hooks/use-query-comments.tsx to include the enableFilter state from the global store. This will allow us to fetch comments for a specific user when the βMy Commentsβ filter is enabled.
We did not implement infinite scroll for comments, so we donβt need to reset the comments when the filter changes.
Step 10: Update the Sidebar Component
in the previous steps, weβve hidden the βDeleteβ and βEditβ buttons for posts that are not created by the logged-in user. Moreover, weβve removed the βMy Postsβ filter when the user is not logged in. We can perform similar checks in the Sidebar component to hide the βMake a Postβ and βMake a Commentβ buttons when the user is not logged in.
However, we want to show a toast message to prompt the user to sign in or create an account when they try to create a post or comment without being logged in. We can use the useAuth hook to get the logged-in user and show a toast message using the useToast hook.
Update the web/src/components/layout/sidebar.tsx file:
Try the app now: sign out and try to create a post. You should see a toast message prompting you to sign in or create an account.
Step 11: Update the Post Actions Component
Likewise, letβs require a user to sign in or create an account to view the comments for a post. Update the web/src/components/post/post-actions.tsx file:
Try the app now: sign out and try to view the comments for a post. You should see a toast message prompting you to sign in or create an account.
Conclusion
In this task, we added authorization to the Posts app. We updated the UI components to hide the βEditβ and βDeleteβ buttons for posts that are not created by the logged-in user. We also updated the API calls to include the session cookie in the request. Finally, we added toast messages to prompt the user to sign in or create an account when they try to create a post or comment without being logged in.