This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
caelum0x 64fbe9f474 feat: round-3 deep product build-out (69 gaps closed, all 7 products green)
Deep-audit + build-out across all products to fully-usable state; all typecheck clean.

- gitgram-web (99): wire Store feature to real endpoints, close 9 gaps
- gitgram-backend (100): close 8 flow gaps, document infra-provisioning items
- gitgram-mobile (90): fix API base-URL/path mismatch (404s) + copy proximity
  components into components/proximity wired to real screens; 15 gaps, +react-native-svg
- aitm-core (95): wire natural-language CLI flow end-to-end; 11 gaps
- aitm-api (97): fix teams metadata runtime bug; copy a2a-js server/client/grpc
  into src/a2a and wire A2A route; crates.io package search; 8 gaps
- aitm-mcp (100): fix A2A client/server card mismatch; 9 gaps
- aitm-website (95): build missing docs routes; 9 gaps

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 13:54:57 +03:00

112 lines
3.6 KiB
TypeScript

import { create } from 'zustand';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
import { pushNotificationService } from '../services/pushNotificationService';
import { API_BASE_URL } from '../constants/Config';
import { logger } from '../utils/logger';
interface User {
id: string;
username: string;
email: string;
avatar?: string;
githubUsername?: string;
bio?: string;
isPrivate?: boolean;
}
interface AuthState {
user: User | null;
accessToken: string | null;
refreshToken: string | null;
isAuthenticated: boolean;
isLoading: boolean;
setUser: (user: User | null) => void;
setTokens: (accessToken: string | null, refreshToken: string | null) => void;
login: (user: User, accessToken: string, refreshToken: string) => Promise<void>;
logout: () => Promise<void>;
refreshAccessToken: () => Promise<string | null>;
initialize: () => Promise<void>;
}
export const useAuthStore = create<AuthState>((set, get) => ({
user: null,
accessToken: null,
refreshToken: null,
isAuthenticated: false,
isLoading: true,
setUser: (user) => set({ user }),
setTokens: (accessToken, refreshToken) => set({ accessToken, refreshToken }),
login: async (user, accessToken, refreshToken) => {
await AsyncStorage.setItem('accessToken', accessToken);
await AsyncStorage.setItem('refreshToken', refreshToken);
await AsyncStorage.setItem('user', JSON.stringify(user));
set({ user, accessToken, refreshToken, isAuthenticated: true });
},
logout: async () => {
// Unregister push notifications before logging out
await pushNotificationService.unregisterPushNotifications();
await AsyncStorage.removeItem('accessToken');
await AsyncStorage.removeItem('refreshToken');
await AsyncStorage.removeItem('user');
set({ user: null, accessToken: null, refreshToken: null, isAuthenticated: false });
},
refreshAccessToken: async () => {
const { refreshToken } = get();
if (!refreshToken) return null;
try {
// Use a bare axios call (not the shared `api` client) to avoid recursing
// through api's request interceptor, which itself calls refreshAccessToken.
// The base URL is sourced from Config so host/path stay consistent with
// authService and the rest of the app.
const response = await axios.post(
`${API_BASE_URL}/auth/refresh`,
{ refreshToken },
{ headers: { 'Content-Type': 'application/json' }, timeout: 30000 }
);
const accessToken: string | undefined = response.data?.accessToken;
if (accessToken) {
await AsyncStorage.setItem('accessToken', accessToken);
set({ accessToken });
return accessToken;
}
// No token returned, treat as failed refresh
await get().logout();
return null;
} catch (error) {
// Refresh token expired/invalid -> force logout so UI returns to auth flow
logger.error('Error refreshing token:', error);
await get().logout();
return null;
}
},
initialize: async () => {
try {
const accessToken = await AsyncStorage.getItem('accessToken');
const refreshToken = await AsyncStorage.getItem('refreshToken');
const userStr = await AsyncStorage.getItem('user');
if (accessToken && refreshToken && userStr) {
const user = JSON.parse(userStr);
set({ user, accessToken, refreshToken, isAuthenticated: true, isLoading: false });
} else {
set({ isLoading: false });
}
} catch (error) {
logger.error('Error initializing auth:', error);
set({ isLoading: false });
}
},
}));