This project demonstrates a practical implementation of the OAuth2 authorization code flow using GitHub as the provider, session management using Postgres, all built with Hono.js. This provides a solid foundation for creating secure web applications that authenticate users via third-party services.
- OAuth2 Authorization Code Flow: Securely authenticates users using GitHub OAuth2.
- Session Management: Stores user session data in a Postgres database.
- Hono.js: Uses the lightweight and efficient Hono.js framework for routing and handling requests.
- Environment Configuration: Utilizes environment variables for sensitive information.
Before getting started, ensure you have the following installed:
- Node.js (v18 or higher): https://nodejs.org
- npm (or yarn/pnpm): Comes with Node.js.
- Postgres: https://www.postgresql.org/download/
- GitHub Account: You'll need a GitHub account to register your OAuth application.
-
Clone the repository:
git clone https://github.com/aksbuzz/github-oauth.git cd github-oauth -
Install Dependencies:
npm install
-
Create a .env file: Copy the contents of
.env.exampleto.envand fill in your values:GITHUB_CLIENT_ID=<your_github_client_id> GITHUB_CLIENT_SECRET=<your_github_client_secret> POSTGRES_HOST=localhost POSTGRES_USER=your_db_user POSTGRES_PASSWORD=your_db_password POSTGRES_DB=your_db_name
GITHUB_CLIENT_ID: Get this from your GitHub OAuth application settings.GITHUB_CLIENT_SECRET: Get this from your GitHub OAuth application settings.POSTGRES_*: Your Postgres database credentials.
-
Configure GitHub OAuth Application:
- Go to https://github.com/settings/developers and register a new OAuth application.
- Set the Homepage URL to
http://localhost:3000. - Set the Authorization callback URL to
http://localhost:3000/login/github/callback. - Copy the Client ID and Client Secret and paste them into your
.envfile.
-
Run schema.sql file This will create the required
sessionanduserstable in your postgres database.
npm run dev # starts the dev server with hot reloadingYou can also use docker-compose to run the app
docker compose -f "docker-compose.yml" up -d --build