Ways to have local middleware inside server/

I want to do authentication inside some of protected api routes. normally i have to put the checking for token header inside every single routes. can i have a local middleware to handling that
4 Replies
manniL
manniL6mo ago
how about building a wrapped defineEventHandler which is doing that for you? 🙂
budget mrekk
budget mrekk6mo ago
i plan to do that also. i plan to put it into utils to have a wrapped around? or i can pass the event params i get from the api routes? what do you think is better? cuz i use sidebase auth and the way to handling auth is just getServerSession(event). -> if (!session) throw createError(...) it's not long but i don't think copy and paste is a really good practice
budget mrekk
budget mrekk6mo ago
Nuxt
server/ · Nuxt Directory Structure
The server/ directory is used to register API and server handlers to your application.
budget mrekk
budget mrekk6mo ago
import { getServerSession } from "#auth"; import Cart from "~/server/models/cart.schema"; import mongoose from "mongoose"; const appConfig = useAppConfig(); export default defineEventHandler(async (event) => { const session = await getServerSession(event); if (!session) { throw createError(appConfig.error.unauthorized); } currently this is my setup
Want results from more Discord servers?
Add your server