A simple Hello World application built with Fastify, PostgreSQL, and Drizzle ORM.
- Node.js (v18 or higher)
- PostgreSQL database running
- npm or yarn
-
Install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env
Then edit
.env
with your actual credentials:DATABASE_URL=postgresql://username:password@localhost:5432/reimbursement PORT=3000 HOST=0.0.0.0 # Google OAuth (see Google OAuth Setup below) GOOGLE_CLIENT_ID=your_google_client_id GOOGLE_CLIENT_SECRET=your_google_client_secret GOOGLE_REDIRECT_URI=http://localhost:3000/auth/google/callback SESSION_SECRET=your_super_secret_session_key ALLOWED_DOMAIN=blockful.io
-
Set up Google OAuth:
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API
- Go to "Credentials" → "Create Credentials" → "OAuth 2.0 Client IDs"
- Choose "Web application"
- Add authorized redirect URIs:
http://localhost:3000/auth/google/callback
- Copy the Client ID and Client Secret to your
.env
file
-
Generate and run database migrations:
npm run db:generate npm run db:push
-
Start the development server:
npm run dev
The server will start at http://localhost:3000
npm run dev
- Start development server with hot reloadnpm run build
- Build for productionnpm run start
- Start production servernpm run db:generate
- Generate database migrationsnpm run db:push
- Push schema changes to databasenpm run db:migrate
- Run database migrationsnpm run db:studio
- Open Drizzle Studio (database GUI)
Beautiful login page with Google OAuth button
Initiate Google OAuth login (redirects to Google)
Handle Google OAuth callback (internal use)
Logout current user
{
"message": "Logged out successfully",
"loginUrl": "/auth/google"
}
Get current user information (requires authentication)
{
"user": {
"id": 1,
"name": "John Doe",
"email": "john@blockful.io",
"avatar": "https://lh3.googleusercontent.com/...",
"lastLogin": "2024-01-01T12:00:00.000Z",
"createdAt": "2024-01-01T00:00:00.000Z"
}
}
Hello World endpoint (shows user info if logged in)
{
"message": "Hello World from Fastify + Drizzle + Google OAuth!",
"user": {
"name": "John Doe",
"email": "john@blockful.io",
"avatar": "https://lh3.googleusercontent.com/..."
},
"loginUrl": null
}
Protected dashboard (requires authentication)
{
"message": "Welcome to your dashboard!",
"user": { ... },
"stats": {
"totalUsers": 5
},
"actions": [
{ "label": "View Profile", "url": "/auth/me" },
{ "label": "View Users", "url": "/users" },
{ "label": "Logout", "url": "/auth/logout" }
]
}
Get all users
{
"users": [
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
]
}
Get all users
{
"users": [
{
"id": 1,
"name": "John Doe",
"email": "john@blockful.io",
"googleId": "google_user_id",
"avatar": "https://lh3.googleusercontent.com/...",
"isActive": true,
"lastLogin": "2024-01-01T12:00:00.000Z",
"createdAt": "2024-01-01T00:00:00.000Z",
"updatedAt": "2024-01-01T00:00:00.000Z"
}
]
}
Create a new user (requires authentication)
curl -X POST http://localhost:3000/users \
-H "Content-Type: application/json" \
-H "Cookie: sessionId=your_session_cookie" \
-d '{"name": "John Doe", "email": "john@blockful.io"}'
- Fastify - Fast and efficient web framework
- PostgreSQL - Robust relational database
- Drizzle ORM - TypeScript ORM with excellent developer experience
- TypeScript - Type safety and better development experience
- Google OAuth 2.0 - Secure authentication with domain restriction
- Session Management - Secure cookie-based sessions
- Domain Restriction: Only
@blockful.io
email addresses allowed - Secure Sessions: HTTP-only, secure cookies with CSRF protection
- OAuth 2.0: Industry-standard authentication flow
- Route Protection: Middleware-based authentication for sensitive endpoints
- User Management: Track login times and user activity
src/
├── auth/
│ ├── middleware.ts # Authentication middleware
│ ├── routes.ts # Auth routes (/login, /callback, /logout)
│ └── utils.ts # Domain validation, user creation
├── db/
│ ├── schema.ts # Database schema definitions
│ └── index.ts # Database connection setup
├── server.ts # Main application server
public/
└── login.html # Beautiful login page
- Visit the app: http://localhost:3000
- Login: Click "Continue with Google" or visit http://localhost:3000/login
- Dashboard: After login, visit http://localhost:3000/dashboard
- API: Use authenticated endpoints with your session cookie
Note: Only @blockful.io
email addresses are allowed. Update ALLOWED_DOMAIN
in .env
to change this restriction.