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;
},
});
}
1 Reply
Deleting the jwt on the client side is not working