Access/Refresh Token Authentication Demo with Node.js And Typescript
Watch the YouTube Tutorial
Backend: Node.js, Typescript, MongoDB, Kubernetes
Frontend: Next.js, Typescript
DevOps: Docker, Terraform, Kubernetes, Google Cloud
Requirements: Docker, Docker Compose, Yarn, Node.js
Optional: Terraform, Google Cloud SDK
Setup
make setup- Create GitHub OAuth app here
- Set "Homepage URL" to
http://localhost:3000 - Set "Authorization callback URL" to
http://localhost:3000/github - Set
GITHUB_CLIENT_IDin.env.development - Set
NEXT_PUBLIC_GITHUB_CLIENT_IDinclient/.env.development - "Generate a new client secret"
- Set
GITHUB_CLIENT_SECRETin.env.development
- Set "Homepage URL" to
Development
make client(Start Next.js development server, http://localhost:3000)make backend(Start development backend services)
Services
clientNext.js client (web application)apiNode.js server (http api)realtimeNode.js server (websocket server)sharedTypescript lib (shared code)infrastructureTerraform (configurations to deploy application in the cloud)
Commands
make init-infrastructure(Initialize Terraform)make apply(Apply cloud resources, ~15 minutes)make deploy(Build and deploy services to Kubernetes cluster)make destroy(Destroy cloud resources)make client-image(Build and push client Docker image)make api-image(Build and push api Docker image)make realtime-image(Build and push Docker image)
Step by step guide
- Configure variables in
Makefile - Sign into your project
gcloud auth application-default login - Create the bucket for storing Terraform state (
make create-terraform-bucket) - Set
terraform.backend.bucketin infrastructure/main.tf toTF_BUCKET - Insert secrets into Google Cloud Secret Manager
- GitHub OAuth (create here)
- Set "Authorization callback URL" to
https://api.${DOMAIN}/github github_client_idgithub_client_secret
- Set "Authorization callback URL" to
- Secrets (generate here)
refresh_token_secretaccess_token_secret
- MongoDB Atlas (create a free cluster here)
- Navigate to Project Settings -> Access Manager -> API Keys -> Create API Key -> Select Project Owner
mongodbatlas_public_keymongodbatlas_private_keyatlas_project_id(found under Settings)
- GitHub OAuth (create here)
- Add
0.0.0.0/0to your MongoDB Atlas project "IP Access List" (Under Network Access) - Enable Container Registry in your GCP console
make init-infrastructuremake deploy- Go to Google Cloud DNS,
main-zone, copy theNSrecord to your domain registrar - Uncomment code block in infrastructure/modules/kubernetes-cert-manager/main.tf and run
make apply - The app should be live at
https://${DOMAIN}
- Every time you want to deploy changes, just run
make deploy