0

The userdetail method returns the token and other info which I need in the local component and I am getting it correctly. The token however needs to be sent in someMethod as a header so I am passing the access_token when calling the method in the auth services. Now in the auth service in someMethod when calling the api I need to send it as authorization header with bearer just like we do in postman

local component

userDetail: any = null;
    access_token;
      constructor(private auth: AuthService, private activatedRoute: ActivatedRoute) { }
      ngOnInit() {
       this.activatedRoute.data.subscribe((data: any)=> {
       this.elcUserDetail = data.elcUserDetailResolver.body.data;
       this.access_token = this.elcUserDetail.access_token;
       console.log(this.access_token);
       })

    if(this.userDetail != null){
    this.auth.someMethod(this.access_token).subscribe((res)={
    })
   }
 }

Auth service

headers: any;

    userdetail(): Observable<any> {
      return this.http.get(this.userdetailURL, {observe: 'response'});
    }

    someMethod(access_token) {
    const headers =  new Headers({ Authorization: `Bearer this.headers`});
   }
    return this.http.post(this.someURL, null,{headers: this.headers } );
    }

user Detail resolve

export class UserDetailResolveService implements Resolve<any> {
  constructor(private auth: AuthService, private http: HttpClient) { }
  resolve(route: ActivatedRouteSnapshot) {
    return this.auth.userdetail();
  }
cupusasa
  • 35
  • 1
  • 7
  • 1
    Can you create a [mcve]? maybe use https://stackblitz.com/fork/angular – Oram Mar 20 '19 at 14:23
  • 1
    Possible duplicate of [How to add Authorization Header to Angular http request?](https://stackoverflow.com/questions/47400929/how-to-add-authorization-header-to-angular-http-request) – Martin Schneider Mar 20 '19 at 14:46

0 Answers0