Closed 5knnbdwm closed 3 weeks ago
Hey, thanks for checking this module out.
Did you add vue-clerk/nuxt
to modules
array in your nuxt.config.ts
file?
hey, yes i did. useAuth does work in a normal endpoint just doesnt seem to work in the middleware
This is a good catch. Thanks! Looks like this might be caused by the ordering of middleware. Looking into it now
Okay just created a reproduction if it as well so it wasn't just a bug in my project.
But thanks for the quick response and looking into it.
thanks, yeah I can confirm this one
Hello! Sorry for the late update.
I have added an experimental feature where you can add custom logic when you want to protect API routes. See this PR for more info.
Example:
// server/middleware/clerk.ts
import { clerkMiddleware } from 'vue-clerk/server'
export default clerkMiddleware((event) => {
const protectedRoutes = ['/api/protected', '/api/another-protected-route']
const { auth } = event.context
if (!auth.userId && protectedRoutes.includes(event.context.path)) {
throw createError({
statusCode: 401,
message: 'Unauthorized'
})
}
})
Thanks. That works beautifully
Hey, I basically want to protect most of my api with clerk, also dont really need access to the userObject so i just wanted to do the auth check in middleware.
However when doing so
getAuth()
returnsundefined
. Any idea as how to solve this?