Skip to content

Commit 875f15a

Browse files
committed
debug: app.config.ts debug mode
1 parent 5aa810d commit 875f15a

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/app/app.config.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,66 @@ import { provideRouter } from '@angular/router';
44
import { routes } from './app.routes';
55
import { provideHttpClient, withInterceptors } from '@angular/common/http';
66
import { JwtService } from './core/auth/services/jwt.service';
7-
import { UserService } from './core/auth/services/user.service';
7+
import { UserService, AuthState } from './core/auth/services/user.service';
88
import { apiInterceptor } from './core/interceptors/api.interceptor';
99
import { tokenInterceptor } from './core/interceptors/token.interceptor';
1010
import { errorInterceptor } from './core/interceptors/error.interceptor';
1111
import { EMPTY } from 'rxjs';
12+
import { User } from './core/auth/user.model';
1213

14+
/**
15+
* Debug interface for testing - exposes app state in a framework-agnostic way.
16+
* Tests can use this instead of directly accessing localStorage or internal state.
17+
*/
18+
export interface ConduitDebug {
19+
getToken: () => string | null;
20+
getAuthState: () => AuthState;
21+
getCurrentUser: () => User | null;
22+
}
23+
24+
declare global {
25+
interface Window {
26+
__conduit_debug__?: ConduitDebug;
27+
}
28+
}
29+
30+
/**
31+
* Sets up the debug interface on window.__conduit_debug__
32+
*/
33+
function setupDebugInterface(jwtService: JwtService, userService: UserService): void {
34+
let currentAuthState: AuthState = 'loading';
35+
let currentUser: User | null = null;
36+
37+
userService.authState.subscribe(state => (currentAuthState = state));
38+
userService.currentUser.subscribe(user => (currentUser = user));
39+
40+
window.__conduit_debug__ = {
41+
getToken: () => jwtService.getToken(),
42+
getAuthState: () => currentAuthState,
43+
getCurrentUser: () => currentUser,
44+
};
45+
}
46+
47+
/**
48+
* App initializer: checks auth state at startup.
49+
*
50+
* - No token → purgeAuth() to exit 'loading' state → 'unauthenticated'
51+
* - Token exists → getCurrentUser() to validate it:
52+
* - Success → 'authenticated'
53+
* - 4XX → 'unauthenticated' (invalid token, cleared)
54+
* - 5XX → 'unavailable' (server down, token kept, auto-retry)
55+
*/
1356
export function initAuth(jwtService: JwtService, userService: UserService) {
14-
return () => (jwtService.getToken() ? userService.getCurrentUser() : EMPTY);
57+
return () => {
58+
setupDebugInterface(jwtService, userService);
59+
60+
if (jwtService.getToken()) {
61+
return userService.getCurrentUser();
62+
} else {
63+
userService.purgeAuth();
64+
return EMPTY;
65+
}
66+
};
1567
}
1668

1769
export const appConfig: ApplicationConfig = {

0 commit comments

Comments
 (0)