Giter Site home page Giter Site logo

Comments (9)

timaxapa avatar timaxapa commented on May 19, 2024 1

Yes, it will work. Thanks for your answer. Issue can be closed

from ngx-restangular.

mackelito avatar mackelito commented on May 19, 2024

Could you recreate in a plunkr?

from ngx-restangular.

timaxapa avatar timaxapa commented on May 19, 2024

@mackelito plunker
Open debugging console. By default, you will get both error messages, but if you remove error and finally callbacks for 1st subscriber - you also will not get an error message from the 2nd subscriber. I am a newbie in Rx programming, maybe I have a logic mistake in my code

from ngx-restangular.

mackelito avatar mackelito commented on May 19, 2024

in the service returning the object (this.restangular.one('posts/a')) instead of the result
https://plnkr.co/edit/UhfG7PTTuiBz5D60hgPm?p=preview

Note: updated my replay.. of course you can use the get() method :)

from ngx-restangular.

timaxapa avatar timaxapa commented on May 19, 2024

But in this case, we have two requests to the server. I can explain what I want. I have AuthService with SignIn method and SignInComponent. In component after submitting the form, I do request with AuthService and got a result as Observable. Then I need to save token I get. I want to save token in my service and return observable to component
In service
signIn(email: string, password: string): Observable<SignInResponse> { let observable$ = this.restangular.one(this.url).post('signIn', new SignInModel(email, password)); observable$.subscribe(res => { // save token to localStorage }); return observable$; }

In component
this.authService.signIn(formGroup.value.email, formGroup.value.password) .subscribe((response: SignInResponse) => { // redirects, etc });
But, If request returns error, Component subsriber doesn't work

from ngx-restangular.

mackelito avatar mackelito commented on May 19, 2024

hmmm.. not sure if I follow...
The issue is that you need to authenticate if you don´t have a valid token right?..

I did a solution for this exact senario just a few weeks ago... I ended up using canActivate/AuthGuard and it worked perfectly! :)

from ngx-restangular.

timaxapa avatar timaxapa commented on May 19, 2024

No ) I just want to have 2 subscribers on one request result: 1st in my service to save token, and 2nd in my component to redirect from sign in form.

from ngx-restangular.

mackelito avatar mackelito commented on May 19, 2024

so the senario is that you have a login component.. and once the user has submitted the form and has gotten the token you want to do a redirect.

So the service will have a login() method that you subscribe to and based on the response you get back you want to redirect if login was successful.. correct?

The service should not have .subscribe() it should return the observable that the component subscribes to.

If you need to store the token in localstorage you can do that in the service.. Here is an example.

authenticate(): Observable<boolean> {
  console.log('running authenticate()')
    const data =
      'grant_type=' + 'implicit' +
      '&client_id=' + 'WjFdWnyzE44pDUQXV1hLbawrgLYIP2ZanT6C8Cr6ri'

return this.http.post(this.authUrl, data, { headers: this.headers })
  .flatMap((response: Response) => {
    return localforage.setItem('auth', response.json()).then(res => {
      this.token = _.get(res, ['access_token'])
      this.tokenExpires = _.get(res, ['expires'])
      return !_.isUndefined(this.token)
    })
  })
}

from ngx-restangular.

mackelito avatar mackelito commented on May 19, 2024

I´m using http instead of restangular here because of this issue, https://github.com/2muchcoffeecom/ng2-restangular/issues/14#issuecomment-272466188

from ngx-restangular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.