-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenStorage.js
More file actions
71 lines (61 loc) · 1.73 KB
/
Copy pathtokenStorage.js
File metadata and controls
71 lines (61 loc) · 1.73 KB
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
62
63
64
65
66
67
68
69
70
71
// Simple token storage for CloudAdapter (since it doesn't have OAuth methods)
// In production, use a database instead of in-memory storage
class TokenStorage {
constructor() {
this.tokens = new Map(); // userId -> { token, expiresAt, refreshToken }
}
/**
* Store user token
*/
setToken(userId, token, expiresIn = 3600, refreshToken = null) {
const expiresAt = Date.now() + (expiresIn * 1000);
this.tokens.set(userId, {
token,
expiresAt,
refreshToken
});
console.log(`Token stored for user ${userId}, expires at ${new Date(expiresAt).toISOString()}`);
}
/**
* Get user token if still valid
*/
getToken(userId) {
const tokenData = this.tokens.get(userId);
if (!tokenData) {
console.log(`No token found for user ${userId}`);
return null;
}
// Check if token is expired (with 5 minute buffer)
if (Date.now() > (tokenData.expiresAt - 300000)) {
console.log(`Token expired for user ${userId}`);
this.tokens.delete(userId);
return null;
}
console.log(`Valid token found for user ${userId}`);
return tokenData.token;
}
/**
* Remove user token
*/
clearToken(userId) {
this.tokens.delete(userId);
console.log(`Token cleared for user ${userId}`);
}
/**
* Get all stored tokens (for debugging)
*/
getAllTokens() {
const result = {};
for (const [userId, tokenData] of this.tokens.entries()) {
result[userId] = {
hasToken: !!tokenData.token,
expiresAt: new Date(tokenData.expiresAt).toISOString(),
isExpired: Date.now() > tokenData.expiresAt
};
}
return result;
}
}
// Singleton instance
const tokenStorage = new TokenStorage();
module.exports = tokenStorage;