Resolver Function for Authentication


#1

I was following the GraphCool tutorial on YouTube on resolver functions for a custom authentication:

Copying the code, i implemented the following schema-type:

  type EmailUser @model {
  id: ID! @isUnique
  createdAt: DateTime!
  updatedAt: DateTime!
  email: String! @isUnique
  password: String!
}

as well as the following “Resolver SDL”

type SignupEmailUserPayload {
  id: ID!
  token: String!
}

extend type Mutation {
  signupEmailUser(email: String!, password: String!): SignupEmailUserPayload
}

And “Inline Code”:

module.exports = event => {
  if (!event.context.graphcool.pat) {
    console.log('Please provide a valid root token')
    return { error: 'Email Signup not configured correctly'}
  }
  
  // Retrieve Payload from event
  const email = event.data.email
  const password = event.data.password
  
  // Create Graphcool API
  const graphcool = fromEvent(event)
  const api = graphcool.api('simple/v1')
  
  const SALT_ROUNDS = 10
  
  if (validator.isEmail(email)) {
    return graphcoolUser(api, email)
    .then(graphcoolUser => {
          if(!graphcoolUser) {
      return bcrypt.hash(password, SALT_ROUNDS)
      .then(hash => createGraphcoolUser(api, email, hash))
    } else {
      return Promise.reject("Email already in use")
    }
  })
  .then(graphcoolUserId => {
      return graphcool.generateAuthToken(graphcoolUserId, 'EmailUser')
      .then( token => {
        return { data: {id: graphcoolUserId, token}}
      })
    })
    .catch(error => {
      // Log error, but don't expose to caller
      console.log('Error: ${JSON.stringify(error)}')
    })
  } else {
    return {error: "Not a valid email" }
  }
}

But unfortunately, when when trying to run this resolver function in the playground (as “Admin”):

mutation resolverAuth {
  signupEmailUser(email: "test@gmail.com", password: "test") {
    id
    token
  }
}

I get the following error:

{
  "data": {
    "signupEmailUser": null
  },
  "errors": [
    {
      "locations": [
        {
          "line": 18,
          "column": 3
        }
      ],
      "path": [
        "signupEmailUser"
      ],
      "code": 5000,
      "message": "A function returned an unhandled error. Please check the logs for executionId 'eu-west-1:simple:cj9yszhnr54r00131i9q7rcip'",
      "requestId": "eu-west-1:simple:cj9yszhnr54r00131i9q7rcip"
    }
  ]
}

Given the cryptic error-message, i have no idea what’s going on. Any suggestions?


#2

What’s the output of graphcool logs --tail while you run the signup mutation?