Comments (9)
Yes, it will work. Thanks for your answer. Issue can be closed
from ngx-restangular.
Could you recreate in a plunkr?
from ngx-restangular.
@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.
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.
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.
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.
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.
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.
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)
- How to send array in Request Payload ? HOT 1
- PUT on a sub-property of an element HOT 2
- Error npm install with git branch HOT 6
- Custom headers emptying the headers map HOT 1
- Not working with Angular 8 HOT 1
- Remove deep import of core.js 3 HOT 2
- Support type defintions for ngx-restangular HOT 1
- Angular 9 Upgrade HOT 17
- Integrate HTTP_INTERCEPTORS of Angular as FullRequestInterceptors
- Define dependencies in package.json?
- Should `@types/lodash` be a non-dev dependency? HOT 1
- Why was use of HttpClient explicitly removed? HOT 2
- Enhance support for getMethodOverride in getList function HOT 3
- set default headers after login HOT 1
- Should it add query string to route? HOT 2
- Is this project still being maintained? HOT 2
- AuthService Code
- Uncaught Error: Type RestangularModule does not have 'ɵmod' property. HOT 1
- ngx-restangular still being maintained? Community fork? HOT 1
- The bug in defaultResponseInterceptor
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-restangular.