Skip to content
Snippets Groups Projects
Select Git revision
  • df97cf010d2209ad0471695d8f225f903f2ce14e
  • master default
  • autorisation_visit
  • signup_mydata
  • format_editData
  • dev
  • authorize-valid-profile
  • feli90-patch-2
  • feli90-patch-1
  • context_sheet
  • testNewForm
  • FeliLocalTest1
  • ios-account-fix
  • change-form-link
  • dev_pages_projet
  • remise_sorties
  • release/first-users
17 results

auth.service.ts

  • auth.service.ts 3.46 KiB
    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Router } from '@angular/router';
    import { Observable, BehaviorSubject } from 'rxjs';
    import { tap, map } from 'rxjs/operators';
    import { environment } from 'environments/environment';
    import { User } from './models';
    import { ObjectStoredItem, SimpleStoredItem } from '../storage';
    import { UserAdapter } from './adapters';
    import { HttpParams } from '@angular/common/http';
    
    
    class StoredUser extends ObjectStoredItem<User> { key = 'oser-cs-user-info'; }
    class StoredToken extends SimpleStoredItem { key = 'oser-cs-user-token'; }
    
    
    @Injectable({
      providedIn: 'root',
    })
    export class AuthService {
    
      private loginUrl = environment.apiUrl  + 'auth/get-token/';
      private resetUrl = environment.apiUrl + 'rest-auth/password/reset/';
      private resetConfirmUrl = environment.apiUrl + 'rest-auth/password/reset/confirm/';
      private mandatorySignatureUrl = environment.apiUrl + 'charter/'; 
      //add the corresponding path in backend interface 
    
    
      fromGuard: boolean;
      redirectUrl: string;
      fromUnauthorized: boolean;
    
      private userAdapter = new UserAdapter();
      private user = new StoredUser();
      private token = new StoredToken();
      private user$: BehaviorSubject<User>;
    
      constructor(private http: HttpClient, private router: Router) {
        const userData = this.getUserSnapshot();
        const initialUser = userData ? new User(userData) : null;
        this.user$ = new BehaviorSubject(initialUser);
      }
    
      login(username: string, password: string): Observable<boolean> {
        return this.http.post<any>(this.loginUrl, { username: username, password: password }).pipe(
          tap(data => this.token.set(data.token)),
          map(data => this.userAdapter.adapt(data.user)),
          tap((user: User) => this.user.set(user)),
          tap((user: User) => this.user$.next(user)),
          map(() => true),
        );
      }
    
      reset(email: string): Observable<boolean> {
        console.log("reset function of auth service");
        return this.http.post<any>(this.resetUrl, { email }).pipe(
          map(() => true),
        );
      }
    
      resetConfirm(uid: string, token: string, new_password1: string, new_password2: string): Observable<boolean> {
        console.log("reset confirm function");
        return this.http.post<any>(this.resetConfirmUrl, { uid, token, new_password1, new_password2 }).pipe(
          map(() => true),
        );
      }  
    
      redirectLogin() {
        this.router.navigate(['/connexion']);
      }