Nelson
Nelson
Explore posts from servers
BABetter Auth
Created by Nelson on 3/2/2025 in #help
How to correctly update the user info?
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
})
}
8 replies
BABetter Auth
Created by Nelson on 3/2/2025 in #help
How to correctly update the user info?
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?
8 replies
BABetter Auth
Created by Nelson on 3/2/2025 in #help
How to correctly update the user info?
I did it before this approach. But the session isn’t updated unless using router.refresh()
8 replies
BABetter Auth
Created by Nelson on 3/2/2025 in #help
How to manually invalidate session data?
Fixed by using auth.api.updateUser and nextCookies() plugin
6 replies
BABetter Auth
Created by Nelson on 3/2/2025 in #help
How to manually invalidate session data?
Thanks for your response! I'm still a bit confused about this. Currently, I'm retrieving all my sessions using:
await auth.api.getSession({
headers: requestHeaders
})
await auth.api.getSession({
headers: requestHeaders
})
What should I do in this case?
6 replies
BABetter Auth
Created by Highlighted on 2/23/2025 in #help
Better-auth and e2e tests
Thanks! But I end up using this one:
const cookie = await serializeSignedCookie(
'better-auth.session_token',
sessionToken,
env.BETTER_AUTH_SECRET
)
const cookie = await serializeSignedCookie(
'better-auth.session_token',
sessionToken,
env.BETTER_AUTH_SECRET
)
8 replies
BABetter Auth
Created by Highlighted on 2/23/2025 in #help
Better-auth and e2e tests
Hello, sorry to bother. I'm wondering how to achieve the same result with social provider?
8 replies
BABetter Auth
Created by Nelson on 3/1/2025 in #help
GitHub OAuth: user name can be null
Oh, my mistake. The name will never be null.
6 replies
BABetter Auth
Created by Nelson on 3/1/2025 in #help
GitHub OAuth: user name can be null
name: text('name').default('Anonymous').notNull() I end up using this.
6 replies
BABetter Auth
Created by Nelson on 2/8/2025 in #help
Override types
Thanks 🙏🏻
3 replies
DTDrizzle Team
Created by Nelson on 1/14/2024 in #help
Question about relations
Prisma:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime? @map("email_verified")
image String? @db.Text
accounts Account[]
sessions Session[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Comment Comment[]
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime? @map("email_verified")
image String? @db.Text
accounts Account[]
sessions Session[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Comment Comment[]
}
model Comment {
id String @id @default(cuid())
body String
user User @relation(fields: [userId], references: [id])
userId String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Post Post @relation(fields: [postId], references: [id])
postId String
children Comment[] @relation("comment_children")
parent Comment? @relation("comment_children", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
parentId String?

@@index([userId])
@@index([parentId])
@@index([postId])
}
model Comment {
id String @id @default(cuid())
body String
user User @relation(fields: [userId], references: [id])
userId String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
Post Post @relation(fields: [postId], references: [id])
postId String
children Comment[] @relation("comment_children")
parent Comment? @relation("comment_children", fields: [parentId], references: [id], onDelete: NoAction, onUpdate: NoAction)
parentId String?

@@index([userId])
@@index([parentId])
@@index([postId])
}
3 replies