Home > Blockchain >  Get user.id as a string from clerk
Get user.id as a string from clerk

Time:04-19

I need to get the clerk id to check with the backend but clerk uses a hook which doesnt work in this type of function

export const getServerSideProps = async ({ req }) => {

  const { isLoaded, isSignedIn, user } = useUser();

 

  const userman = user.id;

 

  const posts = await prisma.kalender.findMany({

    where:{accountid: userman}

  })

  await prisma.$disconnect();

  return { props: { posts: JSON.parse(JSON.stringify(posts))  } }

}

This is how i would like it to work. Is there a way to get the user.id as a string? Thanks!

CodePudding user response:

You can cast user.id to string.

user.id.toString() or String(user.id), both will act the same if user.id is defined.

Ref:

CodePudding user response:

You can use type assertion.

1. Type assertion on UseUser.

First create a new type (above export const):

interface UseUserT {
  isLoaded: boolean;
  isSignedIn: boolean;
  user: { id: string };
}

Then assign this type to useUser:

const { isLoaded, isSignedIn, user } = useUser() as UseUserT;

2. Type assertion on user.id.

const userman = user.id as string;
  • Related