Skip to content

Open-Source user management system template. Built with TS, ExpressJS, Apollo GraphQL, & PrismaORM

Notifications You must be signed in to change notification settings

usmansbk/userbase-api-server-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Userbase

dotenv-vault

User Identity And Management System API Server Template

Features

  • Join/Leave waiting list
  • Login with identity provider (Google)
  • Refresh token rotation
  • Role Based Access Control
  • Register with email and Password
  • Login with email
  • Verify email address
  • Verify phone number
  • Login with verified phone number SMS OTP
  • Login with verified email OTP
  • Reset password
  • Logout from all devices
  • Delete user account
  • Update user profile
  • User profile picture
  • WebSocket via GraphQL Subscriptions
  • Brute-Force login protection
  • User Management

Prerequisites

Getting Started

Environment Variables

  • Existing project
npx dotenv-vault login
npx dotenv-vault pull
  • New project
cp .env.example .env

3rd-Party Services

  • Set your AWS_REGION, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY variables.
  • Create an S3 bucket to store documents (images, files, etc) and set your AWS_S3_BUCKET env variable.
  • Follow AWS Serverless Image Handler instructions to create a CDN and set your CLOUDFRONT_API_ENDPOINT env variable.
  • Create a Dynamodb table for in-app notifications and set your AWS_DYNAMODB_DELTA_TABLE.
  • Setup SES for Email (Ensure you have this AWS IAM Policy) and add your SENDER_EMAIL to the env variables.
  • Setup SNS for SMS.
  • Create a sentry project and add the SENTRY_DSN to your environment file

Google Authentication

Build

yarn docker:build

Run

yarn docker:start

Stop

yarn docker:stop

Test

yarn test

Database Migrations

yarn sh
yarn db:migrate

Create Default Application, Owner, Admin Role, & Permissions

In development

yarn sh
yarn init:dev

In production

yarn init:app

Development

Clients

Main technologies

GraphQL

Run codegen after modifying the graphql schema to generate TypeScript definitions

yarn codegen

File Upload

We store information about uploaded files in the File table within the database. To ensure the deletion of S3 objects when an associated file row is removed, it is crucial to use the Prisma delete method. This is because our Prisma client is hooked to delete any associated file objects in S3.

Example:

// DONT: This will not delete the picture in s3
prisma.update({
  where: {},
  data: {
    picture: {
      delete: true,
    },
  },
});

// DO: this will delete the file row and corresponding object in s3
await prisma.file.delete({
  where: {
    key: "...",
    bucket: "...",
  },
});

Error Handling

We use "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.

Deployment

Secrets

Begin by creating your project's env vault and authenticating against it.

npx dotenv-vault new
npx dotenv-vault login

Push development `.env`` file securely

npx dotenv-vault push

Open the production environment to edit the production variables

npx dotenv-vault open production

Build your project's encrypted .env.vault file

npx dotenv-vault build

Fetch your production decryption key (will be used in the next step)

npx dotenv-vault keys production

CI/CD

  • Generate and copy an access token for your DockerHub account

  • Add the access token to your repo actions secrets as DOCKER_HUB_ACCESS_TOKEN

  • Add your dockerhub username to your repo actions secrets as DOCKER_HUB_USERNAME

  • Add your dotenv-vault decryption key to your repo actions secrets as DOTENV_KEY

Database
  • Create a new PostgreSQL database

  • Add the External Database URL to your repo actions secrets as DATABASE_URL

  • Add the Internal Database URL to your vault's production enviroment as DATABASE_URL

Cache
  • Create a new Redis Cache

  • Add the Internal Redis URL to your vault's prodction environment as REDIS_URL

Deploy
  • Build your project's encrypted .env.vault file
npx dotenv-vault build
  • Commit and push changes to trigger action

  • Wait for CI build to finish successfully

  • Create a new Web Service using the new image from dockerhub. (You may need to add your DockerHub access token to render for private images)

  • Click the Advanced button and add your DOTENV_KEY environment variable

VSCode Extensions

Readings

About

Open-Source user management system template. Built with TS, ExpressJS, Apollo GraphQL, & PrismaORM

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published