{"version":3,"file":"cccteam-ccc-lib-auth-authentication-guard.mjs","sources":["../../../projects/ccc-lib/auth-authentication-guard/authentication.guard.ts","../../../projects/ccc-lib/auth-authentication-guard/cccteam-ccc-lib-auth-authentication-guard.ts"],"sourcesContent":["import { inject } from '@angular/core';\nimport { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';\nimport { AuthService } from '@cccteam/ccc-lib/auth-service';\nimport { API_URL, BASE_URL } from '@cccteam/ccc-lib/types';\nimport { Observable, of } from 'rxjs';\nimport { catchError, map, switchMap } from 'rxjs/operators';\n\n/**\n * Route guard that protects routes using OIDC (OpenID Connect) authentication.\n *\n * If the user is not authenticated, they are redirected to the external OIDC\n * login endpoint with the current URL encoded as a return URL parameter.\n *\n */\nexport const OIDCAuthenticationGuard = (\n  _: ActivatedRouteSnapshot,\n  routerState: RouterStateSnapshot,\n): Observable<boolean> => {\n  const authService = inject(AuthService);\n  const baseUrl = inject(BASE_URL);\n  const apiUrl = inject(API_URL);\n\n  const authenticate = (): void => {\n    const url = routerState.url;\n    const absoluteUrl = baseUrl + (!url.toString().startsWith('/') ? '/' + url : url);\n    const encodedUrl = encodeURIComponent(absoluteUrl);\n    window.location.href = `${apiUrl}/user/login?returnUrl=${encodedUrl}`;\n  };\n\n  if (authService.authenticated()) {\n    return of(true);\n  }\n\n  return authService.checkUserSession().pipe(\n    map((sessionInfo) => {\n      if (sessionInfo?.authenticated) {\n        return true;\n      }\n\n      authenticate();\n      return false;\n    }),\n    catchError(() => {\n      authenticate();\n      return of(false);\n    }),\n  );\n};\n\n/**\n * Route guard that protects routes using internal login-based authentication.\n *\n * If the user is not authenticated, they are redirected to the application's\n * internal FRONTEND_LOGIN_PATH route. The attempted URL is stored in the AuthService so the\n * user can be redirected back after successful login.\n *\n */\nexport const LoginAuthenticationGuard: CanActivateFn = (\n  _: ActivatedRouteSnapshot,\n  routerState: RouterStateSnapshot,\n) => {\n  const router = inject(Router);\n  const authService = inject(AuthService);\n\n  if (authService.authenticated()) {\n    return of(true);\n  }\n\n  return authService.checkUserSession().pipe(\n    switchMap((sessionInfo) => {\n      if (sessionInfo?.authenticated) {\n        return of(true);\n      }\n\n      return setRedirectAndGetLoginUrlTree(authService, router, routerState.url);\n    }),\n    catchError(() => {\n      return setRedirectAndGetLoginUrlTree(authService, router, routerState.url);\n    }),\n  );\n};\n\n/**\n * Impure function used to handle redirecting user to login\n *\n * Has the side effect of storing the url the user wished to access\n * in AuthService\n */\nconst setRedirectAndGetLoginUrlTree = (authService: AuthService, router: Router, redirectUrl: string) => {\n  authService.redirectUrl.set(redirectUrl);\n  return of(router.createUrlTree([authService.loginRoute()]));\n};\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;;AAOA;;;;;;AAMG;MACU,uBAAuB,GAAG,CACrC,CAAyB,EACzB,WAAgC,KACT;AACvB,IAAA,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;AACvC,IAAA,MAAM,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC;AAChC,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAE9B,MAAM,YAAY,GAAG,MAAW;AAC9B,QAAA,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG;QAC3B,MAAM,WAAW,GAAG,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AACjF,QAAA,MAAM,UAAU,GAAG,kBAAkB,CAAC,WAAW,CAAC;QAClD,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,GAAG,MAAM,CAAA,sBAAA,EAAyB,UAAU,CAAA,CAAE;AACvE,IAAA,CAAC;AAED,IAAA,IAAI,WAAW,CAAC,aAAa,EAAE,EAAE;AAC/B,QAAA,OAAO,EAAE,CAAC,IAAI,CAAC;IACjB;AAEA,IAAA,OAAO,WAAW,CAAC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