Skip to content

Commit

Permalink
Added GitHub OAuth Router
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyowen committed Mar 10, 2021
1 parent ad49bf5 commit 33c302d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ app.use(passport.initialize());
const usersRouter = require('./routes/users.route');
const todoRouter = require('./routes/todo.route');
const statusRouter = require('./routes/status.route');
const oauthRouter = require('./routes/oauth.route');
app.use('/data/accounts/', usersRouter);
app.use('/data/todo/', todoRouter);
app.use('/oauth/', oauthRouter);
app.use('/', statusRouter);

const URI = process.env.ATLAS_URI;
Expand Down
41 changes: 41 additions & 0 deletions server/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 server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"crypto": "^1.0.1",
Expand Down
31 changes: 31 additions & 0 deletions server/routes/oauth.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const router = require('express').Router();
const axios = require('axios');

const CLIENT_ID = process.env.GITHUB_ID;
const CLIENT_SECRET = process.env.GITHUB_SECRET;

router.get('/github', async (req, res) => {
const code = req.query.code;
await axios({
method: 'post',
url: `https://github.com/login/oauth/access_token?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&code=${code}`,
headers: { accept: 'application/json' }
})
.then(async result => {
const token = result.data.access_token;
if(token){
await axios({
method: 'get',
url: `https://api.github.com/user`,
headers: {
Authorization: 'token ' + token
}
})
.then(user => { res.json(user.data) })
.catch(err => console.log(err))
}else res.json('Error in Registering via GitHub');
})
.catch(err => console.log(err))
})

module.exports = router;

0 comments on commit 33c302d

Please sign in to comment.