nsarrazin HF Staff commited on
Commit
3471d51
·
unverified ·
1 Parent(s): f7a8eb7

fix(featureflags): use correct env vars (#1405)

Browse files
chart/env/prod.yaml CHANGED
@@ -473,8 +473,8 @@ envVars:
473
  "outputComponentIdx": 0
474
  }
475
  ]
476
- HF_ORG_ADMIN: 'huggingchat'
477
- HF_ORG_EARLY_ACCESS: 'huggingface'
478
 
479
  infisical:
480
  enabled: true
 
473
  "outputComponentIdx": 0
474
  }
475
  ]
476
+ HF_ORG_ADMIN: '644171cfbd0c97265298aa99'
477
+ HF_ORG_EARLY_ACCESS: '5e67bd5b1009063689407478'
478
 
479
  infisical:
480
  enabled: true
src/routes/login/callback/updateUser.ts CHANGED
@@ -9,42 +9,8 @@ import crypto from "crypto";
9
  import { sha256 } from "$lib/utils/sha256";
10
  import { addWeeks } from "date-fns";
11
  import { OIDConfig } from "$lib/server/auth";
12
- import { HF_ORG_ADMIN, HF_ORG_EARLY_ACCESS } from "$env/static/private";
13
  import { logger } from "$lib/server/logger";
14
- import { building } from "$app/environment";
15
-
16
- let earlyAccessIds: string[] | null = null;
17
- let adminIds: string[] | null = null;
18
-
19
- if (!building) {
20
- earlyAccessIds = HF_ORG_EARLY_ACCESS
21
- ? await fetch(`https://huggingface.co/api/organizations/${HF_ORG_EARLY_ACCESS}/members`)
22
- .then((res) => res.json())
23
- .then((res: Array<{ _id: string }>) => res.map((user: { _id: string }) => user._id))
24
- .then((res) => {
25
- logger.debug(`Found ${res.length} early access members`);
26
- return res;
27
- })
28
- .catch((err) => {
29
- logger.error(err, "Failed to fetch early access members");
30
- return null;
31
- })
32
- : null;
33
-
34
- adminIds = HF_ORG_ADMIN
35
- ? await fetch(`https://huggingface.co/api/organizations/${HF_ORG_ADMIN}/members`)
36
- .then((res) => res.json())
37
- .then((res: Array<{ _id: string }>) => res.map((user) => user._id))
38
- .then((res) => {
39
- logger.debug(`Found ${res.length} admin members`);
40
- return res;
41
- })
42
- .catch((err) => {
43
- logger.error(err, "Failed to fetch admin members");
44
- return null;
45
- })
46
- : null;
47
- }
48
 
49
  export async function updateUser(params: {
50
  userData: UserinfoResponse;
@@ -67,6 +33,7 @@ export async function updateUser(params: {
67
  email,
68
  picture: avatarUrl,
69
  sub: hfUserId,
 
70
  } = z
71
  .object({
72
  preferred_username: z.string().optional(),
@@ -112,19 +79,19 @@ export async function updateUser(params: {
112
  // Dynamically access user data based on NAME_CLAIM from environment
113
  // This approach allows us to adapt to different OIDC providers flexibly.
114
 
115
- let isAdmin = undefined;
116
- let isEarlyAccess = undefined;
117
-
118
- if (hfUserId) {
119
- if (adminIds !== null) {
120
- isAdmin = adminIds.includes(hfUserId);
121
- logger.info(`Setting admin to ${isAdmin} for user ${hfUserId}`);
122
- }
123
- if (earlyAccessIds !== null) {
124
- isEarlyAccess = earlyAccessIds.includes(hfUserId);
125
- logger.info(`Setting early access to ${isEarlyAccess} for user ${hfUserId}`);
126
- }
127
- }
128
 
129
  logger.debug(
130
  {
@@ -154,8 +121,7 @@ export async function updateUser(params: {
154
  // update existing user if any
155
  await collections.users.updateOne(
156
  { _id: existingUser._id },
157
- { $set: { username, name, avatarUrl, isAdmin, isEarlyAccess } },
158
- { ignoreUndefined: true }
159
  );
160
 
161
  // remove previous session if it exists and add new one
@@ -172,21 +138,18 @@ export async function updateUser(params: {
172
  });
173
  } else {
174
  // user doesn't exist yet, create a new one
175
- const { insertedId } = await collections.users.insertOne(
176
- {
177
- _id: new ObjectId(),
178
- createdAt: new Date(),
179
- updatedAt: new Date(),
180
- username,
181
- name,
182
- email,
183
- avatarUrl,
184
- hfUserId,
185
- isAdmin,
186
- isEarlyAccess,
187
- },
188
- { ignoreUndefined: true }
189
- );
190
 
191
  userId = insertedId;
192
 
 
9
  import { sha256 } from "$lib/utils/sha256";
10
  import { addWeeks } from "date-fns";
11
  import { OIDConfig } from "$lib/server/auth";
12
+ import { env } from "$env/dynamic/private";
13
  import { logger } from "$lib/server/logger";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  export async function updateUser(params: {
16
  userData: UserinfoResponse;
 
33
  email,
34
  picture: avatarUrl,
35
  sub: hfUserId,
36
+ orgs,
37
  } = z
38
  .object({
39
  preferred_username: z.string().optional(),
 
79
  // Dynamically access user data based on NAME_CLAIM from environment
80
  // This approach allows us to adapt to different OIDC providers flexibly.
81
 
82
+ logger.info(
83
+ {
84
+ login_username: username,
85
+ login_name: name,
86
+ login_email: email,
87
+ login_orgs: orgs?.map((el) => el.sub),
88
+ },
89
+ "user login"
90
+ );
91
+ // if using huggingface as auth provider, check orgs for earl access and amin rights
92
+ const isAdmin = (env.HF_ORG_ADMIN && orgs?.some((org) => org.sub === env.HF_ORG_ADMIN)) || false;
93
+ const isEarlyAccess =
94
+ (env.HF_ORG_EARLY_ACCESS && orgs?.some((org) => org.sub === env.HF_ORG_EARLY_ACCESS)) || false;
95
 
96
  logger.debug(
97
  {
 
121
  // update existing user if any
122
  await collections.users.updateOne(
123
  { _id: existingUser._id },
124
+ { $set: { username, name, avatarUrl, isAdmin, isEarlyAccess } }
 
125
  );
126
 
127
  // remove previous session if it exists and add new one
 
138
  });
139
  } else {
140
  // user doesn't exist yet, create a new one
141
+ const { insertedId } = await collections.users.insertOne({
142
+ _id: new ObjectId(),
143
+ createdAt: new Date(),
144
+ updatedAt: new Date(),
145
+ username,
146
+ name,
147
+ email,
148
+ avatarUrl,
149
+ hfUserId,
150
+ isAdmin,
151
+ isEarlyAccess,
152
+ });
 
 
 
153
 
154
  userId = insertedId;
155