GithubHelp home page GithubHelp logo

lunchbreakdev / nexus-auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryands17/nexus-auth

0.0 0.0 0.0 6.3 MB

A sample auth module with Prisma 2 and Nexus

TypeScript 79.61% Dockerfile 2.51% JavaScript 9.88% JSONiq 2.44% Shell 5.56%

nexus-auth's Introduction

GraphQL Server with Authentication Prisma 2 and Nexus Schema

Build Status

This example shows how to implement a GraphQL server with an email-password-based auth, based on Prisma, apollo-server, graphql-shield & Nexus Schema via the Nexus Prisma plugin.

How to use

1. Clone this repo & install dependencies

Install Node dependencies:

yarn (recommended) or npm install

2. Set up the database

This uses a simple SQLite database.

Note: You can delete the migrations folder to create your own new migrations

To set up your database, run:

yarn db:save
yarn db:migrate

You can now use the SQLite Browser to view and edit your data in the ./prisma/dev.db file that was created when you ran yarn db:migrate.

3. Generate Prisma Client (type-safe database client)

Run the following command to generate Prisma Client:

yarn generate:prisma

Now you can seed your database using the seed script from package.json:

yarn seed

4. Start the GraphQL server

Launch your GraphQL server with this command:

yarn dev

Navigate to http://localhost:4002 in your browser to explore the API of your GraphQL server in a GraphQL Playground.

5. Using the GraphQL API

The schema that specifies the API operations of your GraphQL server is defined in ./src/generated/schema.graphql. Below are a number of operations that you can send to the API using the GraphQL Playground.

Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.

Retrieve all published posts and their authors

query {
  feed {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}
See more API operations

Register a new user

You can send the following mutation in the Playground to sign up a new user and retrieve an authentication token for them:

mutation {
  signup(name: "Alice", email: "[email protected]", password: "graphql") {
    token
  }
}

Log in an existing user

This mutation will log in an existing user by requesting a new authentication token for them:

mutation {
  login(email: "[email protected]", password: "graphql") {
    token
  }
}

Check 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:

{
  "Authorization": "Bearer __YOUR_TOKEN__"
}

With a real token, this looks similar to this:

{
  "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjanAydHJyczFmczE1MGEwM3kxaWl6c285IiwiaWF0IjoxNTQzNTA5NjY1fQ.Vx6ad6DuXA0FSQVyaIngOHYVzjKwbwq45flQslnqX04"
}

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
    name
    email
  }
}

6. Changing the GraphQL schema

To make changes to the GraphQL schema, you need to manipulate the files in the resolvers folder.

Next steps

Testing

Run yarn test or npm run test to run tests via Jest in the tests folder.

nexus-auth's People

Contributors

dependabot-preview[bot] avatar renovate-bot avatar ryands17 avatar dependabot[bot] avatar renovate[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.