{"version":3,"file":"ng-vagabond-lab-ng-dsv-modules-auth.mjs","sources":["../../../projects/ng-dsv/modules/auth/component/auth.component.ts","../../../projects/ng-dsv/modules/auth/component/auth.component.html","../../../projects/ng-dsv/modules/auth/guard/auth.guard.ts","../../../projects/ng-dsv/modules/auth/service/auth.service.ts","../../../projects/ng-dsv/modules/auth/service/auth.google.service.ts","../../../projects/ng-dsv/modules/auth/utils/auth.utils.ts","../../../projects/ng-dsv/modules/auth/ng-vagabond-lab-ng-dsv-modules-auth.ts"],"sourcesContent":["import { CommonModule } from '@angular/common';\nimport {\n  afterNextRender,\n  Component,\n  effect,\n  inject,\n  output,\n  signal,\n} from '@angular/core';\nimport { ID } from '@ng-vagabond-lab/ng-dsv/api';\nimport {\n  ModalAlertComponent,\n  ModalButtonComponent,\n} from '@ng-vagabond-lab/ng-dsv/ds/modal';\nimport { EnvironmentService } from '@ng-vagabond-lab/ng-dsv/environment';\nimport { AuthGoogleService, AuthService } from '../public-api';\n\n@Component({\n  selector: 'app-auth',\n  imports: [CommonModule, ModalButtonComponent, ModalAlertComponent],\n  templateUrl: './auth.component.html',\n  styleUrls: ['./auth.component.scss'],\n})\nexport class AuthComponent {\n  readonly authGoogleService = inject(AuthGoogleService);\n  readonly authService = inject(AuthService);\n  readonly environmentService = inject(EnvironmentService);\n\n  readonly initMember = output<ID>();\n\n  readonly ssrRendered = signal(false);\n\n  constructor() {\n    afterNextRender(() => {\n      this.ssrRendered.set(true);\n    });\n    effect(() => {\n      if (this.authService.userConnected() === null) {\n        this.ssrRendered() && this.authGoogleService.loginWithGoogle();\n      } else {\n        this.initMember.emit(this.authService.userConnected()?.user?.id);\n      }\n    });\n    effect(() => {\n      if (this.environmentService.env() && this.ssrRendered()) {\n        this.authService.loginFromCache();\n        this.authGoogleService.initGoogleAuth();\n      }\n    });\n  }\n\n  logout() {\n    this.authService.logout();\n  }\n}\n","<div class=\"auth-button\">\n  <button\n    id=\"google-signin-button\"\n    [class.hidden]=\"authService.userConnected() !== null\"\n  ></button>\n</div>\n\n@if (authService.userConnected()) {\n  <div class=\"profile\">\n    <img [src]=\"authService.userConnected()?.user?.avatar\" alt=\"profile\" />\n    <app-modal-button id=\"logout\" icon=\"ri-logout-box-line\" />\n  </div>\n  <app-modal-alert\n    id=\"logout\"\n    title=\"Déconnexion\"\n    text=\"Voulez-vous vraiment vous déconnecter ?\"\n    button=\"Oui\"\n    buttonClose=\"Non\"\n    (callback)=\"logout()\"\n  ></app-modal-alert>\n}\n","import { inject } from '@angular/core';\nimport { ActivatedRouteSnapshot, CanActivateFn, Router } from '@angular/router';\nimport { PlatformService } from '@ng-vagabond-lab/ng-dsv/platform';\nimport { AuthService, hasRole } from '../public-api';\n\nexport const authGuard: CanActivateFn = (\n    route: ActivatedRouteSnapshot,\n) => {\n    const platformService = inject(PlatformService);\n\n    if (!platformService.isPlatformBrowser()) {\n        return true;\n    }\n\n    const authService = inject(AuthService);\n    const router = inject(Router);\n\n    const requiredRole = route.data['role'];\n\n    if (!requiredRole) {\n        console.warn('No role specified in route data.');\n        return false;\n    }\n\n    authService.loginFromCache();\n    const profiles = authService.userConnected()?.user?.profiles;\n    if (hasRole(requiredRole, profiles)) {\n        return true;\n    }\n\n    router.navigate(['/']);\n    return false;\n};\n","import { inject, Injectable, signal } from '@angular/core';\nimport { ApiService } from '@ng-vagabond-lab/ng-dsv/api';\nimport { ToastService } from '@ng-vagabond-lab/ng-dsv/ds/toast';\nimport { StorageService } from '@ng-vagabond-lab/ng-dsv/storage';\nimport { UserConnectedDto } from '../dto/user.dto';\n\n@Injectable({\n  providedIn: 'root',\n})\nexport class AuthService {\n  apiService = inject(ApiService);\n  toastService = inject(ToastService);\n  storageService = inject(StorageService);\n\n  userConnected = signal<UserConnectedDto | null>(null);\n\n  googleLogin(credential: string) {\n    this.apiService.post<UserConnectedDto>(\n      'auth/google-identity-connect',\n      {\n        googleToken: credential,\n      },\n      (data) => {\n        this.storageService.setItem('user-connected', JSON.stringify(data));\n        this.userConnected.set(data);\n        this.toastService.showToast({\n          type: 'success',\n          text: 'Connexion réussie',\n        });\n      }\n    );\n  }\n\n  loginFromCache() {\n    const userConnected =\n      typeof window !== 'undefined' &&\n      JSON.parse(this.storageService?.getItem('user-connected')!);\n    this.userConnected.set(userConnected);\n    this.apiService.info('userConnected', userConnected);\n    return userConnected;\n  }\n\n  logout() {\n    this.storageService?.removeItem('user-connected');\n    this.userConnected.set(null);\n    this.toastService.showToast({\n      type: 'success',\n      text: 'Déconnexion réussie',\n    });\n  }\n}\n","import { inject, Injectable } from '@angular/core';\nimport { EnvironmentService } from '@ng-vagabond-lab/ng-dsv/environment';\nimport { AuthService } from './auth.service';\n\ndeclare const google: any;\n\n@Injectable({\n  providedIn: 'root',\n})\nexport class AuthGoogleService {\n  private readonly authService = inject(AuthService);\n  private readonly environmentService = inject(EnvironmentService);\n\n  initGoogleAuth() {\n    google.accounts.id.initialize({\n      client_id: this.environmentService.env()?.GOOGLE_CLIENT_ID,\n      callback: this.handleCredentialResponse.bind(this),\n    });\n    google.accounts.id.renderButton(\n      document.getElementById('google-signin-button')!,\n      {\n        theme: 'outline',\n        size: 'medium',\n        type: 'icon',\n      }\n    );\n  }\n\n  handleCredentialResponse(response: { credential: string }) {\n    this.authService.googleLogin(response.credential);\n  }\n\n  decodeJwtToken(token: string) {\n    const base64Url = token.split('.')[1];\n    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');\n    const jsonPayload = decodeURIComponent(\n      atob(base64)\n        .split('')\n        .map(function (c) {\n          return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);\n        })\n        .join('')\n    );\n    return JSON.parse(jsonPayload);\n  }\n\n  loginWithGoogle() {\n    google?.accounts.id.prompt();\n  }\n}\n","import { ApiDto } from \"@ng-vagabond-lab/ng-dsv/api\";\n\nexport const hasRole = (role: string, userRoles?: ApiDto[]) => {\n    return userRoles?.find(userRole => userRole['name' as keyof ApiDto] === role) !== undefined;\n}","/**\n * Generated bundle index. Do not edit.\n */\n\n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