Flow Control on my Login Invisible to get New Token with Http Interceptor and Observable
I'm trying to implement a refresh login with HttpInterceptor, the problem is the flow is not the desired flow. To get new Token I do a Invisible Login with username and password.
My interceptor is:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
let token = localStorage.getItem('TOKEN');
let clone: HttpRequest<any>;
if (token)
let idUser = JSON.parse(this.jwtHelper.decodeToken(token).id);
if (idUser == localStorage.getItem('idUser'))
if (this.jwtHelper.isTokenExpired(token))
this._up.loginInvisibleEmail().subscribe(res =>
console.log("Token Refrescado con Email");
, error2 =>
//TODO CATCH ERROR
console.log("Error obteniendo refrescando token");
);
;
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`,
Authorization: `Bearer $token`
);
else
console.log("idUser diferentes");
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
console.log("Without TOKEN");
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`
);
console.log("HI INTERCEPTOR");
return next.handle(clone);
And the service that provide me the new token is :
loginInvisibleEmail()
let postParams = "email": localStorage.getItem("name"), "password": localStorage.getItem("password");
let url = URL_SERVICIOS + "/auth";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions(headers: headers);
return this.http2.post(url, JSON.stringify(postParams), options)
.map(resp =>
let data_resp = resp.json();
if (data_resp.error)
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
localStorage.removeItem("TOKEN");
localStorage.setItem('TOKEN', data_resp.result['access_token']);
, (error: any) =>
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
);
I'm trying to delete an element witht this method, and with expired token to check the flow .
deleteEducation(id): Observable<any>
let url = URL_SERVICIOS + '/users/educative-experience/' + id;
// let headers = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('TOKEN'));
return this.http
.delete(url)
.map(resp =>
return resp['result'];
)
The result flow is :
https://prnt.sc/lhdbxa
I need refresh token first to delete, in this case the first time I delete it fails but it refreshing token like the last step, if I delete it on second time it function well because the token was refreshing before.
How can I do this to refresh token on first time ?
Thank you in advance.
angular ionic3 angular-http-interceptors
add a comment |
I'm trying to implement a refresh login with HttpInterceptor, the problem is the flow is not the desired flow. To get new Token I do a Invisible Login with username and password.
My interceptor is:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
let token = localStorage.getItem('TOKEN');
let clone: HttpRequest<any>;
if (token)
let idUser = JSON.parse(this.jwtHelper.decodeToken(token).id);
if (idUser == localStorage.getItem('idUser'))
if (this.jwtHelper.isTokenExpired(token))
this._up.loginInvisibleEmail().subscribe(res =>
console.log("Token Refrescado con Email");
, error2 =>
//TODO CATCH ERROR
console.log("Error obteniendo refrescando token");
);
;
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`,
Authorization: `Bearer $token`
);
else
console.log("idUser diferentes");
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
console.log("Without TOKEN");
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`
);
console.log("HI INTERCEPTOR");
return next.handle(clone);
And the service that provide me the new token is :
loginInvisibleEmail()
let postParams = "email": localStorage.getItem("name"), "password": localStorage.getItem("password");
let url = URL_SERVICIOS + "/auth";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions(headers: headers);
return this.http2.post(url, JSON.stringify(postParams), options)
.map(resp =>
let data_resp = resp.json();
if (data_resp.error)
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
localStorage.removeItem("TOKEN");
localStorage.setItem('TOKEN', data_resp.result['access_token']);
, (error: any) =>
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
);
I'm trying to delete an element witht this method, and with expired token to check the flow .
deleteEducation(id): Observable<any>
let url = URL_SERVICIOS + '/users/educative-experience/' + id;
// let headers = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('TOKEN'));
return this.http
.delete(url)
.map(resp =>
return resp['result'];
)
The result flow is :
https://prnt.sc/lhdbxa
I need refresh token first to delete, in this case the first time I delete it fails but it refreshing token like the last step, if I delete it on second time it function well because the token was refreshing before.
How can I do this to refresh token on first time ?
Thank you in advance.
angular ionic3 angular-http-interceptors
add a comment |
I'm trying to implement a refresh login with HttpInterceptor, the problem is the flow is not the desired flow. To get new Token I do a Invisible Login with username and password.
My interceptor is:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
let token = localStorage.getItem('TOKEN');
let clone: HttpRequest<any>;
if (token)
let idUser = JSON.parse(this.jwtHelper.decodeToken(token).id);
if (idUser == localStorage.getItem('idUser'))
if (this.jwtHelper.isTokenExpired(token))
this._up.loginInvisibleEmail().subscribe(res =>
console.log("Token Refrescado con Email");
, error2 =>
//TODO CATCH ERROR
console.log("Error obteniendo refrescando token");
);
;
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`,
Authorization: `Bearer $token`
);
else
console.log("idUser diferentes");
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
console.log("Without TOKEN");
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`
);
console.log("HI INTERCEPTOR");
return next.handle(clone);
And the service that provide me the new token is :
loginInvisibleEmail()
let postParams = "email": localStorage.getItem("name"), "password": localStorage.getItem("password");
let url = URL_SERVICIOS + "/auth";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions(headers: headers);
return this.http2.post(url, JSON.stringify(postParams), options)
.map(resp =>
let data_resp = resp.json();
if (data_resp.error)
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
localStorage.removeItem("TOKEN");
localStorage.setItem('TOKEN', data_resp.result['access_token']);
, (error: any) =>
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
);
I'm trying to delete an element witht this method, and with expired token to check the flow .
deleteEducation(id): Observable<any>
let url = URL_SERVICIOS + '/users/educative-experience/' + id;
// let headers = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('TOKEN'));
return this.http
.delete(url)
.map(resp =>
return resp['result'];
)
The result flow is :
https://prnt.sc/lhdbxa
I need refresh token first to delete, in this case the first time I delete it fails but it refreshing token like the last step, if I delete it on second time it function well because the token was refreshing before.
How can I do this to refresh token on first time ?
Thank you in advance.
angular ionic3 angular-http-interceptors
I'm trying to implement a refresh login with HttpInterceptor, the problem is the flow is not the desired flow. To get new Token I do a Invisible Login with username and password.
My interceptor is:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
let token = localStorage.getItem('TOKEN');
let clone: HttpRequest<any>;
if (token)
let idUser = JSON.parse(this.jwtHelper.decodeToken(token).id);
if (idUser == localStorage.getItem('idUser'))
if (this.jwtHelper.isTokenExpired(token))
this._up.loginInvisibleEmail().subscribe(res =>
console.log("Token Refrescado con Email");
, error2 =>
//TODO CATCH ERROR
console.log("Error obteniendo refrescando token");
);
;
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`,
Authorization: `Bearer $token`
);
else
console.log("idUser diferentes");
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
console.log("Without TOKEN");
clone = request.clone(
setHeaders:
Accept: `application/json`,
'Content-Type': `application/json`
);
console.log("HI INTERCEPTOR");
return next.handle(clone);
And the service that provide me the new token is :
loginInvisibleEmail()
let postParams = "email": localStorage.getItem("name"), "password": localStorage.getItem("password");
let url = URL_SERVICIOS + "/auth";
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions(headers: headers);
return this.http2.post(url, JSON.stringify(postParams), options)
.map(resp =>
let data_resp = resp.json();
if (data_resp.error)
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
else
localStorage.removeItem("TOKEN");
localStorage.setItem('TOKEN', data_resp.result['access_token']);
, (error: any) =>
this._ctp.cleanStorage();
let nav = this.app.getActiveNav();
nav.setRoot('Login');
);
I'm trying to delete an element witht this method, and with expired token to check the flow .
deleteEducation(id): Observable<any>
let url = URL_SERVICIOS + '/users/educative-experience/' + id;
// let headers = new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('TOKEN'));
return this.http
.delete(url)
.map(resp =>
return resp['result'];
)
The result flow is :
https://prnt.sc/lhdbxa
I need refresh token first to delete, in this case the first time I delete it fails but it refreshing token like the last step, if I delete it on second time it function well because the token was refreshing before.
How can I do this to refresh token on first time ?
Thank you in advance.
angular ionic3 angular-http-interceptors
angular ionic3 angular-http-interceptors
asked Nov 12 at 13:19
SDLUJO
235
235
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263053%2fflow-control-on-my-login-invisible-to-get-new-token-with-http-interceptor-and-ob%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263053%2fflow-control-on-my-login-invisible-to-get-new-token-with-http-interceptor-and-ob%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown