Skip to content

Commit 0d8d136

Browse files
authored
Merge pull request #3515 from OpenNeuroOrg/orcid-redirect-fix
Preserve browser location on ORCID logins
2 parents a371915 + 692b29a commit 0d8d136

File tree

1 file changed

+41
-36
lines changed
  • packages/openneuro-server/src/libs/authentication

1 file changed

+41
-36
lines changed

packages/openneuro-server/src/libs/authentication/orcid.ts

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import * as Sentry from "@sentry/node"
44
import { userMigration } from "./user-migration"
55
import User from "../../models/user"
66

7-
export const requestAuth = passport.authenticate("orcid", {
8-
session: false,
9-
})
7+
export const requestAuth = (req, res, next) =>
8+
passport.authenticate("orcid", {
9+
session: false,
10+
state: req.query.redirectPath || null,
11+
})(req, res, next)
1012

1113
/**
1214
* Complete a successful login
@@ -30,41 +32,44 @@ export function completeRequestLogin(req, res, next, user) {
3032
}
3133

3234
export const authCallback = (req, res, next) =>
33-
passport.authenticate("orcid", async (err, user) => {
34-
if (err) {
35-
Sentry.captureException(err)
36-
if (err.type) {
37-
return res.redirect(`/error/orcid/${err.type}`)
38-
} else {
39-
return res.redirect("/error/orcid/unknown")
35+
passport.authenticate(
36+
"orcid",
37+
async (err, user) => {
38+
if (err) {
39+
Sentry.captureException(err)
40+
if (err.type) {
41+
return res.redirect(`/error/orcid/${err.type}`)
42+
} else {
43+
return res.redirect("/error/orcid/unknown")
44+
}
45+
}
46+
if (!user) {
47+
return res.redirect("/")
4048
}
41-
}
42-
if (!user) {
43-
return res.redirect("/")
44-
}
4549

46-
try {
47-
// adds new date for login/lastSeen
48-
await User.findByIdAndUpdate(user._id, { lastSeen: new Date() })
49-
} catch (error: unknown) {
50-
if (error instanceof Error) {
51-
Sentry.captureException(error)
52-
} else {
53-
Sentry.captureException(new Error(String(error)))
50+
try {
51+
// adds new date for login/lastSeen
52+
await User.findByIdAndUpdate(user._id, { lastSeen: new Date() })
53+
} catch (error: unknown) {
54+
if (error instanceof Error) {
55+
Sentry.captureException(error)
56+
} else {
57+
Sentry.captureException(new Error(String(error)))
58+
}
59+
// Don't block the login flow
5460
}
55-
// Don't block the login flow
56-
}
5761

58-
// Google user
59-
const existingAuth = parsedJwtFromRequest(req)
60-
if (
61-
existingAuth && existingAuth.provider === "google" &&
62-
existingAuth.exp * 1000 > Date.now()
63-
) {
64-
return userMigration(user.providerId, existingAuth.sub).then(() => {
62+
// Google user
63+
const existingAuth = parsedJwtFromRequest(req)
64+
if (
65+
existingAuth && existingAuth.provider === "google" &&
66+
existingAuth.exp * 1000 > Date.now()
67+
) {
68+
return userMigration(user.providerId, existingAuth.sub).then(() => {
69+
return completeRequestLogin(req, res, next, user)
70+
})
71+
} else {
6572
return completeRequestLogin(req, res, next, user)
66-
})
67-
} else {
68-
return completeRequestLogin(req, res, next, user)
69-
}
70-
})(req, res, next)
73+
}
74+
},
75+
)(req, res, next)

0 commit comments

Comments
 (0)