This project is a full-stack authentication template, providing a robust backend built with NestJS and a dynamic frontend developed with React. It features user registration, login, and Google OAuth for seamless authentication. The entire application is containerized using Docker Compose, making it easy to set up and run in various environments, with PostgreSQL serving as the database.
- User Authentication: Secure user registration and login functionalities.
- Google OAuth: Integrate Google for social login, enhancing user experience.
- JWT-based Authentication: Secure API endpoints using JSON Web Tokens.
- NestJS Backend: A progressive Node.js framework for building efficient and scalable server-side applications.
- React Frontend: A modern JavaScript library for building user interfaces.
- PostgreSQL Database: A powerful, open-source object-relational database system.
- Docker Compose: Easy setup and management of multi-container Docker applications for local development.
- Framework: NestJS
- Language: TypeScript
- Authentication: Passport.js (JWT, Google OAuth2)
- Database ORM: Prisma
- Database: PostgreSQL
- Framework: React
- Language: TypeScript
- Styling: Tailwind CSS, Shadcn UI
- Routing: React Router DOM
- Containerization: Docker, Docker Compose
Before you begin, ensure you have the following installed on your system:
Follow these steps to get your development environment running:
-
Clone the repository:
git clone https://github.com/D-Seonay/my-auth-template.git cd my-auth-template -
Configure Environment Variables:
- Navigate to the
backenddirectory:cd backend - Copy the
.env.examplefile to.env:cp .env.example .env
- Open
backend/.envand fill in your specific environment variables, especially for Google OAuth. Refer to the "Google OAuth Configuration" section below.
- Navigate to the
-
Build and Run with Docker Compose:
- Navigate back to the project root directory:
cd .. - Build and start the Docker containers:
docker-compose up --build
This command will:
- Build Docker images for both the backend and frontend.
- Start the PostgreSQL database, backend, and frontend services.
- Run Prisma migrations to set up your database schema.
- Navigate back to the project root directory:
To enable Google OAuth, you need to create a Google Cloud Project and obtain API credentials.
- Create a Google Cloud Project: Go to the Google Cloud Console and create a new project.
- Enable the Google People API: In the Cloud Console, search for "Google People API" and enable it.
- Create OAuth 2.0 Client IDs:
- Go to "APIs & Services" > "Credentials".
- Click "CREATE CREDENTIALS" and select "OAuth client ID".
- Choose "Web application".
- Authorized JavaScript origins: Add
http://localhost:5173(your frontend URL). - Authorized redirect URIs: Add
http://localhost:3000/auth/google/callback(your backend callback URL). - This will give you a Client ID and Client Secret.
- Update
backend/.env: Open thebackend/.envfile and set the following variables with your obtained credentials:GOOGLE_CLIENT_ID=YOUR_GOOGLE_CLIENT_ID GOOGLE_CLIENT_SECRET=YOUR_GOOGLE_CLIENT_SECRET GOOGLE_CALLBACK_URL=http://localhost:3000/auth/google/callback OAUTH_SUCCESS_REDIRECT_URL=http://localhost:5173/oauth/success
Once the services are up and running:
- Frontend: Access the React application at
http://localhost:5173 - Backend API: The NestJS API will be available at
http://localhost:3000.
You can now register new users, log in with email/password, or use the "Continuer avec Google" button on the login page.
backend/: Contains the NestJS API application.src/: Backend source code.prisma/: Prisma schema and migrations.
frontend/: Contains the React client application.src/: Frontend source code.components/: Reusable React components.pages/: React page components.
docker-compose.yml: Defines the services, networks, and volumes for the Dockerized application.k8s/: Contains the Kubernetes configuration files.
To deploy this application to a Kubernetes cluster, follow these steps.
Before deploying to Kubernetes, you need to build the Docker images for the frontend and backend and push them to a container registry (e.g., Docker Hub, Google Container Registry, Amazon ECR).
-
Backend Image:
docker build -t your-registry/your-backend-image:latest ./backend docker push your-registry/your-backend-image:latest
-
Frontend Image:
docker build -t your-registry/your-frontend-image:latest ./frontend docker push your-registry/your-frontend-image:latest
Replace
your-registry/your-backend-image:latestandyour-registry/your-frontend-image:latestwith your actual image names.
The Kubernetes configuration uses secrets to manage sensitive information. You need to provide these secrets before deploying.
-
Encode Your Secrets: The
k8s/secrets.ymlfile requires base64 encoded values. You can encode your secrets using the following command:echo -n 'your-secret-value' | base64
You will need to do this for:
- PostgreSQL password
- JWT secret
- Google Client ID
- Google Client Secret
-
Update
k8s/secrets.yml: Replace the placeholder values ink8s/secrets.ymlwith your base64 encoded secrets.
-
Image Names:
- In
k8s/backend.yml, replaceyour-docker-registry/your-backend-image:latestwith your backend image URI. - In
k8s/frontend.yml, replaceyour-docker-registry/your-frontend-image:latestwith your frontend image URI.
- In
-
Domain Name:
- In
k8s/frontend.yml, replace<YOUR_DOMAIN>in theIngressresource with the domain you will use to access the application. - In
k8s/configmaps.yml, replace<YOUR_DOMAIN>in theOAUTH_SUCCESS_REDIRECT_URL.
- In
Once you have configured your images and secrets, you can deploy the application using kubectl.
kubectl apply -k k8s/This command will:
- Create all the necessary Kubernetes resources defined in the
k8sdirectory. - Deploy the PostgreSQL database, backend, and frontend to your cluster.
After the deployment is complete and the pods are running, you can access your application at the domain you configured in the frontend Ingress resource.
To check the status of your pods, you can run:
kubectl get podsTo view the logs of a specific pod:
kubectl logs <pod-name>Feel free to fork the repository, open issues, and submit pull requests.
This project is licensed under the MIT License.