-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.js
42 lines (37 loc) · 1.45 KB
/
index.js
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
// Import the express lirbary
const express = require('express')
// Import the axios library, to make HTTP requests
const axios = require('axios')
// This is the client ID and client secret that you obtained
// while registering the application
const clientID = '<your client id>'
const clientSecret = '<your client secret>'
// Create a new express application and use
// the express static middleware, to serve all files
// inside the public directory
const app = express()
app.use(express.static(__dirname + '/public'))
app.get('/oauth/redirect', (req, res) => {
// The req.query object has the query params that
// were sent to this route. We want the `code` param
const requestToken = req.query.code
axios({
// make a POST request
method: 'post',
// to the Github authentication API, with the client ID, client secret
// and request token
url: `https://github.com/login/oauth/access_token?client_id=${clientID}&client_secret=${clientSecret}&code=${requestToken}`,
// Set the content type header, so that we get the response in JSOn
headers: {
accept: 'application/json'
}
}).then((response) => {
// Once we get the response, extract the access token from
// the response body
const accessToken = response.data.access_token
// redirect the user to the welcome page, along with the access token
res.redirect(`/welcome.html?access_token=${accessToken}`)
})
})
// Start the server on port 8080
app.listen(8080)