I'm trying to write a TypeScript method for my Angular web app that simply checks the validity of a JWT token on the server with an Http call.
I want this method to return a boolean value, telling my caller if the token is valid or not. I come from C# so i would expect I can do something like this and I haven't got a clear idea of how function returns are managed in JS/TS:
verifyCanActivate(): boolean {
const actionUrl = this.apiBaseUrl + 'ValidateToken';
this.http.get(actionUrl)
.map((data: HttpResponse<any>) => data.status === 200);}
An Http interceptor has already been set up, so every request I make is automatically decorated with header authentication info.
My server-side action for this controller is an empty ASP.NET Core WebAPI method, flagged with the [Authorize] attribute so I can easily check the validity of client tokens every time a call is made.
How do I have to write my TS method so it can return a boolean
value based on the HttpResponse
Status? I would also appreciate if you can provide a useful and up-to-date documentation about this topic in general.
Thank you in advance.
[Authorize]
? - dee zg[Authorize]
decorated endpoint gets hit by unauthorized user (or expired jwt) it should return401
. That's what you handle in interceptor and redirect to login page. - dee zg