This example demonstrates how to implement a GraphQL server with an email-password-based authentication workflow based on Prisma & graphql-yoga.
The prisma
cli is the core component of your development workflow. prisma
should be installed as a global dependency, you can install this with npm install -g prisma
Clone the Prisma monorepo and navigate to this directory or download only this example with the following command:
curl https://codeload.github.com/prismagraphql/prisma/tar.gz/master | tar -xz --strip=2 prisma-master/examples/authentication
Next, navigate into the downloaded folder and install the NPM dependencies:
cd authentication
yarn install
You can now deploy the Prisma service (note that this requires you to have Docker installed on your machine - if that's not the case, follow the collapsed instructions below the code block):
# Ensure docker is running the server's dependencies
docker-compose up
# Deploy the server
cd prisma
prisma deploy
I don't have Docker installed on my machine
To deploy your service to a demo server (rather than locally with Docker), please follow this link.
yarn start
The easiest way to explore this deployed service and play with the API generated from the data model is by using the GraphQL Playground.
You can either start the desktop app via
yarn playground
Or you can open a Playground by navigating to http://localhost:4000 in your browser.
You can send the following mutation in the Playground to create a new User
node and at the same time retrieve an authentication token for it:
mutation {
signup(email: "[email protected]", password: "graphql") {
token
}
}
This mutation will log in an existing user by requesting a new authentication token for her:
mutation {
login(email: "[email protected]", password: "graphql") {
token
}
}
For this query, you need to make sure a valid authentication token is sent along with the Bearer
-prefix in the Authorization
header of the request. Inside the Playground, you can set HTTP headers in the bottom-left corner:
Once you've set the header, you can send the following query to check whether the token is valid:
{
me {
id
email
}
}
If the token is valid, the server will return the id
and email
of the User
node that it belongs to.