Using Interface with observable

Asked

Viewed 44 times

0

I created an Interface to save user session information to use with an observable, idea and set when the user logs in and the other pages, and a service for HTTP requests will do the get of this Interface. The service for requesting Undefined after getting a get on the interface.

export interface ICredential {
    IsLogged?: boolean;
    XAccessToken?: string;
    UserId?: string;
}

Page with login form

export class HomePage {
  _ICredential: ICredential;
 
.....

  GoogleSingIn(): void {
    this._SocialAuthService.signIn(GoogleLoginProvider.PROVIDER_ID).then(
      Data => {
      ....

         this._ICredential = {
            XAccessToken: this.XAccessToken,
            UserId: this.UserId,
            IsLogged: true,
          }
      })
     })
  }

In service Session I created the observable

  Session(): Observable<ICredential> {
    return new Observable<ICredential>(observer => {
      observer.next(this._ICredential);
    });
  }

in the service with HTTP requests, I send a console.log and return Undefined

 this._SessionService.Session().subscribe(
      data => { 
        console.log(data.XAccessToken);
        this.XAccessToken = data.XAccessToken  }, 
      error => alert(JSON.stringify(error))
    )
  • The interface itself doesn’t have much to do with your problem, when you sign up for Session() Subscription is returning something ? You are making this application before or after you have called the method GoogleSingIn and how much such a method should be called? ... The interface simply serves to say what format your data has, whether or not you are receiving something has nothing to do with the interface

  • Session is returning Undefined. I log in with Googlesigin and on the next page I try to recover data from the Interface. So my interpretation was wrong about the interface storing the data?

  • Exactly, interfaces do not store any data except the format that the data that implement the interface should have, let me see if I understood the problem well: On page A you log in and redirect the user to page B where you call the method Session() and hopes to get the data you’ve stored before, that’s it?

  • That’s right. Just as the next pages will use data that would be from Icredential to validate the session.

  • This way it will never work, you set your _ICredential in the class HomePage which I imagine to be its component, while its method Session is in the service _SessionService which has never had any variable startup, create a single service or use what you already have in the case _SessionService to authenticate and save the values you need, this way you do not lose this data when you browse between pages.

  • I get it. Icredential is inside an Interface folder and I really use the Homepage as a home / login, so I will create inside this page the observable to return this information. The idea of _Sessionservice is to return session data

  • Within the home I created a call to the service login and within this I created Behaviorsubject with the information I need during the user’s session. Posting here the final solution for people with little knowledge, as I rs.

Show 2 more comments

1 answer

0

Solution to my doubt. When the user clicks on the button to log in with Google’s social network, I call login service to log in.

 export class HomePage {
  
   GoogleSingIn(): void {
    this._SocialAuthService.signIn(GoogleLoginProvider.PROVIDER_ID).then(
      Data => {
        this._LoginService.SignIn(Data.email, Data.name);
      })
  }

Inside the login service I create Behaviorsubject and after login I give a set with the correct values.

private IsLoggedIn = new BehaviorSubject<boolean>(false);
  get _IsLoggedIn() {
    return this.IsLoggedIn.asObservable();
  }

  private Token = new BehaviorSubject<any>("");
  get _Token() {
    return this.Token.asObservable();
  }

  private UUID = new BehaviorSubject<any>("");
  get _UUID() {
    return this.UUID.asObservable();
  }


   SignIn(UserEmail: any, UserName: any) {
  
    ...

      this.IsLoggedIn.next(true);
      this.Token.next(this.XAccessToken);
      this.UUID.next(this.UserId);

    })
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.