andreadc

example.authentication.angular.ts

May 12th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //COMPONENT
  2. this.authenticationService.login(this.f.username.value, this.f.password.value)
  3.           .pipe(
  4.           take(1)
  5.           ).subscribe({
  6.  
  7.             next:() => {
  8.               this.user.next(this.authenticationService.currentUserValue);
  9.               this.isLoggedIn = this.authenticationService.isLoggedIn();
  10.  
  11.             },
  12.  
  13.             error:error => {
  14.               console.error(error);
  15.               this.errorsService.sendError(error);
  16.               this.errorsService.registerError(error);
  17.             }
  18.           }
  19.       );
  20. //SERVICE
  21. login(username, password) {
  22.        
  23.         let params = new HttpParams()
  24.             .set('username', username)
  25.             .set('password', password);
  26.        
  27.         let options = { headers: this.headers, params: params };
  28.        
  29.         // http.get() ritorna un observable quindi la funzione ritorna l'observable di http.get()
  30.         // e nel componente mi sottoscrivo all' observable
  31.         return this.http.get<any>(serverUrl + 'login', options)
  32.             .pipe(
  33.                 map(data => {
  34.                     this.setSession(data)
  35.                     this.currentUserSubject.next(data.user)                
  36.                 }),
  37.                 retry(3)
  38.             );
  39.  
  40.     }
Add Comment
Please, Sign In to add comment