Andrew commited on
Commit
b4b34c7
·
1 Parent(s): 70025fa

feat(auth): add helper to retrieve decrypted user HF tokens

Browse files
Files changed (1) hide show
  1. src/lib/server/userTokens.ts +24 -0
src/lib/server/userTokens.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { collections } from "./database";
2
+ import { decryptToken } from "./tokenEncryption";
3
+ import type { ObjectId } from "mongodb";
4
+
5
+ /**
6
+ * Retrieves and decrypts a user's Hugging Face token if available
7
+ */
8
+ export async function getUserHFToken(userId: ObjectId): Promise<string | null> {
9
+ const userToken = await collections.userTokens.findOne({
10
+ userId,
11
+ provider: "huggingface",
12
+ });
13
+
14
+ if (!userToken) {
15
+ return null;
16
+ }
17
+
18
+ try {
19
+ return decryptToken(userToken.encryptedToken);
20
+ } catch (error) {
21
+ console.error("Failed to decrypt user token:", error);
22
+ return null;
23
+ }
24
+ }