|
@@ -1,5 +1,6 @@
|
|
|
import React, { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
|
import React, { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
|
|
|
import { API_URL } from '@/lib/config';
|
|
import { API_URL } from '@/lib/config';
|
|
|
|
|
+import { supabase } from '@/lib/supabase';
|
|
|
|
|
|
|
|
interface StoreData {
|
|
interface StoreData {
|
|
|
id: string;
|
|
id: string;
|
|
@@ -38,17 +39,18 @@ export const ShopProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
|
|
|
// Fetch stores function - can be called from context or externally
|
|
// Fetch stores function - can be called from context or externally
|
|
|
const fetchStores = async () => {
|
|
const fetchStores = async () => {
|
|
|
try {
|
|
try {
|
|
|
- const sessionData = localStorage.getItem('session_data');
|
|
|
|
|
- if (!sessionData) {
|
|
|
|
|
|
|
+ // Use Supabase SDK to get valid session (handles token refresh automatically)
|
|
|
|
|
+ const { data: { session }, error: sessionError } = await supabase.auth.getSession();
|
|
|
|
|
+
|
|
|
|
|
+ if (sessionError || !session) {
|
|
|
// No session - mark as loaded with empty stores
|
|
// No session - mark as loaded with empty stores
|
|
|
setStores([]);
|
|
setStores([]);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const session = JSON.parse(sessionData);
|
|
|
|
|
const response = await fetch(`${API_URL}/api/stores`, {
|
|
const response = await fetch(`${API_URL}/api/stores`, {
|
|
|
headers: {
|
|
headers: {
|
|
|
- 'Authorization': `Bearer ${session.session.access_token}`,
|
|
|
|
|
|
|
+ 'Authorization': `Bearer ${session.access_token}`,
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|