Newer
Older
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const OrcidStrategy = require('passport-orcid')
const config = require('config')
const get = require('lodash/get')
const { clientID, clientSecret, callbackPath, successPath } = config.get(
'orcid',
)
let userId
const LinkOrcid = app => {
const { passport } = app.locals
passport.serializeUser((user, done) => {
done(null, user)
})
passport.deserializeUser((user, done) => {
done(null, user)
})
passport.use(
new OrcidStrategy(
{
sandbox: process.env.NODE_ENV !== 'production',
callbackURL: `${config.get('pubsweet-client.baseUrl')}${callbackPath}`,
clientID,
clientSecret,
},
(accessToken, refreshToken, params, profile, done) => {
profile = { orcid: params.orcid, name: params.name }
return done(null, profile)
},
),
)
app.get(
'/api/users/orcid',
(req, res, next) => {
userId = get(req, 'query.userId')
next()
},
passport.authenticate('orcid'),
)
app.get(
callbackPath,
passport.authenticate('orcid', {
failureRedirect: successPath,
}),
linkOrcidHandler(app.locals.models),
)
}
const linkOrcidHandler = models => async (req, res) => {
const user = await models.User.find(userId)
user.orcid = req.user.orcid
await user.save()
res.redirect(successPath)
}
module.exports = LinkOrcid