Angel (Usen Arch Linux)
Angel (Usen Arch Linux)
BABetter Auth
Created by Angel (Usen Arch Linux) on 4/26/2025 in #help
Environment Variables not found
No description
6 replies
BABetter Auth
Created by Angel (Usen Arch Linux) on 2/5/2025 in #help
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.
5 replies