Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikolasburk/auth-extension
https://github.com/nikolasburk/auth-extension
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/nikolasburk/auth-extension
- Owner: nikolasburk
- Created: 2018-03-09T18:19:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-03-09T18:19:35.000Z (over 6 years ago)
- Last Synced: 2024-10-02T17:21:06.470Z (about 1 month ago)
- Language: JavaScript
- Size: 51.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Authentication
This example demonstrates how to implement a GraphQL server with an **email-password-based authentication** workflow based on Prisma & [`graphql-yoga`](https://github.com/graphcool/graphql-yoga).
## Get started
> **Note**: `prisma` is listed as a _development dependency_ and _script_ in this project's [`package.json`](./package.json). This means you can invoke the Prisma CLI without having it globally installed on your machine (by prefixing it with `yarn`), e.g. `yarn prisma deploy` or `yarn prisma playground`. If you have the Prisma CLI installed globally (which you can do with `npm install -g prisma`), you can omit the `yarn` prefix.
### 1. Download the example & install dependencies
Clone the Prisma monorepo and navigate to this directory or download _only_ this example with the following command:
```sh
curl https://codeload.github.com/graphcool/prisma/tar.gz/master | tar -xz --strip=2 prisma-master/examples/auth
```Next, navigate into the downloaded folder and install the NPM dependencies:
```sh
cd auth
yarn install
```### 2. Deploy the Prisma database service
You can now [deploy](https://www.prismagraphql.com/docs/reference/cli-command-reference/database-service/prisma-deploy-kee1iedaov) the Prisma service (note that this requires you to have [Docker](https://www.docker.com) installed on your machine - if that's not the case, follow the collapsed instructions below the code block):
```sh
yarn prisma deploy
```I don't have Docker installed on my machine
To deploy your service to a public cluster (rather than locally with Docker), you need to perform the following steps:
1. Remove the `cluster` property from `prisma.yml`
1. Run `yarn prisma deploy`
1. When prompted by the CLI, select a public cluster (e.g. `prisma-eu1` or `prisma-us1`)
1. Replace the [`endpoint`](./src/index.js#L23) in `index.js` with the HTTP endpoint that was printed after the previous command### 3. Start the GraphQL server
The Prisma database service that's backing your GraphQL server is now available. This means you can now start the server:
```sh
yarn start
```The server is now running on [http://localhost:4000](http://localhost:4000).
## Testing the API
The easiest way to test the deployed service is by using a [GraphQL Playground](https://github.com/graphcool/graphql-playground).
### Open a Playground
You can either start the [desktop app](https://github.com/graphcool/graphql-playground) via
```sh
yarn playground
```Or you can open a Playground by navigating to [http://localhost:4000](http://localhost:4000) in your browser.
> **Note**: You can also invoke the `yarn dev` script (instead of `yarn start`) which starts the server _and_ opens a Playground in parallel. This will also give you access to the Prisma API directly.
#### Register a new user with the `signup` mutation
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:
```graphql
mutation {
signup(email: "[email protected]" password: "graphql") {
token
}
}
```#### Logging in an existing user with the `login` mutation
This mutation will log in an _existing_ user by requesting a new authentication token for her:
```graphql
mutation {
login(email: "[email protected]" password: "graphql") {
token
}
}
```#### Checking whether a user is currently logged in with the `me` query
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:
![](https://imgur.com/bEGUtO0.png)
Once you've set the header, you can send the following query to check whether the token is valid:
```graphql
{
me {
id
}
}
```If the token is valid, the server will return the `id` and `email` of the `User` node that it belongs to.
## Troubleshooting
I'm getting the error message
[Network error]: FetchError: request to http://localhost:4466/auth-example/dev failed, reason: connect ECONNREFUSED
when trying to send a query or mutationThis is because the endpoint for the Prisma service is hardcoded in [`index.js`](index.js#L23). The service is assumed to be running on the default port for a local cluster: `http://localhost:4466`. Apparently, your local cluster is using a different port.
You now have two options:
1. Figure out the port of your local cluster and adjust it in `index.js`. You can look it up in `~/.prisma/config.yml`.
1. Deploy the service to a public cluster. Expand the `I don't have Docker installed on my machine`-section in step 2 for instructions.Either way, you need to adjust the `endpoint` that's passed to the `Prisma` constructor in `index.js` so it reflects the actual cluster domain and service endpoint.