Auth0 Rule to Create a Graphcool User and Generate a Node Token


After considerable help from @agartha and @nilan I would like to put out the Generate Graphcool Token Auth0 Rule. This Rule creates a new Graphcool user or adds a new Auth0 Connection to an existing user (if, for instance, the user logs in using Facebook after they’ve already logged in using Google) after they have successfully authenticated using Auth0. It also generates a Graphcool Node Token using the Graphcool System API and adds it to Auth0’s generated idToken during user authentication.

Note that this rule manages the user identity using their email address; this way the same user may use multiple Auth0 Connections to login, and this Rule keeps track of those “identities” within the Graphcool schema itself:

type User @model {
  id: ID! @isUnique
  identities: [Auth0Identity!]! @relation(name: "UserAuth0Identity")

type Auth0Identity @model {
  id: ID! @isUnique
  auth0UserId: String! @isUnique
  user: User @relation(name: "UserAuth0Identity")

The rule works by checking whether or not the Auth0 Connection used by the user to authenticate is already known to Graphcool. If it is, we generate a Graphcool Node Token and attach it to the Auth0 idtoken returned.

Otherwise, if this is the first time the user is authenticated using this Auth0 Connection, we first make sure that Auth0 Connection returned an email address. If not, we redirect him to a form to collect their email address and redirect back to the rule. This form must be created and hosted by your service.

Finally we continue the process of storing the Auth0 Identity by either adding it to an existing user if the email address was known or by creating a new user first.

The source code is extensively documented for more details.

This Rule requires the following Configuration Objects:

Note: I’m not sure if that’s still the case, but I remember that the maximum length of the Configuration Objects in Auth0 was 255. This is why the Graphcool Token above is split into 3 parts.


This is amazing, thanks!