How to refetch data after Clerk login?
Problem: data is stale after authentication change.
I need a way to invalidate data (tRPC) after login/logout. Clerk doesn't seem to give you a way to manually control login-logout. They give you prebuilt SignInButton/SignOutButton but you can't control it.
I tried using useEffect with {isSignIn} = useUser() but it only worked with sign-out and not sign-in.
Solution:Jump to solution
Update your middleware matcher
```tsx
export const config = {
matcher: [...
13 Replies
Clerk gives you plently of ways to check login status and even how to log out.
useAuth has:
isSignedIn
<-- will be false on sign out.
signOut
<-- a way to sign out
userId
<-- will be null on sign out. and not when you are logged in.
useClerk()
gives you access to every low level part of Clerk.
Maybe describe what you actually want? The code snippet you have is just a way to sign in so not sure that is relevant.
If you want to handle your own flows to sign in.
useSignIn
useSignUp
Then you can do conditional fetches if that is what you are asking:
When I sign-in/sign out with the <SignInButton>/<SignOutButton>, my trpc data is isn't updated. So I need something like this.
I also tried useEffect like this. This only works for sign-out however.
Basically after the redirect from clerk sign-in page I need to invalidate data
Although I don't understand why it doesn't do that automatically since it should be similar to a full page refresh
This fires every single time the state changes, userId or userId becomes null.
Clerk doesn't full refresh on a sign out, nor really on sign in depending on how you are doing signin.
Same for this:
same problem, it only works on sign-out
Both of those work on sign in and sign out.
Why don't you give the full code example that you are trying
https://github.com/Apestein/chirp
in: https://github.com/Apestein/chirp/blob/main/src/components/layout.tsx
okay this is little weird, problem exist on dev build only
the production updates fine
on my dev build the hearts don't update when I log-in
Solution
Update your middleware matcher
So your tRPC routes run correctly.
I see useEffect firing correctly
yeah that fixed it
been trying to fix this one for awhile, thanks
i was following theo tutorial, that's why i had that matcher
Yeah no worries... I work at Clerk so happy to help 😄
I couldn't find any documentation on it. Could you point me to the docs if they exist
And it seem to work fine here: https://github.com/t3dotgg/chirp/blob/main/src/middleware.ts
And also on clerk: https://clerk.com/docs/nextjs/middleware
TRPC | Clerk
Learn how to integrate Clerk into your Next.js with TRPC