GraphQL Cookbook

Logging in an existing user through the Parse GraphQL API

Problem

You want to log in an existing user in your backend through the Parse GraphQL API.

Solution

Using the Parse GraphQL API, you can log in an existing user just by sending the user’s credentials through the logIn mutation. The username and password arguments are mandatory. The mutation will return back all users’s fields, including the sessionToken.

After logging in an existing user, you can use the authenticating an user recipe to send the sessionToken in the following operations so they will be executed in the behavior of this user. You can also use the logging out recipe to destroy the sessionToken.

Version Information

Depending on the version of Parse you choose to run, the GraphQL queries, mutations and results will be slightly different.
Please choose the correct example along with the Parse version you are running.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mutation LogIn{
  logIn(input: {
    username: "somefolk"
    password: "somepassword"
  }){
    viewer{
      user{
        id
        createdAt
        updatedAt
        username
      }
      sessionToken
    }
  }
}
Result Parse 3.10.0 and later:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "data": {
    "logIn": {
      "viewer": {
        "user": {
          "id": "X1VzZXI6UHNOUkJ3Y1YyRQ==",
          "createdAt": "2020-02-06T13:38:04.517Z",
          "updatedAt": "2020-02-06T13:38:04.517Z",
          "username": "somefolk"
        },
        "sessionToken": "r:a5318d28821a78069f5b618de35b57bb"
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
mutation LogIn{
  logIn(fields:{
    username: "somefolk"
    password: "somepassword"
  }){
    id,
    createdAt,
    updatedAt,
    username,
    sessionToken
  }
}
Result Parse 3.9.0:
1
2
3
4
5
6
7
8
{
  "data": {
    "viewer": {
      "sessionToken": "r:1450d329038f876835fb7aac16742380",
      "username": "somefolk"
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
mutation LogIn{
  logIn(fields:{
    username: "somefolk"
    password: "somepassword"
  }){
    objectId,
    createdAt,
    updatedAt,
    username,
    sessionToken
  }
}
Result Parse 3.8.0:
1
2
3
4
5
6
7
8
9
10
11
{
  "data": {
    "logIn": {
      "objectId": "KTznKVzto2",
      "createdAt": "2019-11-04T14:23:46.014Z",
      "updatedAt": "2019-11-04T14:23:46.014Z",
      "username": "somefolk",
      "sessionToken": "r:fe39d9de406d53d13e9af1efbbe967a8"
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
mutation LogIn {
  users {
    logIn(username: "somefolk", password: "somepassword") {
      objectId,
      createdAt,
      updatedAt,
      username,
      sessionToken
    }
  }
}
Result Parse 3.7.2:
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "data": {
    "users": {
      "logIn": {
        "objectId": "NyU1lNlhPd",
        "createdAt": "2019-07-29T09:09:58.222Z",
        "updatedAt": "2019-07-29T09:09:58.222Z",
        "username": "somefolk",
        "sessionToken": "r:cbca71d29d7601761b48ed01bbe9638d"
      }
    }
  }
}