In this blog, we will implement Google Authentication in an Express.js app using:
- TypeScript
- Express.js
- Mongoose
- Passport.js
- JWT Authentication
Project Structure
└── src/
├── config/
├── controllers/
├── middlewares/
├── models/
├── routes/
├── utils/
├── app.ts
└── index.ts
├── .env
├── package.json
└── tsconfig.json
Install Required Packages
npm install passport passport-google-oauth20
npm install jsonwebtoken cookie-parser bcrypt mongoose
npm install -D @types/passport-google-oauth20
Create Google OAuth Credentials
Go to
2. Configure OAuth Consent Screen
5. Copy Client ID and Client Secret
http://localhost:3000/api/v1/auth/google
If configured correctly:
- Google login opens
- User account gets created
- JWT cookies are generated
Common Issues
Invalid Redirect URI
Ensure callback URL matches exactly in:
- Google Console
.env- Passport Strategy
Missing Email
Make sure scope contains:
scope: ["profile", "email"]
Production Tips
Use secure cookies in production:
secure: true,
httpOnly: true,
sameSite: "strict",
Final Thoughts
You now have Google Authentication working with:
- Express.js
- TypeScript
- Mongoose
- Passport.js
- JWT
You can further extend this with:
- GitHub OAuth
- Discord OAuth
- Role-based auth
- Email verification
Happy coding 🚀
SOCIAL SHARE CARD GENERATOR