import cookieSession from 'cookie-session';
import { NextApiRequest, NextApiResponse } from 'next';
import nc from 'next-connect';
import { error } from 'next/dist/build/output/log';
import passport from './passport';
import { trustProxyMiddleware } from './trust-proxy-middleware';
export interface Request extends NextApiRequest {
// Passport adds these to the request object
logout: () => void;
logIn: (user: Express.User, done: (err?: any) => void) => void;
user?: Express.User;
protocol?: string;
}
const COOKIE_SECRET = process.env.COOKIE_SECRET;
/**
* Create an API route handler with next-connect and all the necessary middlewares
*
* @example
* ```ts
* export default handler().get((req, res) => { ... })
* ```
*/
function handler() {
if (!COOKIE_SECRET) throw new Error(`Please add COOKIE_SECRET to your .env.local file!`);
return (
nc<Request, NextApiResponse>({
onError: (err: any, _: any, res: NextApiResponse) => {
error(err);
res.status(500).end(err.toString());
},
})
// In order for authentication to work on Vercel, req.protocol needs to be set correctly.
// However, Vercel's and Netlify's reverse proxy setup breaks req.protocol, which the custom
// trustProxyMiddleware fixes again.
.use(trustProxyMiddleware)
.use(
cookieSession({
name: 'session',
keys: [COOKIE_SECRET],
maxAge: 24 * 60 * 60 * 1000 * 30,
// Do not change the lines below, they make cy.auth() work in e2e tests
secure: process.env.NODE_ENV !== 'development' && !process.env.INSECURE_AUTH,
signed: process.env.NODE_ENV !== 'development' && !process.env.INSECURE_AUTH,
}),
)
.use(passport.initialize())
.use(passport.session())
);
}
export default handler;
now returns this error: [{ "resource": "XXXX/server/api-route.ts", "owner": "typescript", "code": "2349", "severity": 8, "message": "This expression is not callable.\n Type 'typeof import("/XXXX/node_modules/next-connect/dist/types/index")' has no call signatures.", "source": "ts", "startLineNumber": 32, "startColumn": 3, "endLineNumber": 32, "endColumn": 5 }] Where i'm trying to use nc.
Used to work. I updated all packages and could fix all problems except for this one.