-
Notifications
You must be signed in to change notification settings - Fork 0
Development to Main Diff #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a4c43e0
22e9a2d
747b7e0
bb9598f
c14dcf8
6348b32
09dbb0c
de8f371
7baff4b
bc2ff72
726a2dc
df9c0ab
6ca236e
ee38a05
a05f665
3f69eae
5463482
4271df8
24c38aa
923196a
7d97630
c2922a4
94af054
c5822ee
73112d3
5ce579a
b87b731
e9971bc
5dd077e
0906084
6bf9c9a
afe664e
b646fb7
f390c1b
fd23a3e
39eb96b
e7704b4
20ce23c
d1f5eaa
b5f46f8
673a5c5
ac0182f
9f102fb
6e79a8d
443d0a2
2e5bbbd
83fac3a
d2ec198
dde7c30
f977234
280dd45
4ad48ec
52edaab
032ef45
447490b
de392a1
44b75cf
4ee227a
20305c0
66b3b5c
1f4c5d2
13830ee
da4986d
0537b1f
3c59aea
adf2e4f
8b1aad5
1957b73
43a322e
5b220f3
93cf7a4
1e6df19
c6b69d6
2eba4aa
b8f1e24
ccd2ad8
1e103b6
b8fa84c
7b735f5
ebdcd13
272ca58
3a2769f
4e9bcaf
7e57428
39c110f
68da79d
07db204
a5ce55d
629447e
9ed3175
d9a6d76
0fd815b
c4c2512
0c21de8
f0d7bc2
36ed128
7d68ee8
0ad0888
e52c8cc
7227a9e
b4f104d
6e4d54b
b1dd2ba
d6e4ac7
33fde1e
1a8a684
b80228c
48a60f5
d147cea
29d45c3
ba418c0
3523fa7
1b6f74a
c98a41c
ab82078
241b4af
9445392
b3376d1
57d6365
2d0b203
e145229
6e25780
1bccbcd
546f1c4
4e9617e
9540159
6577fb6
470bf0b
7bde502
0e0edd1
5d0c879
b945e88
fedf0e2
4e72e12
a6c28ca
85889cf
875b98d
70b2207
d4aec32
73988d2
55f7cab
58bff9a
e8ed4a8
ea31512
836e9df
e3ffcdd
4737e22
bdd8420
6785e41
5f7fcdd
3bc5bd0
b9ac9bf
9f8d1f0
f451eb7
2759f58
c50d357
8858fdf
406b034
759867f
8d8ed01
7e73531
f48a8a1
f1ba631
15384c2
5f0bdb4
163aace
5594bb3
9461e84
b1daad0
0b4b18b
dc44bf4
2a7d1a0
3461008
423769b
c3c6c11
35dcea1
38ad147
d89112b
8acffa9
91cfbfb
70514cb
873bf48
24aec94
c333b5f
b1ba2a9
f2b52a4
84783f1
f9344d1
b6f4173
d7ed12b
1388354
81c3484
43db3a3
a0ea62e
4e0b682
8f64829
38df33b
36be993
8b7e5ba
e60d2dc
3071a96
e4e480b
5e88be0
4fc3339
6ca1faa
a2d2940
6ad60f1
2bd6757
ca5f25c
0370469
5662474
6f86b7f
f50439b
fc37141
37c8654
c6324a0
e557882
b7b3b2d
9cd1bd1
ae0e2da
c559c37
9767730
59e62a0
b160c8d
cdfdc18
8563249
da30f02
c9d2e1a
a559eb7
d0f1d02
2bd64a0
829c439
81c5ee9
699881c
57f3194
42becae
3b0fc91
ea44f0a
10a1970
81fb9b5
f96b930
e955f60
9f5c4f3
62fd563
2dae7bf
be2c765
a403889
4139492
2778414
7445f91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| import { auth } from "express-oauth2-jwt-bearer" | ||
| import { extractToken, extractUser, isTokenExpired } from "../utilities/token.js" | ||
| import User from "../classes/User/User.js" | ||
|
|
||
| /** | ||
| * This function verifies authorization tokens using Auth0 library. to protect a route using this function in a different component: | ||
| 1. import the function in that component | ||
|
|
@@ -17,7 +15,7 @@ function auth0Middleware() { | |
| issuerBaseURL: `https://${process.env.DOMAIN}/`, | ||
| }) | ||
|
|
||
| // Extract user from the token and set req.user. req.user can be set to specific info from the payload, like sib, roles, etc. | ||
| // Extract user from the token and set req.user. req.user can be set to specific info from the payload, like sub, roles, etc. | ||
| async function setUser(req, res, next) { | ||
| const { payload } = req.auth | ||
|
|
||
|
|
@@ -32,24 +30,67 @@ function auth0Middleware() { | |
| try { | ||
| const uid = agent.split("id/")[1] | ||
| const user = new User(uid) | ||
| user.getSelf().then(async (u) => { | ||
| if(!u || !u?.profile) { | ||
| const u = await user.getSelf() | ||
| if(!u || !u?.profile) { | ||
| const email = payload.name | ||
|
|
||
| // Check if a temporary user exists with this email | ||
| let existingUser = null | ||
| try { | ||
| existingUser = await user.getByEmail(email) | ||
| } catch (err) { | ||
| // No user found - that's fine, continue | ||
| } | ||
|
|
||
| if (existingUser && existingUser.inviteCode) { | ||
| // Found a temporary user - merge their memberships into this new user | ||
| user.data = { | ||
| _id: uid, | ||
| agent, | ||
| _sub: payload.sub, | ||
| email: payload.name, | ||
| email: email, | ||
| profile: { displayName: payload.nickname }, | ||
| } | ||
| user.save() | ||
| await user.mergeFromTemporaryUser(existingUser) | ||
| await user.save() | ||
| req.user = user | ||
| next() | ||
| return | ||
| } else if (existingUser) { | ||
| // Non-temporary user with same email - this is a conflict | ||
| const err = new Error(`User with email ${email} already exists. Please contact TPEN3 administrators for assistance.`) | ||
| err.status = 409 | ||
| next(err) | ||
| return | ||
| } else { | ||
| // No existing user - create new | ||
| user.data = { | ||
| _id: uid, | ||
| agent, | ||
| _sub: payload.sub, | ||
| email: email, | ||
| profile: { displayName: payload.nickname }, | ||
| } | ||
| await user.save() | ||
| req.user = user | ||
| next() | ||
| return | ||
| } | ||
| req.user = u | ||
| next() | ||
| return | ||
| }) | ||
| } | ||
| // Ensure no inviteCode on authenticated user | ||
| delete u.inviteCode | ||
|
|
||
| // If user exists but has wrong _sub (e.g., from temp user), update it | ||
| if (u._sub !== payload.sub) { | ||
| u._sub = payload.sub | ||
| const userObj = new User(uid) | ||
| userObj.data = u | ||
| await userObj.update() | ||
| } | ||
|
Comment on lines
+84
to
+89
|
||
|
|
||
| req.user = u | ||
| next() | ||
| return | ||
| } catch (error) { | ||
| next(error) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The authentication middleware now handles temporary user merging, which could be a slow operation involving multiple database queries and group updates. This blocking operation happens on every authenticated request for new users. Consider offloading this to a background job or showing a loading state to the user, as this could cause request timeouts for users with many project invitations.