This is an example project showcasing JWT authentication using Google account. Implemented using Node.js, Express, PostgreSQL, Prisma and JWT.
Here is the repo with authentication client - authentication-client-react
Before running the project, ensure you have the following prerequisites installed on your system:
- Node.js (version 14 or higher)
- PostgreSQL
Create a PostgreSQL database. Setup env variables. Check .env.example
Follow the steps below to set up and run the project:
-
Clone the repository:
git clone <repository_url>
-
Navigate to the project directory:
cd node-authentication
-
Install dependencies:
npm i
-
Generate the Prisma client:
npx prisma generate
-
Run database migrations:
npx prisma migrate dev
-
Run database migrations:
npx prisma migrate dev
-
Run server:
npm run dev
The following endpoints are available:
POST /auth/login/google
- Login user. Creates new user in DB if doesn't exist. Requires providing Google id token in Authorization header. Set refresh token in cookie, sends access token in response body.POST /auth/verify
- Verifies access token. Requires providing access token in the Authorization header.POST /auth/refresh
- Sends new access token in response body. Requires providing refresh token in cookies.POST /auth/logout
- Logout, clears auth cookies.GET /ping
- Public route.GET /data
- Get protected data. Requires a valid JWT access token in the Authorization header.