I have these methods in my auth.service and an auth.interceptor.ts code in order to take the token from header and I want to send it with post method what should add in header in order to set the token? Any help is much appreciated!!
//shop.service.ts
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json',
'Authorization': 'X-OBSERVATORY-AUTH'})
};
addShop (shop: Shop): Observable<Shop> {
return this.http.post<Shop>(this.shopsUrl, shop, httpOptions);
}
// auth.interceptor.ts
intercept(req: HttpRequest<any>, next: HttpHandler) {
const authToken = this.authService.getToken();
const authRequest = req.clone({
headers: req.headers.set("Authorization", "Bearer " + authToken)
});
return next.handle(authRequest);
}
//auth.service.ts
getToken() {
return this.token;
}
login( username: string, password: string) {
var user: User = { username: username, password: password };
this.http
.post<any>("http://localhost:3000/observatory/api/login",user,
{observe:'response'})
.subscribe((res) => {
const token = res.headers.get('X-OBSERVATORY-AUTH');
console.log(token);
this.token = token;
if (token!==null) {
this.isAuthenticated = true;
this.userId = res.body._id;
this.isAdmin=res.body.isAdmin;
this.userAdmin=res.body.isAdmin;
this.username=res.body.username;
this.authStatusListener.next(true);
this.saveAuthData(token, this.userId,this.username, this.isAdmin);
} });