Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import { create } from "zustand";import { Agent } from "@atproto/api";import * as SecureStore from "expo-secure-store";import { signIn, restoreSession } from "@/lib/oauth";
const DID_STORAGE_KEY = "walkabout_did";
interface AuthState { agent: Agent | null; did: string | null; handle: string | null; avatar: string | null; isLoading: boolean; error: string | null; login: (handle: string) => Promise<void>; logout: () => void; restore: () => Promise<void>;}
export const useAuthStore = create<AuthState>((set) => ({ agent: null, did: null, handle: null, avatar: null, isLoading: false, error: null,
login: async (handle: string) => { set({ isLoading: true, error: null }); try { const result = await signIn(handle); await SecureStore.setItemAsync(DID_STORAGE_KEY, result.did); set({ agent: result.agent, did: result.did, handle: result.handle, avatar: result.avatar ?? null, isLoading: false, }); } catch (e) { set({ isLoading: false, error: e instanceof Error ? e.message : "Login failed", }); } },
logout: () => { SecureStore.deleteItemAsync(DID_STORAGE_KEY); set({ agent: null, did: null, handle: null, avatar: null, error: null }); },
restore: async () => { set({ isLoading: true }); try { const did = await SecureStore.getItemAsync(DID_STORAGE_KEY); if (!did) { set({ isLoading: false }); return; } const result = await restoreSession(did); if (result) { set({ agent: result.agent, did: result.did, handle: result.handle, avatar: result.avatar ?? null, isLoading: false, }); } else { await SecureStore.deleteItemAsync(DID_STORAGE_KEY); set({ isLoading: false }); } } catch { set({ isLoading: false }); } },}));