This is TypeScript code that I wrote a couple of years ago:
import { expressjwt } from "express-jwt";
import { Request } from 'express';
class Authentication {
static loginRequired() {
expressjwt({
secret: process.env.SECRET,
credentialsRequired: true,
getToken: function fromHeaderOrQuerystring(req: Request) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
}
});
}
}
export { Authentication as auth };
I am getting an error at the getToken property:
Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>) => string | ParsedQs | (string | ParsedQs)[]' is not assignable to type 'TokenGetter'.
Type 'string | ParsedQs | (string | ParsedQs)[]' is not assignable to type 'string | Promise<string>'.
Type 'ParsedQs' is not assignable to type 'string | Promise<string>'.
Type 'ParsedQs' is missing the following properties from type 'Promise<string>': then, catch, finally, [Symbol.toStringTag]ts(2322)
index.d.ts(37, 5): The expected type comes from property 'getToken' which is declared here on type 'Params'
(property) getToken?: TokenGetter
Defines how to retrieves the token from the request object
When I wrote this code I was using Node 14.
return req.query.token as string?