1

I'm working on the Drupal decoupled project, where the client is VueJs and the backend is Drupal 9. I'm using Drupal's contrib module GraphQL 8.x-3.1 for the interaction between client and server. I'm new to GraphQL.

I created a GraphQL query to validate the user during login. Below is the query,

query{
  userQuery(filter: {
    conjunction: AND, 
    groups: [{conditions: [
      {operator: EQUAL, field: "name", value: "editor.user"},
      {operator: EQUAL, field: "status", value: "1"},
    ]}]}) {
    count
    entities {
        ... on User {
        uid
        name
        pass {
          value
          existing
          preHashed
        }
        mail
      }
    }
  }
}

The query is displaying the pass field as null.

{
  "data": {
    "userQuery": {
      "count": 1,
      "entities": [
        {
          "uid": 15,
          "name": "editor.user",
          "pass": null,
          "mail": "[email protected]"
        }
      ]
    }
  }
}

Am I doing any mistake to query?

Also, is it the right way to validate the user?

Appreciate your suggestion.

5
  • 2
    Why would the query return the password? That sounds like a bad idea. The login should be handled server side. Maybe this helps a bit: gist.github.com/sebas5384/ab0da24e6091f96a6cced2cb6fb0e069 (Taken from this discussion in the drupal-graphql issue queue.
    – berliner
    Dec 2, 2021 at 21:34
  • 2
    Yes you'll need to use a GraphQL mutation to login, otherwise possible post to /user/login?_format=json The resulting session cookie can be used to authenticate subsequent requests. You can also query currentUserContext after that, with the auth cookie, to get the user details. Dec 3, 2021 at 0:37
  • @berliner Thank you for the suggestion. The above query was just to test whether it works or not with the provided parameter. But it returns 'pass' value 'null' . I had also looked into the link you provided, it shows an error when I implemented it.
    – npcoder
    Dec 3, 2021 at 14:56
  • @DavidThomas: Thank you for the suggestion. As per GraphQL documentation, the mutation is used for the CRUD operation. Could you please elaborate more?
    – npcoder
    Dec 3, 2021 at 15:00
  • I finally figure out, the login validation using could be implemented using JWT:drupal.org/project/jwt, a Drupal contributed module. Also, have to install another module, 'Get JWT on Login' (drupal.org/project/getjwtonlogin) which provided a JWT token to the end-user.
    – npcoder
    Dec 6, 2021 at 22:01

1 Answer 1

2
+25

Drupal saves hashed password in database and it is a one way encryption, which means you can not get original password from it, during authentication process drupal encode the input password by user and checks if the encoded pass equals the one in the database and when met this condition, in a monolithic project (not decoupled) it creates a cookie with information about the user and sends it back to the browser which will be stored and used for future uses.

But when developing a decoupled approach like using GraphQL or JsonAPI or Rest API you cant always use cookie cause the front end application might not support it (like an android mobile app or ...) so in this case it is better to use Tokens for authentication , there are some modules which one of the most popular is Simple OAuth (OAuth2) & OpenID Connect (STABLE AND WIDLY USED)

after setting this module up (installing using composer ,creating private and public key and introducing consumers) you can simply get a token by making a Post request to:

{SiteName}/oauth/token

and in the body which should be of type multipart/form-data (application/json is not supported) you can have:

grant_type = password
client_id = CONSUMER ID YOU CAN BUILD THROGH UI
client_secret = CONSUMER PASSWORD (IF YOU HAVE SET)
username = USERNAME
password = PASSWORD
scope = ROLES YOU WANT USER HAVE WHICH IS NOT DEFIEND IN CONSUMER (WILL NOT AFFECT IF USER DOES NOT HAVE THAT ROLE, SO NO SECURITY CONCERNS)

and that's it, you will get an access and refresh token on response. which then can be used in future requests calling GraphQL or any other REST services.

One important notice about this module is about the usage of Scope, and that is when introducing a client you can assign one or more scopes to that client and when a user logins through that client will have all the roles assign to that client

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.