Add email and password login to your Graphcool Project 🎁
graphcool add-template graphcool/templates/auth/email-password
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.
The functions in authenticate.js
and signup.js
need access to a root token.
After you added the template to your service, you need to have (at least) one root token configured in your service.
If the list of rootTokens
in your graphcool.yml
is currently empty, add the following root token to it:
rootTokens
- authentication
Note that the name of the root token is actually not relevant. You can call it something else than
authentication
.
Finally, you need to apply all the changes you just made by deploying the service:
graphcool deploy
- Your app calls the Graphcool mutation
signupUser(email: String!, password: String!)
. - If no user exists yet that corresponds to the passed
email
, a newUser
node will be created with the password (after being hashed and salted). - If a user with the passed
email
exists, aUser
node is not created and an error is returned (sinceemail
has an@isUnique
requirement). - If a user is created, then the
signupUser(email: String!, password: String!)
mutation returns theid
as well as an authenticationtoken
for the new user.
- Your app calls the Graphcool mutation
authenticateUser(email: String!, password: String!)
. - If no user exists yet that corresponds to the passed
email
, or thepassword
does not match, an error will be returned. - If a user with the passed
email
exists and thepassword
matches, the mutation returns a valid token for the user. - Your app stores the token and uses it in its
Authorization
header for all further requests to Graphcool.
Go to the Graphcool Playground:
graphcool playground
Run this mutation to create a user:
mutation {
# replace __EMAIL__ and __PASSWORD__
signupUser(email: "__EMAIL__", password: "__PASSWORD__") {
id
token
}
}
and this to authenticate as that user:
mutation {
# replace __EMAIL__ and __PASSWORD__
authenticateUser(email: "__EMAIL__", password: "__PASSWORD__") {
token
}
}