Skip to content

roylee17/typescript-tweet

Repository files navigation

Tweet Backend

Description

Practice TypeScript, GraphQL, TypeORM with Nest.

Nest framework TypeScript starter repository.

Architecture

Architecture

Design

Authentication

  • The auth module implements JWT authentication.
  • We handle the authentication at the "transportation layer", which is decoupled from the GraphQL "APIs".
  • The Auth Module supports Sign Up and Sign In over REST APIs.
  • Every time a user signs in, he/she gets the accessToken which valid for and hour.
  • The user uses the accessToken to access the tweet module for both REST endpoints and the GraphQL endpoint.

Quick Start

Download and install Docker Desktop

Start PostgreSQL with docker-compose

docker-compose up -d

Setup the database

psql -h 127.0.0.1 -U postgres -c 'DROP DATABASE IF EXISTS tweet'

psql -h 127.0.0.1 -U postgres -c 'CREATE DATABASE tweet'

Install node and dependencies

npm install

Start the app

npm run start

Test

We have a few tooling

  1. Postman
  2. curl
  3. GraphQL Playground http://localhost:3000/graphql

I've created a postman collection for both Auth Module, and Tweet Module We can use it to test both REST and GraphQL APIs.

The other option is to get the JWT using curl, and test with GraphQL Playground

Test with Postman

  1. Import Postman collection

Import Postman collection

  1. Config environment variable for the url http://localhost:3000

Config environment variable for host url

  1. Sign up and then sign in. This will get an accessToken from the server, and saved for other APIs.

Get access token

Sign up and sign in an user, and save its accessToken

Sign up an user.

curl -X POST 'http://localhost:3000/auth/signup' \
-H 'Content-Type: application/json' \
-d '{
    "username": "[email protected]",
    "password": "O1X0l0bEPtXShuT"
}'

Sign in as the user

curl -X POST 'http://localhost:3000/auth/signin' \
-H 'Content-Type: application/json' \
-d '{
    "username": "[email protected]",
    "password": "O1X0l0bEPtXShuT"
}'

Response:

{
    "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkpvZXNwaDFAZXhhbXBsZS5vcmciLCJpYXQiOjE2MTk1MDIyMTUsImV4cCI6MTYxOTU4ODYxNX0.nsHLRrZlmVtoifNMaBJHm8HKTsMTJJpeN3M46pNi5pM"
}

Open the GraphQL Playground, and set the token in the HTTP header

GraphQL Playground

License

Nest is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published