fix: remove authservice injection on userRepo
This commit is contained in:
		
							parent
							
								
									a2f57cbfa9
								
							
						
					
					
						commit
						94ae002d83
					
				@ -3,15 +3,19 @@ const firebaseRepository = require('./firebase.repository');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
let authService = null;
 | 
					let authService = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function setAuthService(service) {
 | 
					function getAuthService() {
 | 
				
			||||||
    authService = service;
 | 
					    if (!authService) {
 | 
				
			||||||
 | 
					        authService = require('../../services/authService');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return authService;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getBaseRepository() {
 | 
					function getBaseRepository() {
 | 
				
			||||||
    if (!authService) {
 | 
					    const service = getAuthService();
 | 
				
			||||||
        throw new Error('AuthService has not been set for the user repository.');
 | 
					    if (!service) {
 | 
				
			||||||
 | 
					        throw new Error('AuthService could not be loaded for the user repository.');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const user = authService.getCurrentUser();
 | 
					    const user = service.getCurrentUser();
 | 
				
			||||||
    if (user && user.isLoggedIn) {
 | 
					    if (user && user.isLoggedIn) {
 | 
				
			||||||
        return firebaseRepository;
 | 
					        return firebaseRepository;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -25,24 +29,23 @@ const userRepositoryAdapter = {
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    getById: () => {
 | 
					    getById: () => {
 | 
				
			||||||
        const uid = authService.getCurrentUserId();
 | 
					        const uid = getAuthService().getCurrentUserId();
 | 
				
			||||||
        return getBaseRepository().getById(uid);
 | 
					        return getBaseRepository().getById(uid);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    update: (updateData) => {
 | 
					    update: (updateData) => {
 | 
				
			||||||
        const uid = authService.getCurrentUserId();
 | 
					        const uid = getAuthService().getCurrentUserId();
 | 
				
			||||||
        return getBaseRepository().update({ uid, ...updateData });
 | 
					        return getBaseRepository().update({ uid, ...updateData });
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    deleteById: () => {
 | 
					    deleteById: () => {
 | 
				
			||||||
        const uid = authService.getCurrentUserId();
 | 
					        const uid = getAuthService().getCurrentUserId();
 | 
				
			||||||
        return getBaseRepository().deleteById(uid);
 | 
					        return getBaseRepository().deleteById(uid);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
    ...userRepositoryAdapter,
 | 
					    ...userRepositoryAdapter
 | 
				
			||||||
    setAuthService
 | 
					 | 
				
			||||||
}; 
 | 
					}; 
 | 
				
			||||||
@ -4,7 +4,6 @@ const { getFirebaseAuth } = require('./firebaseClient');
 | 
				
			|||||||
const fetch = require('node-fetch');
 | 
					const fetch = require('node-fetch');
 | 
				
			||||||
const encryptionService = require('./encryptionService');
 | 
					const encryptionService = require('./encryptionService');
 | 
				
			||||||
const migrationService = require('./migrationService');
 | 
					const migrationService = require('./migrationService');
 | 
				
			||||||
const userRepository = require('../repositories/user');
 | 
					 | 
				
			||||||
const sessionRepository = require('../repositories/session');
 | 
					const sessionRepository = require('../repositories/session');
 | 
				
			||||||
const providerSettingsRepository = require('../repositories/providerSettings');
 | 
					const providerSettingsRepository = require('../repositories/providerSettings');
 | 
				
			||||||
const userModelSelectionsRepository = require('../repositories/userModelSelections');
 | 
					const userModelSelectionsRepository = require('../repositories/userModelSelections');
 | 
				
			||||||
@ -47,7 +46,6 @@ class AuthService {
 | 
				
			|||||||
        encryptionService.initializeKey(this.currentUserId);
 | 
					        encryptionService.initializeKey(this.currentUserId);
 | 
				
			||||||
        this.initializationPromise = null;
 | 
					        this.initializationPromise = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        userRepository.setAuthService(this);
 | 
					 | 
				
			||||||
        sessionRepository.setAuthService(this);
 | 
					        sessionRepository.setAuthService(this);
 | 
				
			||||||
        providerSettingsRepository.setAuthService(this);
 | 
					        providerSettingsRepository.setAuthService(this);
 | 
				
			||||||
        userModelSelectionsRepository.setAuthService(this);
 | 
					        userModelSelectionsRepository.setAuthService(this);
 | 
				
			||||||
@ -56,12 +54,6 @@ class AuthService {
 | 
				
			|||||||
    initialize() {
 | 
					    initialize() {
 | 
				
			||||||
        if (this.isInitialized) return this.initializationPromise;
 | 
					        if (this.isInitialized) return this.initializationPromise;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // --- Break the circular dependency ---
 | 
					 | 
				
			||||||
        // Inject this authService instance into the session repository so it can be used
 | 
					 | 
				
			||||||
        // without a direct `require` cycle.
 | 
					 | 
				
			||||||
        sessionRepository.setAuthService(this);
 | 
					 | 
				
			||||||
        // --- End of dependency injection ---
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        this.initializationPromise = new Promise((resolve) => {
 | 
					        this.initializationPromise = new Promise((resolve) => {
 | 
				
			||||||
            const auth = getFirebaseAuth();
 | 
					            const auth = getFirebaseAuth();
 | 
				
			||||||
            onAuthStateChanged(auth, async (user) => {
 | 
					            onAuthStateChanged(auth, async (user) => {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user