import AsyncStorage from '@react-native-async-storage/async-storage';

const TOKEN_KEY = 'user_token'; // 添加一个具体的 token key

export const auth = {
  async getToken() {
    try {
      const token = await AsyncStorage.getItem(TOKEN_KEY);
      // console.log('Getting token:', token);
      return token;
    } catch (error) {
      console.error('获取token失败:', error);
      return null;
    }
  },

  async setToken(token: string | undefined) {
    try {
      if (!token) {
        // console.warn('Attempting to set undefined token');
        return;
      }
      await AsyncStorage.setItem(TOKEN_KEY, token);
      // console.log('Token set:', token);
    } catch (error) {
      console.error('保存token失败:', error);
    }
  },

  async removeToken() {
    try {
      await AsyncStorage.removeItem(TOKEN_KEY);
    
      console.log('Token removed'); 
    } catch (error) {
      console.error('删除token失败:', error);
    }
  },

  async isLoggedIn() {
    const token = await this.getToken();
    return !!token;
  }
};