Add Auth0 Authentication to your Graphcool project 🎁
From your server directory (e.g. if you were to just have run graphcool init server your "service directory" would be ./server) run:
graphcool add-template graphcool/templates/auth/auth0The add-template command is performing three major steps:
- Download the source files from the
srcdirectory and put them into your service'ssrcdirectory (into a subdirectory calledauth0). - Download the contents from
graphcool.ymland append them as comments to your service'sgraphcool.yml. - Download the contents from
types.graphqland append them as comments to your service'stypes.graphql.
In order for the changes to take effect, you need to manually uncomment all the lines that have been added by the add-template command.
- Create a new Auth0 account or log-in to your existing one
- Create a new API. Select
RS256as the signing algorithm. Keep the Identifier value handy as we will use it later on.
- Create a new Auth0 client with the following settings
- Client Type - Regular Web Application
- Token Endpoint Authentication Method - Post
- Allowed Callback URLs -
http://localhost:8080/
- In Advanced Settings Section - OAuth tab set JWT Signature Algorithm to value RS256 and turn on the OIDC Conformant swith.
- In Advanced Settings Section - Grant Types tab only leave the Implicit grant type enabled.
Make sure the AUTH0_DOMAIN and AUTH0_API_IDENTIFIER environment variables are properly set before running the command below. The AUTH0_API_IDENTIFIER should match the identifier set in 3.1 Create an API
Finally, you need to install the node dependencies and apply all the changes you just made by deploying the service:
npm install
graphcool deploy- In order to setup Auth0 Lock Widget replace
__AUTH0_DOMAIN__,__AUTH0_CLIENT_ID__and__AUTH0_API_IDENTIFIER__inexample/index.jswith the credentials from your API/Client settings. - Serve the test application locally on port 8080. For example :
npm i -g http-server
cd example
http-server- Open
http://localhost:8080/in a browser and authenticate with the Auth0 Widget. - Copy the mutation displayed on the page.
Go to the Graphcool Playground:
graphcool playgroundPaste the mutation copied from above
You should see that a new user has been created. The returned token can be used to authenticate requests to your Graphcool API as that user. Note that running the mutation again with a new Auth0 token for the same user will not add a new user.





