Skip to content

Instantly share code, notes, and snippets.

@diasjuniorr
Last active June 9, 2021 23:46
Show Gist options
  • Save diasjuniorr/1b6707ed2fff505a68af784749d18560 to your computer and use it in GitHub Desktop.
Save diasjuniorr/1b6707ed2fff505a68af784749d18560 to your computer and use it in GitHub Desktop.
Next.js middleware implementation
// pages/api/endpointGet.ts
import { auth } from "../util/middleware/auth"
// so we can add the user info to the req object
type NextApiRequestWithUser = NextApiRequest & {
user: string
}
const handler = async (req: NextApiRequestWithUser, res: NextApiResponse) => {
const { method, headers, query, user } = req
if (method === 'GET') {
console.log("inside the handler")
return res.status(200).json({ok: true})
}
res.setHeader('Allow', ['GET'])
return res.status(405).end(`Method ${method} Not Allowed`)
}
export default auth(handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment