1- import { createUserWithEmailAndPassword , signInWithEmailAndPassword , signOut } from "firebase/auth" ;
2- import { useAPIStore } from "@vease/stores/api" ;
1+ // Third-party imports
2+ import {
3+ EmailAuthProvider ,
4+ createUserWithEmailAndPassword ,
5+ deleteUser ,
6+ reauthenticateWithCredential ,
7+ signInWithEmailAndPassword ,
8+ signOut ,
9+ } from "firebase/auth" ;
10+ import { appMode } from "@ogw_front/utils/local/app_mode" ;
311import { useFirebaseAuth } from "vuefire" ;
12+ import { useInfraStore } from "@ogw_front/stores/infra" ;
13+
14+ // Local imports
15+ import { useAPIStore } from "@vease/stores/api" ;
416
17+ //oxlint-disable max-lines-per-function
518function useAuth ( ) {
619 const auth = useFirebaseAuth ( ) ;
720 const user = useCurrentUser ( ) ;
821 const APIStore = useAPIStore ( ) ;
22+ const infraStore = useInfraStore ( ) ;
923
1024 async function register ( email , password ) {
1125 const { user : newUser } = await createUserWithEmailAndPassword ( auth , email , password ) ;
@@ -23,21 +37,78 @@ function useAuth() {
2337 return newUser ;
2438 }
2539
40+ async function autoLogin ( ) {
41+ console . log ( "Attempting to retrieve credentials for auto-login" ) ;
42+ if ( infraStore . app_mode !== appMode . DESKTOP ) {
43+ return ;
44+ }
45+ try {
46+ const { success, credentials, error } = await globalThis . electronAPI . get_credentials ( ) ;
47+ if ( ! success ) {
48+ console . error ( "Failed to get credentials:" , error ) ;
49+ return ;
50+ }
51+ console . log ( "Credentials retrieval successful" , { credentials } ) ;
52+ if ( credentials ) {
53+ const { email, password } = credentials ;
54+ try {
55+ return login ( email , password ) ;
56+ } catch ( loginError ) {
57+ console . error ( "Auto-login failed:" , loginError ) ;
58+ return globalThis . electronAPI . delete_credentials ( ) ;
59+ }
60+ }
61+ } catch ( error ) {
62+ console . error ( "Failed to get credentials:" , error ) ;
63+ }
64+ }
65+
2666 async function login ( email , password ) {
2767 const { user : loggedInUser } = await signInWithEmailAndPassword ( auth , email , password ) ;
2868 await loggedInUser . reload ( ) ;
2969 if ( ! loggedInUser . emailVerified ) {
3070 await signOut ( auth ) ;
3171 throw new Error ( "Please verify your email address before logging in." ) ;
3272 }
73+ if ( infraStore . app_mode === appMode . DESKTOP ) {
74+ console . log ( "Saving credentials for desktop mode" ) ;
75+ globalThis . electronAPI . save_credentials ( {
76+ email,
77+ password,
78+ } ) ;
79+ }
3380 return loggedInUser ;
3481 }
3582
36- function logout ( ) {
37- signOut ( auth ) ;
83+ async function deleteAccount ( password ) {
84+ if ( ! user . value ) {
85+ throw new Error ( "No user logged in" ) ;
86+ }
87+
88+ // Re-authenticate before deleting (required by Identity Platform)
89+ const credential = EmailAuthProvider . credential ( user . value . email , password ) ;
90+ await reauthenticateWithCredential ( user . value , credential ) ;
91+
92+ await deleteUser ( user . value ) ;
93+ await logout ( ) ;
3894 }
3995
40- return { user, register, login, logout, resetPassword } ;
96+ async function logout ( ) {
97+ if ( infraStore . app_mode === appMode . DESKTOP ) {
98+ try {
99+ const { success } = await globalThis . electronAPI . delete_credentials ( ) ;
100+ if ( ! success ) {
101+ console . error ( "Failed to delete credentials:" , error ) ;
102+ return ;
103+ }
104+ console . log ( "Credentials deletion successful" ) ;
105+ } catch ( error ) {
106+ console . error ( "Failed to delete credentials:" , error ) ;
107+ }
108+ }
109+ await signOut ( auth ) ;
110+ }
111+ return { user, autoLogin, deleteAccount, register, login, logout, resetPassword } ;
41112}
42113
43114function resetPassword ( email ) {
0 commit comments