NestJS GraphQL Social Auth implementation supporting multiple social providers.
- Description
- Features
- Architecture
- Features
- GraphQL API specification
- Env schema
- Prerequisites
- Installation
- Tests
- License
The project provides the baseline configuration for NestJS GraphQL social authentication. The architecture can easily be extended to support different OAuth providers as the social auth is implemented based on Passport.js library utilizing a strategy design pattern.
Supported providers:
A database social profile entry is created after the user authenticates with the social provider. If the user does not have an account, a new one is created, and the social profile gets attached to that account. After social authentication, the user continues with the default (JWT) authentication strategy. A user can connect his account with multiple social providers.
- Multiple social authentication providers
- JWT authentication
- GraphQL API
- Persistence in PostgreSQL
- NestJS
- Graphql
- Apollo
- Passport
- Jest
- Supertest
- PostgreSQL
- Github Actions
- Codecov
The application follows a feature-first module structure resulting in a clean separation of boundaries between modules. The GraphQL API uses union return types instead of Apollo errors to indicate an alternative flow of action. This decision results in well-documented errors, one source of truth (the GraphQL schema) and better TypeScript support. It also allows the user to indicate which error fields he wants to receive. The error handling was modelled after the GraphQL Conf presentation.
root
โโโ common (shared logic)
โโโ config (configuration)
โโโ graphql (graphql decorators, interfaces and responses)
โโโ feature modules
โโโ input (input dto)
โโโ responses (response dto)
โโโ results (unions of responses and errors)
โโโ entity (database entities)
โโโ service (business logic)
โโโ resolver (graphql resolver)
โโโ repository (database repository)
Social authentication utilizes a strategy design pattern that allows for quick implementation of additional providers.
strategy
โโโ facebook.strategy
โโโ google.strategy
โโโ jwt.strategy
Graphql API specification is available at http://localhost:8000/graphql. The server has to be up and running in for the documentation to be available. Alternatively, a schema.graphql file is provided in the root directory.
Please provide an .env file in the root directory that conforms to the following JSON schema.
{
"type": "object",
"properties": {
"NODE_ENV": {
"type": "string",
"default": "development",
"enum": [
"development",
"production",
"test",
"provision"
]
},
"PORT": {
"type": "number",
"default": 8000
},
"DB_USER": {
"type": "string"
},
"DB_PASSWORD": {
"type": "string"
},
"DB_PORT": {
"type": "number",
"default": 5432
},
"DB_DEV": {
"type": "string"
},
"DB_TEST": {
"type": "string"
},
"JWT_SECRET": {
"type": "string"
},
"JWT_EXPIRES_IN": {
"type": "string"
},
"FACEBOOK_ID": {
"type": "string"
},
"FACEBOOK_SECRET": {
"type": "string"
},
"GOOGLE_ID": {
"type": "string"
},
"GOOGLE_SECRET": {
"type": "string"
}
},
"required": [
"DB_USER",
"DB_PASSWORD",
"DB_DEV",
"DB_TEST"
]
}
Install (node)[https://nodejs.org/en], (npm)[https://www.npmjs.com]. You should be able to run the following commands.
node --version
npm --version
Install docker and docker-compose. You should be able to run the following commands.
docker --version
docker-compose --version
Run the following commands before proceeding to the sections below.
docker-compose --env-file ./.env -f ./docker/docker-compose.dev.yml up -d
or
make setup-dev
cd backend
npm install
npm run start
In order to manually run tests, follow the instructions below.
cd backend
npm run test
cd backend
npm run test:e2e
This project is licensed under the MIT License - see the LICENSE.md file for details.