File size: 2,107 Bytes
5d28468
 
 
 
 
 
 
 
 
 
 
 
 
8190e40
 
 
 
5d28468
 
 
 
8190e40
 
 
 
 
5d28468
 
 
 
 
 
 
8190e40
 
 
5d28468
 
 
 
 
 
 
 
 
 
 
 
 
8190e40
 
 
 
5d28468
 
 
8190e40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d28468
 
8190e40
 
5d28468
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import axios from 'axios';

// Variable to store user ID once obtained
let cachedUserId = null;

/**
 * Identify the current user, either by getting existing ID from API 
 * or creating a new user ID if none exists
 * 
 * @returns {Promise<string>} The user ID
 */
export const identifyUser = async () => {
  try {
    // First check localStorage for existing user ID
    const storedUserId = localStorage.getItem('user_id');
    
    // Return cached or stored user ID if available
    if (cachedUserId) {
      return cachedUserId;
    }
    
    if (storedUserId) {
      cachedUserId = storedUserId;
      return storedUserId;
    }
    
    // Get user ID from the server
    const response = await axios.get('/identify');
    const userId = response.data.user_id;
    
    // Cache the user ID for future use
    cachedUserId = userId;
    
    // Store in localStorage for persistence across sessions
    localStorage.setItem('user_id', userId);
    
    return userId;
  } catch (error) {
    console.error('Error identifying user:', error);
    return null;
  }
};

/**
 * Get the cached user ID or null if not identified yet
 * 
 * @returns {string|null} The cached user ID
 */
export const getCachedUserId = () => {
  if (!cachedUserId) {
    // Try to get from localStorage if not in memory
    cachedUserId = localStorage.getItem('user_id');
  }
  return cachedUserId;
};

/**
 * Configure axios to automatically include user ID in requests
 */
export const setupUserIdInterceptor = () => {
  axios.interceptors.request.use(function (config) {
    const userId = getCachedUserId();
    if (userId) {
      // Add user ID as a header
      config.headers['X-User-ID'] = userId;
      
      // Add user ID as a URL parameter for GET requests
      if (config.method === 'get') {
        config.params = config.params || {};
        if (!config.params.user_id) {
          config.params.user_id = userId;
        }
      }
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });
};

export default {
  identifyUser,
  getCachedUserId,
  setupUserIdInterceptor
};