File size: 1,387 Bytes
4d65452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export const SESSION_KEY = 'sahelOwnerSession';
export const LAST_BUSINESS_KEY = 'sahelLastBusiness';

export function isExpiredSession(session) {
  return session?.expires_at && Date.parse(session.expires_at) <= Date.now();
}

export function getSession() {
  try {
    const storedSession = localStorage.getItem(SESSION_KEY);
    if (storedSession) {
      const parsed = JSON.parse(storedSession);
      if (isExpiredSession(parsed)) {
        localStorage.removeItem(SESSION_KEY);
        return null;
      }
      return parsed;
    }
    const legacyOwner = localStorage.getItem('sahelOwner');
    return legacyOwner ? { owner: JSON.parse(legacyOwner), token: '' } : null;
  } catch {
    return null;
  }
}

export function saveSession(session) {
  localStorage.setItem(SESSION_KEY, JSON.stringify(session));
  localStorage.removeItem('sahelOwner');
}

export function clearSession() {
  localStorage.removeItem(SESSION_KEY);
  localStorage.removeItem('sahelOwner');
}

export function authHeaders(ownerToken) {
  return ownerToken ? { Authorization: `Bearer ${ownerToken}` } : {};
}

export function saveLastBusiness(business) {
  localStorage.setItem(LAST_BUSINESS_KEY, JSON.stringify(business));
}

export function getLastBusiness() {
  try {
    const raw = localStorage.getItem(LAST_BUSINESS_KEY);
    return raw ? JSON.parse(raw) : null;
  } catch {
    return null;
  }
}