How to correctly update the user info?

I want to update specific fields, but the TypeScript tells me to provide all additional fields.
No description
6 Replies
bekacru
bekacru2d ago
If you're on a server already, you should just use your ORM since currently updateUser would trigger additional session fetch.
Nelson
NelsonOP2d ago
I did it before this approach. But the session isn’t updated unless using router.refresh()
bekacru
bekacru2d ago
try 1.2.2-beta.1
Nelson
NelsonOP2d ago
Thanks for the response! If I would like to use the ORM approach:
export const changeThemeAction = authenticatedActionClient
.schema(changeThemeSchema)
.action(async ({ parsedInput, ctx }) => {
const { theme } = parsedInput

await db.update(users).set({ theme }).where(eq(users.id, ctx.user.id))
})
export const changeThemeAction = authenticatedActionClient
.schema(changeThemeSchema)
.action(async ({ parsedInput, ctx }) => {
const { theme } = parsedInput

await db.update(users).set({ theme }).where(eq(users.id, ctx.user.id))
})
How to invalidate the session cache properly? Or should I stick with router.refresh() in Next.js?
bekacru
bekacru2d ago
if you're fetching the session on the client, you can just refetch on response.
Nelson
NelsonOP2d ago
I'm fetching it like this.
import { redirect } from 'next/navigation'

import { getSession } from '@/lib/auth'
import SessionProvider from '@/providers/session-provider'

type MainLayoutProps = {
children: React.ReactNode
}

const MainLayout = async (props: MainLayoutProps) => {
const { children } = props
const session = await getSession()

if (!session?.user) redirect('/login')

return <SessionProvider session={session}>{children}</SessionProvider>
}

export default MainLayout
import { redirect } from 'next/navigation'

import { getSession } from '@/lib/auth'
import SessionProvider from '@/providers/session-provider'

type MainLayoutProps = {
children: React.ReactNode
}

const MainLayout = async (props: MainLayoutProps) => {
const { children } = props
const session = await getSession()

if (!session?.user) redirect('/login')

return <SessionProvider session={session}>{children}</SessionProvider>
}

export default MainLayout
export const getSession = async (requestHeaders: Headers | null = null) => {
if (!requestHeaders) requestHeaders = await headers()

return await auth.api.getSession({
headers: requestHeaders
})
}
export const getSession = async (requestHeaders: Headers | null = null) => {
if (!requestHeaders) requestHeaders = await headers()

return await auth.api.getSession({
headers: requestHeaders
})
}

Did you find this page helpful?