如何将 Auth 服务导入 environment.ts 文件并使用令牌

问题描述 投票:0回答:0

我有一个获取访问令牌的 auth.service.ts 文件

auth.service.ts

  public async getAccessToken(): Promise<string | undefined> {
    await this.initialize();
    const res = await this.getAuthResult();
    this.bearerToken = res?.accessToken;
    return res?.accessToken;
  }

我已经能够使用

将身份验证令牌导入其他页面
import { AuthService } from '../../services/auth/auth.service';

async getToken() {
  await this.auth.getAccessToken();
  console.log("token", this.auth.bearerToken);
}

如果可能,我要做的是将此令牌导入 environment.ts 文件中的“x-authorization-token”

环境.ts

import { HttpHeaders } from '@angular/common/http';
import { AuthService } from '../app/services/auth/auth.service';

export const environment = { production: false };

export const headerAuth: any = {
  headers: new HttpHeaders({
    'x-authorization-token': '', // To be imported from Auth Service
    'access-control-allow-origin': 'https://XXXXX.fred.com/',
    'access-control-allow-methods': 'GET,POST,OPTIONS',
    'access-control-allow-headers': 'Content-Type, X-Authorization-Token, Origin',
    'content-type': 'application/json',
  })
}
angular ionic-framework authorization
© www.soinside.com 2019 - 2024. All rights reserved.