-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathcallback.js
36 lines (33 loc) · 1.08 KB
/
callback.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
const generateScript = require('./login_script.js')
module.exports = (oauth2, oauthProvider) => {
function callbackMiddleWare (req, res, next) {
const code = req.query.code
var options = {
code: code
}
if (oauthProvider === 'gitlab') {
options.client_id = process.env.OAUTH_CLIENT_ID
options.client_secret = process.env.OAUTH_CLIENT_SECRET
options.grant_type = 'authorization_code'
options.redirect_uri = process.env.REDIRECT_URL
}
oauth2.getToken(options)
.then(result => {
const token = oauth2.createToken(result)
content = {
token: token.token.token.access_token,
provider: oauthProvider
}
return { message: 'success', content }
})
.catch(error => {
console.error('Access Token Error', error.message)
return { message: 'error', content: JSON.stringify(error) }
})
.then(result => {
const script = generateScript(oauthProvider, result.message, result.content)
return res.send(script)
})
}
return callbackMiddleWare
}