Get username from a Clerk user
I'm building a blog and I want to display the author's username at the of each blog but I don't know how to get it other than with useUser() hook or currentUser(), but the problem with these 2 solutions is that it displays the current user username. Any help would be much appreciated and sorry for my english
15 Replies
@Cxstum
user.firstName
on the User
objectIt keeps telling me that it's undefined, do you think I should start using trpc to with my project to see if I can fix it because im only using mongodb and prisma. Thx for the help btw
can you show code?
no trpc is complex
if you don't know that you need it, don't add it – you will waste days and it's not useful for the users
@eula1
You should should maybe try a conditional check {user?.firstname}
and where is the user coming from?
What do you mean
show the parent component
where you do
auth()
@eula1 The problem now is that I don't know what to put in the user property
You need to save the users in your own db / make a look up at with the author id you have for your post
The way you have it right now it would always be the signed in user not the actual author of the post
So what you need todo is sync your clerk users to your db, or save a clerkUserId on the post entity in your database
Actually now that I check the docs you probably need to sync users to your own db, check out this docs page: https://clerk.com/docs/users/sync-data#sync-data-to-your-backend-with-webhooks
Sync Clerk data to your backend with webhooks | Clerk
The recommended way to sync data between Clerk and your application's backend is via webhooks. In this guide you'll learn how to enable webhooks and how to set up your backend so that it is updated every time an event happens on your Clerk instance.
Im pretty sure I did every step right so now what do I do to get the user information in my code. Btw im using prisma with mongodb
I haven't used mongodb / no sql for a long time, but in theory what you can do is whenever you create a new post, associate that post with the user in your database
The best way todo this imo is to have the user id = the clerk user id
And then when you fetch the posts, just also fetch the authors information from the associated user
What are you using right now?
drizzle and postgres / mysql