Abudi
Abudi
TTCTheo's Typesafe Cult
Created by Abudi on 4/11/2024 in #questions
API Monitoring, Logging from Scratch
Hello folks I have an assignment where I want to develop a rest monitoring tool where that tracks the status code, the method, response body, request body, cookies and tokens and every other important parameter whenever I send a request to a server How can I do this actually?
2 replies
TTCTheo's Typesafe Cult
Created by Abudi on 3/19/2023 in #questions
HELP HELP HELP HELPThis is the code it is nextjs with apollo graphql
this is a graphql directive If the cookie exists then I will send data otherwise I will delete the jwt
function authDirectiveTransform(schema: GraphQLSchema, directiveName: string) {
return mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: fieldConfig => {
const authDirective = getDirective(
schema,
fieldConfig,
directiveName
)?.[0];

if (authDirective) {
const { resolve = defaultFieldResolver } = fieldConfig;

fieldConfig.resolve = async (source, args, context, info) => {
const { req, res }: { req: NextApiRequest; res: NextApiResponse } =
context;
const cookies = parse(req.headers.cookie || '');
const cookie = cookies["admin_token"] || cookies["token"] || "";
if (!cookie) {
res.setHeader(
"Set-Cookie",
["token=; path=/; expires=0", "admin_token=; path=/; expires=0"]
)
res.status(401).send("")

}
if (cookies['admin_token'] || cookies['token']) {
const index = await getFrequency();
const cookieSecret = `${index}-day`;
if (cookie) {
try {
verify(cookie, cookieSecret)
} catch (error) {
console.error(error)
res.setHeader(
"Set-Cookie",
["token=; path=/; expires=0", "admin_token=; path=/; expires=0"]
).status(401).send("");
return await resolve(source, args, context, info);
}
}
}

return await resolve(source, args, context, info);
};
}

return fieldConfig;
},
});
}
function authDirectiveTransform(schema: GraphQLSchema, directiveName: string) {
return mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: fieldConfig => {
const authDirective = getDirective(
schema,
fieldConfig,
directiveName
)?.[0];

if (authDirective) {
const { resolve = defaultFieldResolver } = fieldConfig;

fieldConfig.resolve = async (source, args, context, info) => {
const { req, res }: { req: NextApiRequest; res: NextApiResponse } =
context;
const cookies = parse(req.headers.cookie || '');
const cookie = cookies["admin_token"] || cookies["token"] || "";
if (!cookie) {
res.setHeader(
"Set-Cookie",
["token=; path=/; expires=0", "admin_token=; path=/; expires=0"]
)
res.status(401).send("")

}
if (cookies['admin_token'] || cookies['token']) {
const index = await getFrequency();
const cookieSecret = `${index}-day`;
if (cookie) {
try {
verify(cookie, cookieSecret)
} catch (error) {
console.error(error)
res.setHeader(
"Set-Cookie",
["token=; path=/; expires=0", "admin_token=; path=/; expires=0"]
).status(401).send("");
return await resolve(source, args, context, info);
}
}
}

return await resolve(source, args, context, info);
};
}

return fieldConfig;
},
});
}
2 replies