After Hook

I am currently trying to send a registration email, and for that, I am using the after hook, following the documentation and example from Better Auth: Better Auth Hooks Documentation. This is my current code:
auth.ts
hooks: {
after: createAuthMiddleware(async (ctx) => {
console.log('Middleware executed in :', ctx.path)

if (ctx.path.startsWith('/sign-up/social')) {
const newSession = ctx.context.newSession
if (newSession) {
console.log('Sending email registration', newSession)
await sendEmailRegistration(
newSession.user.email,
newSession.user.name
)
}
}
})
}
auth.ts
hooks: {
after: createAuthMiddleware(async (ctx) => {
console.log('Middleware executed in :', ctx.path)

if (ctx.path.startsWith('/sign-up/social')) {
const newSession = ctx.context.newSession
if (newSession) {
console.log('Sending email registration', newSession)
await sendEmailRegistration(
newSession.user.email,
newSession.user.name
)
}
}
})
}
Additionally, I have the middleware configured as follows:
middleware

export default async function authMiddleware(request: NextRequest) {
const { data: session } = await betterFetch<Session>(
'/api/auth/get-session',
{
baseURL: request.nextUrl.origin,
headers: {
cookie: request.headers.get('cookie') || ''
}
}
)

if (!session) {
return NextResponse.redirect(new URL('/sign-in', request.url))
}

if (
session.user.role === 'user' &&
request.nextUrl.pathname.startsWith('/dashboard')
) {
return NextResponse.redirect(new URL('/profile', request.url))
}

return NextResponse.next()
}

export const config = {
matcher: ['/checkout/:path*', '/profile', '/dashboard:path*']
}
middleware

export default async function authMiddleware(request: NextRequest) {
const { data: session } = await betterFetch<Session>(
'/api/auth/get-session',
{
baseURL: request.nextUrl.origin,
headers: {
cookie: request.headers.get('cookie') || ''
}
}
)

if (!session) {
return NextResponse.redirect(new URL('/sign-in', request.url))
}

if (
session.user.role === 'user' &&
request.nextUrl.pathname.startsWith('/dashboard')
) {
return NextResponse.redirect(new URL('/profile', request.url))
}

return NextResponse.next()
}

export const config = {
matcher: ['/checkout/:path*', '/profile', '/dashboard:path*']
}
However, the console.log statements in the authentication middleware show the following output: Middleware executed in : /sign-in/social POST /api/auth/sign-in/social 200 in 70ms Middleware executed in : /callback/:id GET /api/auth/callback/google?... 302 in 210ms Middleware executed in : /get-session GET / 200 in 415ms Middleware executed in : /get-session GET /api/auth/get-session 200 in 64ms Is there something I might be overlooking that could be causing this behavior? I would really appreciate any help.
3 Replies
bekacru
bekacru3mo ago
you should use a database hook instead /sign-in/social doesn't register the user yet. It just generates the authorization url
Angel (Usen Arch Linux)
Thanks, I try solve this
Angel (Usen Arch Linux)
I finish xd
No description

Did you find this page helpful?