mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-20 11:43:38 +02:00
155 lines
3.8 KiB
TypeScript
155 lines
3.8 KiB
TypeScript
/**
|
|
* Auth Store - Manages authentication state using Svelte 5 runes
|
|
* Now using Mana Core Auth instead of Supabase Auth
|
|
*/
|
|
|
|
import { initializeWebAuth, type UserData } from '@manacore/shared-auth';
|
|
import { PUBLIC_MANA_CORE_AUTH_URL } from '$env/static/public';
|
|
|
|
// Initialize Mana Core Auth
|
|
const MANA_AUTH_URL = PUBLIC_MANA_CORE_AUTH_URL || 'http://localhost:3001';
|
|
const { authService, tokenManager } = initializeWebAuth({
|
|
baseUrl: MANA_AUTH_URL,
|
|
});
|
|
|
|
// State
|
|
let user = $state<UserData | null>(null);
|
|
let loading = $state(true);
|
|
let initialized = $state(false);
|
|
|
|
export const authStore = {
|
|
// Getters
|
|
get user() {
|
|
return user;
|
|
},
|
|
get loading() {
|
|
return loading;
|
|
},
|
|
get isAuthenticated() {
|
|
return !!user;
|
|
},
|
|
get initialized() {
|
|
return initialized;
|
|
},
|
|
|
|
/**
|
|
* Initialize auth state from stored tokens
|
|
*/
|
|
async initialize() {
|
|
if (initialized) return;
|
|
|
|
loading = true;
|
|
try {
|
|
const authenticated = await authService.isAuthenticated();
|
|
if (authenticated) {
|
|
const userData = await authService.getUserFromToken();
|
|
user = userData;
|
|
}
|
|
initialized = true;
|
|
} catch (error) {
|
|
console.error('Failed to initialize auth:', error);
|
|
user = null;
|
|
} finally {
|
|
loading = false;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Sign in with email and password
|
|
*/
|
|
async signIn(email: string, password: string) {
|
|
try {
|
|
const result = await authService.signIn(email, password);
|
|
|
|
if (!result.success) {
|
|
return { success: false, error: result.error || 'Login failed' };
|
|
}
|
|
|
|
// Get user data from token
|
|
const userData = await authService.getUserFromToken();
|
|
user = userData;
|
|
|
|
return { success: true, error: null };
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
return { success: false, error: errorMessage };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Sign up with email and password
|
|
*/
|
|
async signUp(email: string, password: string) {
|
|
try {
|
|
const result = await authService.signUp(email, password);
|
|
|
|
if (!result.success) {
|
|
return { success: false, error: result.error || 'Signup failed', needsVerification: false };
|
|
}
|
|
|
|
// Mana Core Auth requires separate login after signup
|
|
if (result.needsVerification) {
|
|
return { success: true, error: null, needsVerification: true };
|
|
}
|
|
|
|
// Auto sign in after successful signup
|
|
const signInResult = await this.signIn(email, password);
|
|
return { ...signInResult, needsVerification: false };
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
return { success: false, error: errorMessage, needsVerification: false };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Sign out
|
|
*/
|
|
async signOut() {
|
|
try {
|
|
await authService.signOut();
|
|
user = null;
|
|
} catch (error) {
|
|
console.error('Sign out error:', error);
|
|
// Clear user even if sign out fails
|
|
user = null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Send password reset email
|
|
*/
|
|
async resetPassword(email: string) {
|
|
try {
|
|
const result = await authService.forgotPassword(email);
|
|
|
|
if (!result.success) {
|
|
return { success: false, error: result.error || 'Password reset failed' };
|
|
}
|
|
|
|
return { success: true, error: null };
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
return { success: false, error: errorMessage };
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get user credit balance
|
|
*/
|
|
async getCredits() {
|
|
try {
|
|
const credits = await authService.getUserCredits();
|
|
return credits;
|
|
} catch (error) {
|
|
console.error('Failed to get credits:', error);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Get access token for API calls
|
|
*/
|
|
async getAccessToken() {
|
|
return await authService.getAppToken();
|
|
},
|
|
};
|