Skip to content

Seting up backend for the codebadge project and depriciation of old code #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
login via github implemented
  • Loading branch information
ayushnagar123 committed Jun 18, 2020
commit fe0477d54eb872b1ba604693d2a66bd58fec0371
40 changes: 40 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 @@ -16,6 +16,7 @@
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"express-session": "^1.17.1",
"hbs": "^4.1.1",
"http-errors": "~1.6.3",
"mocha": "^8.0.1",
Expand Down
72 changes: 72 additions & 0 deletions routes/users/githubAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const express = require('express');
const router = express.Router()
const axios = require('axios')
var session = require('express-session')
const {github} = require('./../../config/config')
var clientId=github.clientId
var clientSecret=github.clientSecret

router.get('/auth',(req,res)=>{
const body = {
client_id: clientId,
client_secret: clientSecret,
code: req.query.code,
refresh_token:req.cookies["refresh"]
};

const opts = { headers: { accept: 'application/json' } };
axios.post(`https://github.com/login/oauth/access_token`, body, opts).
then(res =>{
return {token:res.data['access_token'],refresh:res.data['refresh_token']}
})
.then(tokens => {
token = tokens.token;
refresh = tokens.refresh;
oauth_token=token;
remember=false;
if(remember){
res.cookie('githubLogin',true)
res.cookie('token',token,{expires: new Date(Date.now() + + 8*60*60*1000)}) // token expires in 10 years time
res.cookie('refresh',refresh,{expires: new Date(Date.now() + + 315360000000)})
}
else{
session({
secret: 'codebadge token',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
})
res.cookie('token',token) // token expires when session ends
res.cookie('githubLogin',true)
}
let config = {
headers: {
"Authorization":`token ${oauth_token}`
}
}

axios
.get(`https://api.github.com/user`,config=config)
.then(resp => {
res.cookie('username',resp.data["login"],{expires: new Date(Date.now() + + 315360000000)}) //saves username for upcoming queries of logedin user
if(resp.data.type=="User"){
res.redirect('http://localhost:3000/user')
}
else{
res.redirect(`http://localhost:3000/org?username=${resp.data["login"]}`)
}
})
.catch(err=>res.sendStatus(400).json({"error":err}))

}).
catch(err => {
res.status(500).json({"error":err});
});
})

router.get('/',(req,res)=>{
res.redirect(`https://github.com/login/oauth/authorize?client_id=${clientId}&scope=user`);
})


module.exports = router
2 changes: 2 additions & 0 deletions routes/users/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const express = require('express');
const router = express.Router();
const codebadgeLocalAPI = require('./codebadge-local');
const githubAPI = require('./githubAPI');

router.use('/github',githubAPI);
router.use('/codebadge',codebadgeLocalAPI);

module.exports = router;