{"id":15452049,"url":"https://github.com/usmansbk/sequelize-graphql-auth-service","last_synced_at":"2025-04-19T23:55:22.900Z","repository":{"id":37599194,"uuid":"436939165","full_name":"usmansbk/sequelize-graphql-auth-service","owner":"usmansbk","description":"A production-ready GraphQL API authentication/user management sub-graph. Built with Sequelize, Apollo GraphQL, \u0026 Express","archived":false,"fork":false,"pushed_at":"2022-08-07T08:15:18.000Z","size":3484,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2025-04-19T23:55:16.807Z","etag":null,"topics":["apollo-server","apollo-server-express","aws-cloudfront","aws-s3","aws-ses","dotenv","expressjs","graphql","graphql-server","jwt","orm","postgresql","sequelizejs"],"latest_commit_sha":null,"homepage":"https://sequelize-graphql-server.netlify.app","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/usmansbk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-10T10:30:22.000Z","updated_at":"2023-09-01T20:04:41.000Z","dependencies_parsed_at":"2022-07-10T02:46:03.432Z","dependency_job_id":null,"html_url":"https://github.com/usmansbk/sequelize-graphql-auth-service","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmansbk%2Fsequelize-graphql-auth-service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmansbk%2Fsequelize-graphql-auth-service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmansbk%2Fsequelize-graphql-auth-service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/usmansbk%2Fsequelize-graphql-auth-service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/usmansbk","download_url":"https://codeload.github.com/usmansbk/sequelize-graphql-auth-service/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249830852,"owners_count":21331357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apollo-server","apollo-server-express","aws-cloudfront","aws-s3","aws-ses","dotenv","expressjs","graphql","graphql-server","jwt","orm","postgresql","sequelizejs"],"created_at":"2024-10-01T21:41:01.286Z","updated_at":"2025-04-19T23:55:22.878Z","avatar_url":"https://github.com/usmansbk.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Auth Service\n\n[![usmansbk](https://circleci.com/gh/usmansbk/sequelize-graphql-auth-service.svg?style=svg)](https://app.circleci.com/pipelines/github/usmansbk/sequelize-graphql-server) [![dotenv-vault](https://badge.dotenv.org/works-with.svg?r=1)](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1)\n\nAuthentication service/subgraph optimized for Sequelize ORM\n\n## Features\n\n- [x] User Management\n- [x] RBAC\n- [x] Email authentication\n- [x] Social authentication (Google and Facebook)\n- [x] SMS OTP\n- [x] Multi Client\n- [x] i18n\n- [x] Dockerize\n- [x] Analytics\n- [x] CI/CD\n\n## Built with\n\n- [GraphQL](https://graphql.org/)\n- [Apollo Server](https://www.apollographql.com/docs/apollo-server/)\n- [SequelizeORM](https://sequelize.org/master/manual/migrations.html)\n- [ExpressJS](https://expressjs.com/)\n- [Dotenv Vault](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1)\n\n## Prerequisite\n\n- [Docker](https://docs.docker.com/)\n\n## Run\n\n- Create a **.env** file\n\ncopy `.env.example` file as `.env`\n\n```sh\ncp .env.example .env\n```\n\nor pull one from dotenv-vault, if your team has one\n\n```sh\nnpx dotenv-vault@latest pull --dotenvMe=YOUR-TEAM-DOTENV_ME\n```\n\n- Build docker image\n\n```sh\nyarn docker:build\n```\n\n- Start container\n\n```sh\nyarn docker:start -d\n```\n\n## Open Shell\n\n```sh\nyarn docker:cli\n```\n\n## Initialize database\n\n- Create a root user\n\n```sh\nnpx babel-node src/scripts/createRoot\n```\n\n- Create client\n\n  - You must pass the `client_id` in their request headers.\n\n```sh\nnpx babel-node src/scripts/createApp\n```\n\n- List existing clients\n\n```sh\nnpx babel-node src/scripts/listApp\n```\n\n- Seed database (optional)\n\n```sh\nyarn seed\n```\n\n## Test\n\n- Create a `.env.test` file\n\n```sh\ncp .env.example .env.test\n```\n\n- Create Test database:\n\n```sh\nyarn createdb:test\n```\n\n- Run tests\n\n```sh\nyarn test\n```\n\n## Build\n\n- Build production image\n\n```sh\ndocker compose -t usmansbk/simple-server:release . -f Dockerfile.production\n```\n\n- Push to Docker Hub\n\n```sh\ndocker push usmansbk/auth-service:release\n```\n\n## [Mailer](https://nodemailer.com/transports/ses/)\n\nThe server makes use of AWS SES to send emails. Setup your SES account and add the following environment variables. Verify your development email and ensure you have this [AWS IAM Policy](https://nodemailer.com/transports/ses/#example-3).\n\n```sh\nMAIL_FROM=sender@example.com\n\nAWS_ACCESS_KEY_ID=\nAWS_SECRET_ACCESS_KEY=\nAWS_REGION=us-east-1\n```\n\nCheck the [email-templates](https://github.com/forwardemail/email-templates) docs on how to design email templates.\n\n## [SMS](https://www.twilio.com/docs/sms/quickstart/node)\n\nFollow the official Twilio documentation to setup your Twilio account and add your `TWILIO_ACCOUNT_SID`, `TWILIO_AUTH_TOKEN`, and `TWILIO_PHONE_NUMBER` to your `.env` file.\n\n## Google authentication\n\n- Create a [Firebase](https://console.firebase.google.com) project if you don't have one.\n\n- Go to **APIs \u0026 Auth** \u003e **Credentials** in the [Google Developers Console](https://console.cloud.google.com/) and copy your **OAuth 2.0 Client IDs Web Client** `GOOGLE_CLIENT_ID` and `GOOGLE_SECRET_KEY`.\n\n- Generate an _OAuth2 API v2_ id token from Google [0Auth 2.0 Playground](https://developers.google.com/oauthplayground/) to test.\n\n## Facebook authentication\n\n- Create a new [Facebook](https://developers.facebook.com/) app\n- Get your [`FACEBOOK_APP_ACCESS_TOKEN`](https://developers.facebook.com/tools/access_token/) and `FACEBOOK_APP_ID` env variables\n- Navigate to **Roles** ⟶ **Test Users** to get a test account access tokens\n\n## File upload (S3)\n\nWe upload files via `REST` endpoints. [Why not File Upload mutation?](https://www.apollographql.com/blog/backend/file-uploads/file-upload-best-practices/)\n\nTo set up your `S3` for file storage:\n\n- Add your `AWS_S3_BUCKET` to `env` file\n- Ensure you've set the full s3 permissions\n\n## Images CDN\n\nFollow these [instructions](https://aws.amazon.com/solutions/implementations/serverless-image-handler/) to get your `CLOUDFRONT_API_ENDPOINT`. We use [Amazon CloudFront](https://aws.amazon.com/cloudfront/) to provide a caching layer to reduce the cost of image process and the latency of subsequent image delivery. The CloudFront domain name provides cached access to the image handler API.\n\n## Filtering \u0026 Pagination\n\n### Filtering\n\nFor a more complex filtering, we mimic the sequelize filter query. In order to filter by associations, we assume all associations are aliased (using the `as` option). This alias must have corresponding field in your graphql type. Example:\n\nIf you define a User `has-many` Task relationship like so,\n\n```js\nUser.hasMany(Task, { as: \"tasks\" });\n```\n\nyou must define a `tasks` field in your graphql `User` type schema\n\n```gql\ntype User {\n  tasks(filter: TaskFilter): TaskList!\n}\n```\n\nRefer to the sequelize docs for more info on [Operators](https://sequelize.org/docs/v6/core-concepts/model-querying-basics/#operators)\n\n### Pagination\n\nOur cursor-based pagination must adhere to a `List` interface. This is similar to the relay-connection pagination. But unlike relay, we return our `items` as a flat list.\n\n```gql\n# Example\ntype TaskList implements List {\n  items: [Task]!\n  totalCount: Int!\n  pageInfo: PageInfo!\n}\n```\n\n### N+1 Problem\n\nWe eager-load requested fields that have a matching association `alias` in their corresponding model. Example: If we have a User `has-one` Picture relationship:\n\n```js\nUser.hasOne(Picture, { as: \"avatar\" });\n```\n\n```gql\n# This will eager-load the `avatar` association. Both user and avatar will be fetched in a single SQL query\nquery {\n  user {\n    id\n    name\n    avatar {\n      url\n    }\n  }\n}\n```\n\n#### Edge-cases:\n\n- Eager-loading only works with `Query`. `Mutation` isn't supported\n\n- Nested cursor-paginated fields aren't eager-loaded, and hard to maintain in the frontend.\n\n- Paginated fields should be added to the root `Query` for the reason above.\n\n## Analytics\n\n[Segment](https://segment.com/docs/) allows us to collect data with different analytics tools. To setup our analytics, create a Segment account and add your `SEGMENT_WRITE_KEY` to the `.env` file.\n\n## Error handling\n\nWe use [\"wrapping exceptions\"](https://javascript.info/custom-errors#wrapping-exceptions) technique to handle client generated errors. This allows us to take full control of the kind of errors we return, and easily translate them before sending to the end-users.\n\nInternal server errors are logged to sentry. Create a [Sentry](https://sentry.io) account and add your `SENTRY_DSN` to the `.env` file.\n\n## Coding standard\n\nWe use Eslint AirBnB coding guidelines and import alias. All aliases are prefixed with a `~`. To add a new alias, update the `jsconfig.json`, `.eslintrc.js`, and `babel.config.json` files. We also make use of Husky precommit hook to enforce standard.\n\nModel specific logic should be moved to their associated data sources, and resolver errors should be handled using [Wrapping Exception](https://javascript.info/custom-errors) technique.\n\n## Readings\n\n- [GraphQL Schema Design: Building Evolvable Schemas](https://www.apollographql.com/blog/backend/schema-design/graphql-building-evolvable-schemas/)\n\n- [Apollo Server File Upload Best Practices](https://www.apollographql.com/blog/backend/file-uploads/file-upload-best-practices/)\n\n- [Designing a GraphQL server for optimal performance](https://blog.logrocket.com/designing-graphql-server-optimal-performance/)\n\n- [GraphQL Cursors Connections Specification](https://relay.dev/graphql/connections.htm)\n\n- [TDD, Where Did It All Go Wrong - Ian Cooper](https://www.youtube.com/watch?v=EZ05e7EMOLM\u0026list=TLPQMjIwMTIwMjJnzh0h4NGjEg\u0026index=2)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmansbk%2Fsequelize-graphql-auth-service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fusmansbk%2Fsequelize-graphql-auth-service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fusmansbk%2Fsequelize-graphql-auth-service/lists"}