Answer the question
In order to leave comments, you need to log in
How to get data in mergeMap?
Guys hello everyone. Tell me how to get a token in mergeMap
@Effect()
authSignin = this.actions$
.ofType(AuthActions.TRY_SIGNIN)
.pipe(
map((action: AuthActions.TrySignin) => {
return action.payload;
}),
switchMap((authData: { email: string; password: string }) => {
console.log(authData)
return this.httpClient.post('http://143.167.112.46:3001/sessions/create', authData)
}),
switchMap((token) => {
console.log(token)
const headers = new HttpHeaders().set(
'Authorization',
'bearer ' + token.id_token
);
return this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers})
}),
mergeMap((token: string, user) => {
console.log(user, token)
this.router.navigate(['/']);
return [
{
type: AuthActions.SIGNIN
},
{
type: AuthActions.SET_TOKEN,
payload: token
}
];
});
);
Answer the question
In order to leave comments, you need to log in
Well, you can do something like that, if you really want to.
switchMap((token) => {
console.log(token)
const headers = new HttpHeaders().set(
'Authorization',
'bearer ' + token.id_token
);
return this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers}).pipe(
map(user => {
return { user, token }
})
)
}),
mergeMap({ user, token }) => {
console.log(user, token)
this.router.navigate(['/']);
return [
{
type: AuthActions.SIGNIN
},
{
type: AuthActions.SET_TOKEN,
payload: token
}
];
});
this.httpClient.get('http://143.167.112.46:3001/api/protected/user-info', {headers})
and all other requests to the server in the service.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question