Browse Source

feat(admin-ui): Add `loginUrl` option to support external login pages

Relates to #215
Michael Bromley 5 years ago
parent
commit
2745146166

+ 7 - 1
packages/admin-ui/src/lib/core/src/components/app-shell/app-shell.component.ts

@@ -3,6 +3,7 @@ import { Router } from '@angular/router';
 import { EMPTY, Observable } from 'rxjs';
 import { map, switchMap, take } from 'rxjs/operators';
 
+import { getAppConfig } from '../../app.config';
 import { LanguageCode } from '../../common/generated-types';
 import { DataService } from '../../data/providers/data.service';
 import { AuthService } from '../../providers/auth/auth.service';
@@ -64,7 +65,12 @@ export class AppShellComponent implements OnInit {
 
     logOut() {
         this.authService.logOut().subscribe(() => {
-            this.router.navigate(['/login']);
+            const { loginUrl } = getAppConfig();
+            if (loginUrl) {
+                window.location.href = loginUrl;
+            } else {
+                this.router.navigate(['/login']);
+            }
         });
     }
 }

+ 11 - 2
packages/admin-ui/src/lib/core/src/providers/guard/auth.guard.ts

@@ -3,6 +3,7 @@ import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
 import { Observable } from 'rxjs';
 import { tap } from 'rxjs/operators';
 
+import { getAppConfig } from '../../app.config';
 import { AuthService } from '../auth/auth.service';
 
 /**
@@ -13,13 +14,21 @@ import { AuthService } from '../auth/auth.service';
     providedIn: 'root',
 })
 export class AuthGuard implements CanActivate {
-    constructor(private router: Router, private authService: AuthService) {}
+    private readonly externalLoginUrl: string | undefined;
+
+    constructor(private router: Router, private authService: AuthService) {
+        this.externalLoginUrl = getAppConfig().loginUrl;
+    }
 
     canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
         return this.authService.checkAuthenticatedStatus().pipe(
             tap(authenticated => {
                 if (!authenticated) {
-                    this.router.navigate(['/login']);
+                    if (this.externalLoginUrl) {
+                        window.location.href = this.externalLoginUrl;
+                    } else {
+                        this.router.navigate(['/login']);
+                    }
                 }
             }),
         );

+ 8 - 0
packages/common/src/shared-types.ts

@@ -180,6 +180,14 @@ export interface AdminUiConfig {
      * @default [LanguageCode.en, LanguageCode.es]
      */
     availableLanguages: LanguageCode[];
+    /**
+     * @description
+     * If you are using an external {@link AuthenticationStrategy} for the Admin API, you can configure
+     * a custom URL for the login page with this option. On logging out or redirecting an unauthenticated
+     * user, the Admin UI app will redirect the user to this URL rather than the default username/password
+     * screen.
+     */
+    loginUrl?: string;
 }
 
 /**