Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql middleware auth don't worked. #2

Open
shoha-kh opened this issue Feb 2, 2020 · 1 comment
Open

graphql middleware auth don't worked. #2

shoha-kh opened this issue Feb 2, 2020 · 1 comment

Comments

@shoha-kh
Copy link

shoha-kh commented Feb 2, 2020

Hi, I have a problem with the connection authorization after you connect the easygraphql-format-error.
The problem is that, I give information about authorization in req due to the fact that I connected the plugin in context, it stopped being defined.

isAuth.js:

const jwt = require("jsonwebtoken");

module.exports = (req, res, next) => {
  const authHeader = req.get("Authorization");
  if (!authHeader) {
    req.isAuth = false;
    return next();
  }
  const token = authHeader.split(" ")[1];
  if (!token || token === "") {
    req.isAuth = false;
    return next();
  }
  let decodedToken;
  try {
    decodedToken = jwt.verify(token, "somesupersecretkey");
  } catch (err) {
    req.isAuth = false;
    return next();
  }
  if (!decodedToken) {
    req.isAuth = false;
    return next();
  }
  req.isAuth = true;
  req.userId = decodedToken.userId;
  next();
};

app.js:

app.use(isAuth);

app.use("/", (req, res) => {
  console.log("req", req),
    graphqlHttp({
      schema: schema,
      rootValue: graphQlResolvers,
      graphiql: true,
      context: { errorName },
      formatError: err => {
        return formatError.getError(err);
      }
    })(req, res);
});
@shoha-kh
Copy link
Author

shoha-kh commented Feb 2, 2020

I apologize in advance for my English...
I solved this problem like this:
context: { errorName, isAuth: req.isAuth, userId: req.userId }

Perhaps someone has a better solution? Advance thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant