Onboarding
This commit is contained in:
@@ -47,9 +47,34 @@ const authPost = async (path: string, body: object): Promise<{ userId: string; e
|
||||
console.warn(`[Auth] ${path} failed:`, response.status, code, msg);
|
||||
throw new Error(code);
|
||||
}
|
||||
return data as any;
|
||||
};
|
||||
|
||||
return data as any;
|
||||
};
|
||||
|
||||
const authDelete = async (path: string, token: string): Promise<void> => {
|
||||
const backendUrl = getConfiguredBackendRootUrl();
|
||||
const hasBackendUrl = Boolean(backendUrl);
|
||||
const url = hasBackendUrl ? `${backendUrl}${path}` : path;
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
method: 'DELETE',
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
} catch {
|
||||
if (!hasBackendUrl) {
|
||||
throw new Error('BACKEND_URL_MISSING');
|
||||
}
|
||||
throw new Error('NETWORK_ERROR');
|
||||
}
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const code = (data as any).code || 'AUTH_ERROR';
|
||||
const msg = (data as any).message || '';
|
||||
console.warn(`[Auth] ${path} failed:`, response.status, code, msg);
|
||||
throw new Error(code);
|
||||
}
|
||||
};
|
||||
|
||||
const buildSession = (data: { userId: string; email: string; name: string; token: string; isNewUser?: boolean }): AuthSession => {
|
||||
const localUser = AuthDb.ensureLocalUser(data.email, data.name);
|
||||
return {
|
||||
@@ -109,10 +134,22 @@ export const AuthService = {
|
||||
},
|
||||
|
||||
async logout(): Promise<void> {
|
||||
await clearStoredSession();
|
||||
},
|
||||
|
||||
async updateSessionName(name: string): Promise<void> {
|
||||
await clearStoredSession();
|
||||
},
|
||||
|
||||
async deleteAccount(): Promise<void> {
|
||||
const session = await this.getSession();
|
||||
if (!session) {
|
||||
await clearStoredSession();
|
||||
return;
|
||||
}
|
||||
await authDelete('/auth/account', session.token);
|
||||
AuthDb.deleteLocalUser(session.userId);
|
||||
await clearStoredSession();
|
||||
await SecureStore.deleteItemAsync('greenlens_first_run_complete');
|
||||
},
|
||||
|
||||
async updateSessionName(name: string): Promise<void> {
|
||||
const session = await this.getSession();
|
||||
if (!session) return;
|
||||
await SecureStore.setItemAsync(SESSION_KEY, JSON.stringify({ ...session, name }));
|
||||
|
||||
Reference in New Issue
Block a user