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

signup "message": "the key 'message' was not present #14

Open
productdevbook opened this issue May 19, 2020 · 4 comments
Open

signup "message": "the key 'message' was not present #14

productdevbook opened this issue May 19, 2020 · 4 comments

Comments

@productdevbook
Copy link

productdevbook commented May 19, 2020

const fetch = require("node-fetch");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");

const HASURA_OPERATION = `
mutation ($username: String!, $password: String!) {
  insert_user_one(object: {
    username: $username,
    password: $password,
  }) {
    id
  }
}
`;

// execute the parent mutation in Hasura
const execute = async (variables, reqHeaders) => {
  const fetchResponse = await fetch("http://localhost:8080/v1/graphql", {
    method: "POST",
    headers: reqHeaders || {},
    body: JSON.stringify({
      query: HASURA_OPERATION,
      variables,
    }),
  });
  return await fetchResponse.json();
};

// Request Handler
const handler = async (req, res) => {
  // get request input
  const { username, password } = req.body.input;

  // run some business logic
  const salt = bcrypt.genSaltSync();
  let hashedPassword = await bcrypt.hash(password, salt);

  // execute the Hasura operation
  const { data, errors } = await execute(
    { username, password: hashedPassword },
    req.headers
  );

  // if Hasura operation errors, then throw error
  if (errors) {
    return res.status(400).json({
      message: errors.message,
    });
  }

  const tokenContents = {
    sub: data.insert_user_one.id.toString(),
    name: username,
    iat: Date.now() / 1000,
    "https://hasura.io/jwt/claims": {
      "x-hasura-allowed-roles": ["user", "anonymous"],
      "x-hasura-user-id": data.insert_user_one.id.toString(),
      "x-hasura-default-role": "user",
      "x-hasura-role": "user",
    },
    exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60,
  };

  const token = jwt.sign(tokenContents, process.env.AUTH_PUBLIC_KEY);

  // success
  return res.json({
    ...data.insert_user_one,
    token: token,
  });
};

module.exports = handler;

Screen Shot 2020-05-19 at 22 40 20

@alipetarian
Copy link

@mkalayci35 Error is an array. Please try this.

if (errors) { return res.json({ message: errors[0].message, code: 400 }) }

@alipetarian
Copy link

@alipetarian Please set status 400 as well

@natnaelmb1
Copy link

I faced this issue when I directed my Action's handler to my handler running on my localhost. Deploying my handler worked for me. Hope it helps.

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

4 participants