Skip to content

Commit

Permalink
feat(auth): add facebook sign in/up
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed Apr 11, 2024
1 parent a028194 commit 5f8ea3e
Show file tree
Hide file tree
Showing 39 changed files with 849 additions and 224 deletions.
35 changes: 32 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Git
/.git
/.gitignore

# GitHub
/.github
/.husky
/.vscode

# Build
/dist
/dist-lint
/localstack
/node_modules
/coverage

# DevTools
/.husky
/.vscode
/localstack
/.czrc
/.editorconfig
/.eslintrc.js
/.lintstagedrc
/.nvmrc
/jest.config.json
/LICENSE
/redocly.yaml

# Env
.env.*
!.env.docker
!/prisma/.env

# Deploy
/appspec.yml

# Unnecessary
/openapi
/scripts
/testes
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ AWS_REGION=us-east-1
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=

PASETO_PRIVATE_KEY=2MPoBMMJwdnHwx5qIso9RcxR5o3SycCgBgWFeHCE2Oz6uI3sCLGOQLqwdmRAPFmU28UPMc9FGuncPy3tpKq+bg==

DATABASE_URL=postgresql://username:password@postgres:5432/database?schema=public
6 changes: 5 additions & 1 deletion .github/workflows/api-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Create .env file
uses: SpicyPizza/[email protected]
Expand All @@ -33,7 +35,9 @@ jobs:
envkey_AWS_REGION: ${{ secrets.AWS_REGION }}
envkey_GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
envkey_GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
envkey_JWT_SECRET: ${{ secrets.JWT_SECRET }}
envkey_FACEBOOK_CLIENT_ID: ${{ secrets.FACEBOOK_CLIENT_ID }}
envkey_FACEBOOK_CLIENT_SECRET: ${{ secrets.FACEBOOK_CLIENT_SECRET }}
envkey_PASETO_PRIVATE_KEY: ${{ secrets.PASETO_PRIVATE_KEY }}
envkey_DATABASE_URL: ${{ secrets.DATABASE_URL }}

- name: Build
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/dbdocs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install prisma & dbdocs
run: pnpm add -D dbdocs prisma prisma-dbml-generator
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/dbdocs-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install prisma & dbdocs
run: pnpm add --ignore-scripts -D dbdocs prisma prisma-dbml-generator
Expand Down
12 changes: 10 additions & 2 deletions .github/workflows/openapi-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install redocly
run: pnpm add --ignore-scripts -D @redocly/cli

- name: Validate openapi
run: pnpm run lint:openapi

- name: GitHub Action
- name: Build openapi
run: pnpm run openapi:bundle

- name: Deploy to readme
uses: readmeio/rdme@v8
with:
rdme: docs ./openapi/bundle.yaml --key=${{ secrets.README_API_KEY }} --version=1.0
rdme: openapi ./openapi/bundle.yaml --key=${{ secrets.README_API_KEY }} --version=1.0
2 changes: 2 additions & 0 deletions .github/workflows/openapi-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install redocly
run: pnpm add --ignore-scripts -D @redocly/cli
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/prisma-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install prisma
run: pnpm add --ignore-scripts -D prisma
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
node-version-file: .nvmrc

- uses: pnpm/action-setup@v3
with:
version: 8

- name: Install dependencies
run: pnpm install --ignore-scripts
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lerna-debug.log*
.env
.env.*
!.env.example
!/prisma/.env

# Temp
/tmp
Expand Down
13 changes: 4 additions & 9 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
FROM node:20

WORKDIR /app

COPY ./package.json ./package.json
COPY ./pnpm-lock.yaml ./pnpm-lock.yaml

RUN pnpm i --ignore-scripts

ADD ./prisma ./prisma

RUN pnpm run prepare
RUN npm i --ignore-scripts --no-package-lock

ADD ./ ./

CMD pnpm run start:docker
RUN npm run db:prisma

