I am facing an issue with ESLint where it says that the Request and Response from the first line are unused. However, if I remove them, ESLint then complains that req.headers.authorization does not exist. So, I imported them from functions and the intellisense worked, but now it is saying they are unused. How can I solve this issue? I want to mention that the type of this parameter in my function is a request, and it has the authorization property.
By the way, I am using a singleton pattern and this function will be called passing the req and res parameters inside a cloud function.
import { Request, Response } from 'firebase-functions';
import { auth } from 'firebase-admin';
const authInstance = auth();
export class Authenticator {
static instance: Authenticator;
private constructor() {}
static getInstance() {
if (Authenticator.instance === null) {
Authenticator.instance = new Authenticator();
}
return Authenticator.instance;
}
async authenticate(
req: Request,
res: Response,
log: boolean = false
): Promise<void> {
if (
!req.headers.authorization ||
!req.headers.authorization.startsWith('Bearer ')
) {
const response = {
code: 'auth/missing-argument',
message: 'Unauthorized Access',
};
res.status(401).json(response);
}
const token = req.headers.authorization.split('Bearer ')[1];
try {
const decodedToken = await authInstance.verifyIdToken(token);
if (log === true) {
const user = await authInstance.getUser(decodedToken.uid);
const logInfo = {
userId: decodedToken.uid,
user: user.displayName,
email: user.email,
timeGenerated: decodedToken.iat,
time: new Date().toDateString(),
};
console.log(logInfo);
}
} catch (error) {
console.log(error);
if (error.code === 'auth/argument-error') {
const response = {
code: error.code,
message:
'Something wrong with your TOKEN, please make sure that you passed the entire string in JWT format',
};
res.status(400).json(response);
} else if (error.code === 'auth/id-token-expired') {
const response = {
code: error.code,
message:
'Unauthorized access, your token expired. Get a fresh token from your client and try again',
};
res.status(401).json(response);
} else {
const response = {
code: error.code,
message:
'Internal Error, check your status code and contact support!',
};
res.status(500).json(response);
}
}
}
}