Howdy developers! π
Are you looking for a hassle-free way to set up GitHub OAuth for your applications? Whether you're building a Chrome Extension, a web app, or any other project that requires secure authentication, Render.com provides an easy and cost-effective solution for hosting your OAuth backend. Let's get started! π
π Why Use Render for OAuth?
- Free Tier: Ideal for small projects and prototypes.
- Simple Setup: Deploy your backend in just a few clicks.
- Auto-Deploy from GitHub: Automatically updates your backend when you push code.
- Secure and Reliable: HTTPS support out of the box.
π§ Setting Up GitHub OAuth with Render
1. Create Your OAuth Backend π οΈ
- Initialize a Node.js project:
npm init -y
npm install express axios cors dotenv
-
Create a
server.js
file:
require('dotenv').config();
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
const CLIENT_ID = process.env.GITHUB_CLIENT_ID;
const CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET;
app.post('/auth/github', async (req, res) => {
const { code } = req.body;
if (!code) return res.status(400).json({ error: 'Authorization code missing' });
try {
const tokenResponse = await axios.post(
'https://github.com/login/oauth/access_token',
{ client_id: CLIENT_ID, client_secret: CLIENT_SECRET, code },
{ headers: { Accept: 'application/json' } }
);
const accessToken = tokenResponse.data.access_token;
const userResponse = await axios.get('https://api.github.com/user', {
headers: { Authorization: `token ${accessToken}` },
});
res.json({ access_token: accessToken, github_username: userResponse.data.login });
} catch (error) {
res.status(500).json({ error: 'OAuth flow failed', details: error.message });
}
});
app.listen(PORT, () => console.log(`β
Server running on port ${PORT}`));
-
Create a
.env
file:
GITHUB_CLIENT_ID=your_client_id
GITHUB_CLIENT_SECRET=your_client_secret
- Push Your Code to GitHub:
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin main
2. Deploy on Render.com π
- Sign Up/Log In to Render.com.
- Click "New Web Service" and connect your GitHub.
-
Select your repository and configure:
- Environment: Node
-
Build Command:
npm install
-
Start Command:
node server.js
-
Add Environment Variables in Render:
-
GITHUB_CLIENT_ID
: Your GitHub OAuth Client ID -
GITHUB_CLIENT_SECRET
: Your GitHub OAuth Client Secret
-
Click "Deploy" and your backend will be live! π₯
3. Configure GitHub OAuth Settings π
- Go to your GitHub Developer Settings.
-
Edit your OAuth App:
- Authorization Callback URL:
https://your-render-app.onrender.com/auth/github
π Done! What's Next?
- Integrate with your front-end or Chrome Extension.
- Test the OAuth flow by logging in with GitHub.
- Expand your app with more GitHub API integrations!
π Final Thoughts
Using Render.com for hosting your OAuth backend is quick, easy, and free for small projects. Itβs a great way to manage secure authentication without worrying about complex server setups.
Have you tried deploying OAuth on Render? Share your experiences or ask questions in the comments! π
Happy coding! π»π
Follow me on LinkedIn | Codewars | LeetCode | Behance
Check out the full source code on GitHub!
Top comments (0)