I’ve tried many times with many ways, but I still can’t get token from Capacitor Storage, to send this into request’s Authorization header. Can you help me?
This code don’t manipulate request.
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import {
Storage
} from '@capacitor/core';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
Storage.get({ key: 'token' }).then(token => {
console.log(token);
request = request.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
});
return next.handle(request);
}
}
but this code returns instead of a string with token, an [object Promise]:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';
import {
Storage
} from '@capacitor/core';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${Storage.get({ key: 'token' }).then(token => { return token;
})}`
}});
return next.handle(request);
}
}