Angular part of video lesson:
JWT Authorization and Token Refresh
Angular part of video lesson:
JWT Authorization and Token Refresh
Hallo!
Thx for your code! But it has an issue. If your token has expired and we call the refreshToken function on backend, and as example it response 401, too (our refresh token was expired, too as example). Then the code don't working. It hangs in "handle401Error" method. I think the reason for this is that the second 401 error (from refreshToken) cannot be handled - it goes to handle401Error, too and nothing else happened.
Use your code, change only the backend for returning new token/refresh token (for this example) to this as example:
return res.status(401).json({message: "some error in refresh token"})
Greetings, Flo
I can only get the handle401Error to work if I add subscribe() to the end of the pipe in the function observables. That allows my app to make a new request for a new token, but once the new calls are made with the new token, the page resolver doesn't receive the new data. I'm obviously doing something wrong.
private handle401Error(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const currToken = this.authenticationService.getToken();
if (!this.isRefreshing) {
this.isRefreshing = true;
this.refreshTokenSubject.next(null);
this.httpApiService.getAccessToken(currToken).pipe(
tap((response: any) => {
console.log("in refresh", response.body);
this.authenticationService.setAccessToken(response.body);
}),
switchMap((res) => {
this.isRefreshing = false;
this.refreshTokenSubject.next(res.body);
return next.handle(this.addToken(request, res.body));
})).subscribe()
} else {
this.refreshTokenSubject.pipe(
filter(token => token != null),
take(1),
switchMap(token => {
console.log("in refreshTokentSubject switchMap", token);
return next.handle(this.addToken(request, token));
})).subscribe()
}
}
}
Hello, how can I catch the RefreshToken API error and then redirect to the login page.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.