Skip to content

Commit

Permalink
Merge pull request #1519 from shadow-dot-cat/castaway/account_load_fa…
Browse files Browse the repository at this point in the history
…ster

fix(account): Refetch / check userinfo less when activating pages
  • Loading branch information
castaway authored Jan 8, 2024
2 parents 843a06e + ec93e67 commit 7befd67
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
50 changes: 32 additions & 18 deletions src/app/account-app/no-products-for-subaccounts.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,40 @@ export class NoProductsForSubaccountsGuard implements CanActivate, CanActivateCh
canActivate(
route: ActivatedRouteSnapshot, _state: RouterStateSnapshot
): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
return this.authGuard.checkLogin().then(
(success) => {
if (typeof(success) === 'boolean' && success) {
const restricted = this.banned_components.find(c => route.component === c);
if (restricted) {
return this.rmmapi.me.toPromise().then(me => {
if (me.owner) {
return this.router.parseUrl('/account/not-for-subaccounts');
} else {
return true;
}
});
} else {
return true;
}
const restricted = this.banned_components.find(c => route.component === c);
const loginCheck = this.authGuard.checkLogin().then(
(success) => {
if (typeof(success) === 'boolean' && success) {
if (restricted) {
return this.rmmapi.me.toPromise().then(me => {
if (me.owner) {
return this.router.parseUrl('/account/not-for-subaccounts');
} else {
return true;
}
});
} else {
return true;
}
} else {
// Its a UrlTree or similar
return success;
}
});

if(this.authGuard.wasLoggedIn && this.authGuard.currentMe) {
if (restricted) {
if (this.authGuard.currentMe.owner) {
return this.router.parseUrl('/account/not-for-subaccounts');
} else {
// Its a UrlTree or similar
return success;
return true;
}
});
} else {
return true;
}
}

return loginCheck;
}

canActivateChild(
Expand Down
45 changes: 25 additions & 20 deletions src/app/rmmapi/rmmauthguard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class RMMAuthGuardService implements CanActivate, CanActivateChild {

urlBeforeLogin = '';
wasLoggedIn = false;

loginPromise;
currentMe;

constructor(
Expand All @@ -40,26 +40,31 @@ export class RMMAuthGuardService implements CanActivate, CanActivateChild {
}

checkLogin(): Promise<boolean | UrlTree> {
return new Promise<boolean | UrlTree>((resolve, _reject) => {
this.isLoggedIn().pipe(take(1)).subscribe(
success => {
if (!success) {
resolve(this.router.parseUrl('/login'));
} else {
resolve(success);
}
},
error => {
if (error.status === 403) {
resolve(false);
} else {
// No indication that the user is unauthorized.
// Let them in, and have the httpinterceptor figure out what to do.
resolve(true);
if (!this.loginPromise) {
this.loginPromise = new Promise<boolean | UrlTree>((resolve, _reject) => {
this.isLoggedIn().pipe(take(1)).subscribe(
success => {
this.loginPromise = null;
if (!success) {
resolve(this.router.parseUrl('/login'));
} else {
resolve(success);
}
},
error => {
this.loginPromise = null;
if (error.status === 403) {
resolve(false);
} else {
// No indication that the user is unauthorized.
// Let them in, and have the httpinterceptor figure out what to do.
resolve(true);
}
}
}
);
});
);
});
}
return this.loginPromise;
}

isLoggedIn(): Observable<boolean | UrlTree> {
Expand Down

0 comments on commit 7befd67

Please sign in to comment.