I want to make a singup function to get a token. Function should work like that:
- First I have to make a HTTP POST request passing login value to get "nonce" string
- Second, after first is done, when I have already "nonce" string, I have to make another HTTP POST request with login value and password encrypted like
SHA256(SHA256(password) + nonce))
I have a singup ractive form, and my on click button "onLogin" function use a service "singup(login, password)" function which return an observable to check if requests are done well (or not).
So I wonder how to properly implement sequence of requests to return to onLogin() function information (as observable) if one the one of them (requests) failed (i want to know if getNonce or login return error).
Auth Service:
singup(login: string, password: string) {
return this.getNonce(login).subscribe(result => {
const nonce = result.nonce;
return this.login(login, password, nonce);
});
};
login(login: string, password: string, nonce: string | undefined): Observable<any> {
return this.http.post<any>(`${this.config.url}/user/login`, {
login: login,
password: sha256(sha256(password) + nonce)
});
};
getNonce(login: string) {
return this.http.post<{ nonce: string }>(`${this.config.url}/user/login`, {
login: login
});
}
FormClass
onLogin() {
this.authService.singup(this.singupForm.value.login, this.singupForm.value.password).subscribe(result => {
// steps after get token or not
});
}
I will be thankful for good advices.