How to update `api/auth/get-session` with new session data?

I am updating the users table with the new name but accessing api/auth/get-session gives the old data. Is there a way to trigger an update when the database is updated so session data is also updated? I am using tanstack start. ```import { createAPIFileRoute } from '@tanstack/react-start/api' import { auth } from '@/lib/auth' export const APIRoute = createAPIFileRoute('/api/auth/$')({ GET: ({ request }) => auth.handler(request), POST: ({ request }) => auth.handler(request), })
18 Replies
rhitune
rhitune4w ago
send ur code
generator101
generator101OP4w ago
// /routes/api/auth/.ts'
import { createAPIFileRoute } from '@tanstack/react-start/api'

import { auth } from '@/lib/auth'

export const APIRoute = createAPIFileRoute('/api/auth/$')({
GET: ({ request }) => auth.handler(request),
POST: ({ request }) => auth.handler(request),
})
// /routes/api/auth/.ts'
import { createAPIFileRoute } from '@tanstack/react-start/api'

import { auth } from '@/lib/auth'

export const APIRoute = createAPIFileRoute('/api/auth/$')({
GET: ({ request }) => auth.handler(request),
POST: ({ request }) => auth.handler(request),
})
The server function in __root.tsx
const getUser = createServerFn({ method: 'GET' }).handler(async () => {
const { headers } = getWebRequest()!
const session = await auth.api.getSession({ headers })

return session?.user || null
})

beforeLoad: async ({ context }) => {
const user = await context.queryClient.fetchQuery({
queryKey: ['user'],
queryFn: ({ signal }) => getUser({ signal }),
})
return { user }
},
const getUser = createServerFn({ method: 'GET' }).handler(async () => {
const { headers } = getWebRequest()!
const session = await auth.api.getSession({ headers })

return session?.user || null
})

beforeLoad: async ({ context }) => {
const user = await context.queryClient.fetchQuery({
queryKey: ['user'],
queryFn: ({ signal }) => getUser({ signal }),
})
return { user }
},
and this is how I invalidate the session after database update:
queryClient.invalidateQueries({ queryKey: ['user'] })
router.invalidate()
queryClient.invalidateQueries({ queryKey: ['user'] })
router.invalidate()
bekacru
bekacru4w ago
first I recommend using authCllient.updateUser to update the user name. It'll handle invalidating and updating the cache. If you need to manually update, you can pass disableCookieCache as a query param when you call getSession to update the cache
generator101
generator101OP4w ago
@bekacru thanks for the response, is it possible the queryClient.invalidateQueries invalidate the cache? I am building a profile page where users can edit their name, so upon submission I want to update the session
bekacru
bekacru4w ago
It should unless you've enabled cookieCache. But you can also just call authClient.updateUser which invalidate the cache for you
generator101
generator101OP4w ago
@bekacru Yes, you were right, in my config I had it enabled, after disabling it everything gets invalidated
session: {
cookieCache: {
enabled: false,
},
},
session: {
cookieCache: {
enabled: false,
},
},
skidy
skidy3w ago
is that the only way? i have to process image upload at server side to get the url. currently i'm doing this
await auth.api.updateUser({
headers: await headers(),
body: {
image: imageUrl,
},
});
await auth.api.updateUser({
headers: await headers(),
body: {
image: imageUrl,
},
});
or at least i could modify the client's session?
bekacru
bekacru3w ago
you can use database hooks instead but this should work as well, if you're using nextCookies or setting cookies manually after the response
skidy
skidy3w ago
i don't get this, but i'm using nextjs. should it work automatically? i'm not even sure how to manually set it but with database hook, how can i make it accept File object? bruh, refetch function exist in authClient.useSession but it would nice if i could update the session without refetching
KiNFiSH
KiNFiSH3w ago
If you are using database hook you can upload the image and directly get the url from that and update your fields with that url
skidy
skidy3w ago
but updating user with File will make typescript complain for not passing a string
KiNFiSH
KiNFiSH3w ago
No your file field is gonna be string I mean link of the uploaded file which is uploaded before hand
skidy
skidy3w ago
uploading it in client side wouldn't work and it's not safe
KiNFiSH
KiNFiSH3w ago
That's up to your storage service if it provide an API keys or any kind pre signed url stuff that needs to process from server or client side plus database is not running on client side as well
skidy
skidy3w ago
File is from the browser uploaded by the user, and then get its data to upload it to s3 now if the process is successful i couldn't get its url and update user so it wouldn't be a string until i upload it
KiNFiSH
KiNFiSH3w ago
Yeah write your logic there at db hook and get the url back since all pre signing will handle s3
bekacru
bekacru3w ago
He is suggesting to upload the image first and then call updateUser. Which would be a bit slow since you'd have 2 round trips instead of one If you're using nextjs, just keep on what you're currently doing. If you munted nextCookies plugin in your auth config, it should update the cache for you when you call auth.api.updateUser
skidy
skidy3w ago
thanks, i have missing configuration but nextCookies only works with server actions, but not on api calls

Did you find this page helpful?