|
7 | 7 | finalize, |
8 | 8 | first, |
9 | 9 | map, |
| 10 | + mapTo, |
10 | 11 | switchMap, |
11 | 12 | take, |
12 | 13 | tap, |
@@ -73,41 +74,27 @@ export class AuthorizeComponent { |
73 | 74 | */ |
74 | 75 | ngOnInit(): void { |
75 | 76 | this.loading = true |
| 77 | + let currentSession: UserSession | null = null |
76 | 78 |
|
77 | 79 | forkJoin({ |
78 | 80 | platform: this.loadPlatformInfo(), |
79 | 81 | userSession: this.loadUserSession(), |
80 | | - userRecord: this.recordService.getRecord({}).pipe( |
81 | | - filter((userRecord: UserRecord) => { |
82 | | - return ( |
83 | | - !!userRecord && |
84 | | - !!userRecord?.userInfo && |
85 | | - !!userRecord?.emails && |
86 | | - !!userRecord?.affiliations?.length |
87 | | - ) |
88 | | - }), |
89 | | - take(1) |
90 | | - ), |
91 | 82 | }) |
92 | 83 | .pipe( |
93 | | - switchMap((record) => { |
94 | | - return this.loginMainInterstitialsManagerService |
95 | | - .checkLoginInterstitials(record.userRecord, { |
96 | | - returnType: 'component', |
97 | | - togglzPrefix: 'OAUTH', |
98 | | - }) |
99 | | - .pipe( |
100 | | - take(1), |
101 | | - map((interstitial) => { |
102 | | - this.interstitialComponent = interstitial |
103 | | - }), |
104 | | - switchMap(() => { |
105 | | - return of(record.userSession) |
106 | | - }), |
107 | | - finalize(() => { |
108 | | - this.handleUserSession(record.userSession) |
109 | | - }) |
110 | | - ) |
| 84 | + // Keep a copy of userSession for finalize() |
| 85 | + tap(({ userSession }) => (currentSession = userSession)), |
| 86 | + |
| 87 | + // If logged in, fetch record → check interstitials; otherwise skip straight to oauth session handling |
| 88 | + switchMap(({ userSession }) => |
| 89 | + userSession.loggedIn |
| 90 | + ? this.loadRecordAndCheckInterstitials(userSession) |
| 91 | + : of(userSession) |
| 92 | + ), |
| 93 | + |
| 94 | + // Always run this at the end, regardless of path |
| 95 | + finalize(() => { |
| 96 | + this.handleOauthSession(currentSession) |
| 97 | + this.loading = false |
111 | 98 | }) |
112 | 99 | ) |
113 | 100 | .subscribe() |
@@ -194,7 +181,7 @@ export class AuthorizeComponent { |
194 | 181 | * After loading forkJoin data, decide on final flow: |
195 | 182 | * show error, show domain interstitial, or show authorization screen. |
196 | 183 | */ |
197 | | - private handleUserSession(userSession: UserSession): void { |
| 184 | + private handleOauthSession(userSession: UserSession): void { |
198 | 185 | this.oauthSession = userSession.oauthSession |
199 | 186 |
|
200 | 187 | // 1. If the backend returned an error |
@@ -226,6 +213,33 @@ export class AuthorizeComponent { |
226 | 213 | this.loading = false |
227 | 214 | } |
228 | 215 |
|
| 216 | + /** |
| 217 | + * Fetches a valid UserRecord, then runs checkLoginInterstitials, |
| 218 | + * and finally emits the original UserSession. |
| 219 | + */ |
| 220 | + private loadRecordAndCheckInterstitials( |
| 221 | + session: UserSession |
| 222 | + ): Observable<UserSession> { |
| 223 | + return this.recordService.getRecord({}).pipe( |
| 224 | + // Only proceed if the record has userInfo, emails, and at least one affiliation |
| 225 | + filter((rec: UserRecord) => !!rec && !!rec.userInfo), |
| 226 | + take(1), |
| 227 | + switchMap((validRecord) => |
| 228 | + this.loginMainInterstitialsManagerService |
| 229 | + .checkLoginInterstitials(validRecord, { |
| 230 | + returnType: 'component', |
| 231 | + togglzPrefix: 'OAUTH', |
| 232 | + }) |
| 233 | + .pipe( |
| 234 | + take(1), |
| 235 | + tap((interstitial) => (this.interstitialComponent = interstitial)), |
| 236 | + // After setting up interstitials, pass the original session back downstream |
| 237 | + mapTo(session) |
| 238 | + ) |
| 239 | + ) |
| 240 | + ) |
| 241 | + } |
| 242 | + |
229 | 243 | /** |
230 | 244 | * Determines if the user was already authorized based on OAuth session data. |
231 | 245 | */ |
|
0 commit comments