Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions src/app/authorize/pages/authorize/authorize.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
finalize,
first,
map,
mapTo,
switchMap,
take,
tap,
Expand Down Expand Up @@ -73,41 +74,27 @@ export class AuthorizeComponent {
*/
ngOnInit(): void {
this.loading = true
let currentSession: UserSession | null = null

forkJoin({
platform: this.loadPlatformInfo(),
userSession: this.loadUserSession(),
userRecord: this.recordService.getRecord({}).pipe(
filter((userRecord: UserRecord) => {
return (
!!userRecord &&
!!userRecord?.userInfo &&
!!userRecord?.emails &&
!!userRecord?.affiliations?.length
)
}),
take(1)
),
})
.pipe(
switchMap((record) => {
return this.loginMainInterstitialsManagerService
.checkLoginInterstitials(record.userRecord, {
returnType: 'component',
togglzPrefix: 'OAUTH',
})
.pipe(
take(1),
map((interstitial) => {
this.interstitialComponent = interstitial
}),
switchMap(() => {
return of(record.userSession)
}),
finalize(() => {
this.handleUserSession(record.userSession)
})
)
// Keep a copy of userSession for finalize()
tap(({ userSession }) => (currentSession = userSession)),

// If logged in, fetch record → check interstitials; otherwise skip straight to oauth session handling
switchMap(({ userSession }) =>
userSession.loggedIn
? this.loadRecordAndCheckInterstitials(userSession)
: of(userSession)
),

// Always run this at the end, regardless of path
finalize(() => {
this.handleOauthSession(currentSession)
this.loading = false
})
)
.subscribe()
Expand Down Expand Up @@ -194,7 +181,7 @@ export class AuthorizeComponent {
* After loading forkJoin data, decide on final flow:
* show error, show domain interstitial, or show authorization screen.
*/
private handleUserSession(userSession: UserSession): void {
private handleOauthSession(userSession: UserSession): void {
this.oauthSession = userSession.oauthSession

// 1. If the backend returned an error
Expand Down Expand Up @@ -226,6 +213,33 @@ export class AuthorizeComponent {
this.loading = false
}

/**
* Fetches a valid UserRecord, then runs checkLoginInterstitials,
* and finally emits the original UserSession.
*/
private loadRecordAndCheckInterstitials(
session: UserSession
): Observable<UserSession> {
return this.recordService.getRecord({}).pipe(
// Only proceed if the record has userInfo, emails, and at least one affiliation
filter((rec: UserRecord) => !!rec && !!rec.userInfo),
take(1),
switchMap((validRecord) =>
this.loginMainInterstitialsManagerService
.checkLoginInterstitials(validRecord, {
returnType: 'component',
togglzPrefix: 'OAUTH',
})
.pipe(
take(1),
tap((interstitial) => (this.interstitialComponent = interstitial)),
// After setting up interstitials, pass the original session back downstream
mapTo(session)
)
)
)
}

/**
* Determines if the user was already authorized based on OAuth session data.
*/
Expand Down