CMD npm run start:docker
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ This project use lot's of tools to be as efficient as possible, here's the list
- [API](https://econominhas.readme.io/reference/)
- [Database](https://dbdocs.io/henriqueleite42/Econominhas?view=relationships)

### Running the API for the first time

1. Copy and paste <code>.env.example</code> and rename the copy to <code>.env.docker</code>
2. Run <code>pnpm run start:db</code>
3. Open another console tab and run `pnpm run db:gen-migration <MIGRATION_NAME>`
4. The API will be available at http://localhost:3000/v1

## Useful commands

| Command | Description |
Expand Down Expand Up @@ -209,14 +216,4 @@ This phase is were we convert the documentation to code and make everything work
</tbody>
</table>

### Running the API for the first time

1. Copy and paste <code>.env.example</code> and rename the copy to <code>.env</code>
2. Run <code>pnpm run start:dev</code>
3. Run econominhas-api migrations
- Step 2.1: List id container Run<code>docker ps</code>
- Step 2.2: Run <code>docker exec -it \<container id\> sh</code>
- Step 2.3: Run <code>pnpm run db:migrate</code>
4. Available at http://localhost:3000/v1

<br/>
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ services:
dockerfile: Dockerfile.dev
image: econominhas-api
container_name: econominhas-api
command: pnpm run start:docker
command: npm run start:docker
depends_on:
- postgres
- localstack
ports:
- 3000:3000
env_file:
- .env
- .env.docker
volumes:
- ./:/app/
networks:
Expand Down
21 changes: 10 additions & 11 deletions localstack/buckets.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env bash
bucket1="las-musas-public"
bucket2="las-musas-private"
bucket1="econominhas-public"
bucket2="econominhas-private"

##buckets verification
if awslocal s3 ls "s3://$bucket1" 2>/dev/null; then
echo "[INFO] $bucket1 bucket already exists"
else
awslocal s3 mb s3://$bucket1
echo "[INFO] $bucket1 created"
echo "[INFO] $bucket1 bucket already exists"
else
awslocal s3 mb s3://$bucket1
echo "[INFO] $bucket1 created"
fi

if awslocal s3 ls "s3://$bucket2" 2>/dev/null; then
echo "[INFO] $bucket2 bucket already exists"
else
awslocal s3 mb s3://$bucket2
echo "[INFO] $bucket2 created"
echo "[INFO] $bucket2 bucket already exists"
else
awslocal s3 mb s3://$bucket2
echo "[INFO] $bucket2 created"
fi
2 changes: 2 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ servers:
paths:
/auth/google:
$ref: paths/auth/google.yaml
/auth/facebook:
$ref: paths/auth/facebook.yaml
/auth/email:
$ref: paths/auth/email.yaml
/auth/phone:
Expand Down
15 changes: 15 additions & 0 deletions openapi/paths/auth/facebook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
post:
tags:
- Auth
summary: Sign In/Up with Facebook
description: |
Sign Ins or Sign Ups a user using a facebook account
operationId: auth-facebook
requestBody:
content:
application/json:
schema:
$ref: ../../components/schemas/auth-with-external-provider.yaml
required: true
responses:
$ref: ../../components/responses/auth.yaml
2 changes: 1 addition & 1 deletion openapi/paths/auth/google.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ post:
content:
application/json:
schema:
$ref: ../../components/schemas/auth-with-3rd-party.yaml
$ref: ../../components/schemas/auth-with-external-provider.yaml
required: true
responses:
$ref: ../../components/responses/auth.yaml
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"type": "git",
"url": "git+https://github.com/econominhas/backend.git"
},
"packageManager": "^[email protected]",
"engines": {
"node": ">=20"
},
Expand Down Expand Up @@ -81,24 +80,24 @@
"webpack-cli": "^5.1.4"
},
"scripts": {
"prepare": "husky install && pnpm run db:prisma",
"db:prisma": "pnpm run lint:prisma && prisma generate --generator client",
"db:docs": "pnpm run lint:prisma && prisma generate --generator dbml && dbdocs build ./prisma/schema.dbml",
"db:gen-migration": "pnpm run lint:prisma && prisma migrate dev --name",
"prepare": "husky install && npm run db:prisma",
"db:prisma": "npm run lint:prisma && prisma generate --generator client",
"db:docs": "npm run lint:prisma && prisma generate --generator dbml && dbdocs build ./prisma/schema.dbml",
"db:gen-migration": "npm run lint:prisma && prisma migrate dev --name",
"db:migrate": "prisma migrate deploy",
"openapi:serve": "redocly preview-docs",
"openapi:bundle": "pnpm run lint:openapi && redocly bundle -o openapi/bundle.yaml",
"openapi:postman": "pnpm run openapi:bundle && openapi2postmanv2 -s openapi/bundle.yaml -o openapi/postman.json -O folderStrategy=Tags,requestParametersResolution=Example",
"openapi:bundle": "npm run lint:openapi && redocly bundle -o openapi/bundle.yaml",
"openapi:postman": "npm run openapi:bundle && openapi2postmanv2 -s openapi/bundle.yaml -o openapi/postman.json -O folderStrategy=Tags,requestParametersResolution=Example",
"build": "./scripts/ci-cd/build.sh",
"release": "standard-version",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:docker": "nest start --watch",
"start:dev": "docker compose up",
"start:dev": "docker compose up --build",
"start:db": "docker compose up postgres",
"start:prod": "node main",
"clean:docker": "docker container rm econominhas-api && docker image rm econominhas-api",
"lint:all": "pnpm run lint:ts && pnpm run lint:code && pnpm run lint:prisma && pnpm run lint:openapi",
"lint:all": "npm run lint:ts && npm run lint:code && npm run lint:prisma && npm run lint:openapi",
"lint:ts": "tsc --project tsconfig.lint.json",
"lint:code": "eslint \"src/**/*.ts\" --fix --quiet",
"lint:prisma": "prisma-case-format --file prisma/schema.prisma --map-table-case snake,plural --map-field-case snake --map-enum-case snake -p",
Expand Down
1 change: 1 addition & 0 deletions prisma/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=postgresql://username:password@localhost:5432/database?schema=public
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- AlterEnum
ALTER TYPE "sign_in_provider_enum" ADD VALUE 'FACEBOOK';

-- AlterTable
ALTER TABLE "sign_in_providers" ALTER COLUMN "access_token" SET DATA TYPE VARCHAR(300),
ALTER COLUMN "refresh_token" DROP NOT NULL,
ALTER COLUMN "refresh_token" SET DATA TYPE VARCHAR(300);
5 changes: 3 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ datasource db {
// **********************************

enum SignInProviderEnum {
FACEBOOK
GOOGLE
@@map("sign_in_provider_enum")
Expand Down Expand Up @@ -64,8 +65,8 @@ model SignInProvider {
accountId String @map("account_id") @db.Char(16)
provider SignInProviderEnum
providerId String @map("provider_id") @db.VarChar(50)
accessToken String @map("access_token") @db.VarChar(250)
refreshToken String @map("refresh_token") @db.VarChar(250)
accessToken String @map("access_token") @db.VarChar(300)
refreshToken String? @map("refresh_token") @db.VarChar(300)
expiresAt DateTime @map("expires_at")
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
Expand Down
10 changes: 3 additions & 7 deletions src/adapters/google.ts → src/adapters/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface ExchangeCodeInput {
export interface ExchangeCodeOutput {
scopes: Array<string>;
accessToken: string;
refreshToken: string;
refreshToken?: string;
expiresAt: Date;
}

Expand All @@ -17,12 +17,8 @@ export interface GetAuthenticatedUserDataOutput {
isEmailVerified: boolean;
}

export abstract class GoogleAdapter {
readonly requiredScopes = [
"https://www.googleapis.com/auth/userinfo.profile",
"openid",
"https://www.googleapis.com/auth/userinfo.email",
];
export abstract class AuthProviderAdapter {
abstract requiredScopes: Array<string>;

abstract exchangeCode(i: ExchangeCodeInput): Promise<ExchangeCodeOutput>;

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Account } from "@prisma/client";

export const EMAIL_TEMPLATES = {
MAGIC_LINK_LOGIN: {
from: "",
from: "[email protected]",
title: "",
body: "",
},
Expand Down
Loading

0 comments on commit 5f8ea3e

Please sign in to comment.