Add Facebook Authentication to your Graphcool project 🎁
graphcool add-template graphcool/templates/auth/facebook
The add-template
command is performing three major steps:
- Download the source files from the
src
directory and put them into your service'ssrc
directory (into a subdirectory calledemail-password
). - Download the contents from
graphcool.yml
and append them as comments to your service'sgraphcool.yml
. - Download the contents from
types.graphql
and 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.
Finally, you need to install the node dependencies and apply all the changes you just made by deploying the service:
npm install
graphcool deploy
To use Facebook Login you need to create a Facebook app and add the Facebook Login
product. Follow this guide to create an app in a few minutes.
Once you created a new APP, add the and copy its App ID. Replace __APP_ID__
in login.html
with your App ID.
Note: if your API Version is something else than
v2.9
, you also need to update that inlogin.html
.
Add http://localhost:8000
and http://localhost:8000/login.html
to your valid OAuth URIs of the Facebook Login
product:
To create a test Facebook Token, run login.html
, for example using Python's SimpleHTTPServer
:
python -m SimpleHTTPServer
Open http://localhost:8000/login.html
in your browser and use the login button to create a new Facebook Token.
By default you have access to the public_profile
permission that includes information about the user such as name, age range and location. If you want to access the users email you need to additionally ask for the email permission, more information here. This is already done in login.html
First, obtain a valid Facebook token with the small app in login.html
as mentioned above.
Go to the Graphcool Playground:
graphcool playground
Run this mutation to authenticate a user:
mutation {
# replace __FACEBOOK_TOKEN__!
authenticateUser(facebookToken: "__FACEBOOK_TOKEN__") {
token
}
}
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 the same Facebook token will not add a new user.