Coder Social home page Coder Social logo

wenox / socialauth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nalhin/socialauth

0.0 0.0 0.0 456 KB

๐Ÿ”’ NestJS GraphQL Social Auth implementation supporting multiple social providers

License: MIT License

Shell 0.68% JavaScript 1.50% TypeScript 97.69% Makefile 0.12%

socialauth's Introduction

Test Codecov CodeFactor License

Social Auth

NestJS GraphQL Social Auth implementation supporting multiple social providers.

Table of contents

Description

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:

  • Google
  • Facebook

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.

Features

  • Multiple social authentication providers
  • JWT authentication
  • GraphQL API
  • Persistence in PostgreSQL

Technology stack

Backend

  • NestJS
  • Graphql
  • Apollo
  • Passport
  • Jest
  • Supertest
  • PostgreSQL

CI/CD

  • Github Actions
  • Codecov

Architecture

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.

Folder structure

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

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

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.

Env schema

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"
  ]
}

Prerequisites

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

Installation

Run the following commands before proceeding to the sections below.

Setup database

docker-compose --env-file ./.env --f ./docker/docker-compose.dev.yml up -d

or

make setup-dev

Setup backend

cd backend
npm install
npm run start

Tests

In order to manually run tests, follow the instructions below.

Unit

cd backend
npm run test

E2E

cd backend
npm run test:e2e

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

socialauth's People

Contributors

nalhin avatar dependabot[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.