Skip to content

Commit

Permalink
ADD :: github oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
anys34 committed Aug 20, 2023
1 parent 83732fb commit 851c4a3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules

.DS_Store
.DS_Store

.env
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const express = require("express");

const pageRouter = require("./routes/page")
const authRouter = require("./routes/auth")

const app = express();
app.set("port", process.env.PORT || 3000);

app.use("/", pageRouter);
app.use("/auth", authRouter);

app.listen(app.get("port"), () => {
console.log(app.get("port"), "node start!");
});
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"axios": "^1.4.0",
"dotenv": "^16.3.1",
"express": "^4.18.2"
}
}
38 changes: 38 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require("dotenv").config();
const express = require("express");
const axios = require("axios");
const router = express.Router();

const env = process.env;

router.get("/", (req, res) => {
res.redirect(
`https://github.com/login/oauth/authorize?client_id=${ env.CLIENT_ID }`
);
});

router.get("/github/callback", (req, res) => {
axios
.post(
"https://github.com/login/oauth/access_token",
{
client_id: env.CLIENT_ID,
client_secret: env.CLIENT_SECRET,
code: req.query.code,
},
{
headers: {
Accept: "application/json",
},
}
)
.then((result) => {
console.log(result.data.access_token);
res.send("you are authorized " + result.data.access_token);
})
.catch((err) => {
console.log(err);
});
});

module.exports = router;
8 changes: 8 additions & 0 deletions routes/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
res.send("hello");
});

module.exports = router;

0 comments on commit 851c4a3

Please sign in to comment.