This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.js
More file actions
45 lines (35 loc) · 1.31 KB
/
oauth.js
File metadata and controls
45 lines (35 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require('express')
const axios = require('axios');
const path = require('path');
const app = express()
const secrets = require("./secrets.js")
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')))
app.get("/login", (req, res) => {
res.redirect(`https://github.com/login/oauth/authorize?client_id=${secrets.client_id}&redirect_uri=http://localhost:3000/callback&state=${secrets.state}`)
})
app.get("/callback", (req, res)=>{
console.log(`got a callback with url params ${req.query.code} and ${req.query.state}`);
axios.post("https://github.com/login/oauth/access_token",{
client_id: secrets.client_id,
client_secret: secrets.client_secret,
redirect_uri: "http://localhost:3000/callback",
state:secrets.state,
code: req.query.code
},{headers:{Accept:"application/json"}})
.then(response=>{
let o = response.data.access_token;
console.log("Github said " + o);
return axios.get("https://api.github.com/user?access_token=" + o)
})
.then(response=>{
res.send(response.data);
})
.catch(error=>{
console.log("There was an error " + error);
})
})
app.get("/logout", (req, res) => {
console.log("Logout");
res.sendFile(path.join(__dirname, 'index.html'))
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))