0
0

More than 1 year has passed since last update.

NestJSでAWSCognitoからJWT指定でユーザ情報取得を行うと「ConfigError: Missing region in config」

Last updated at Posted at 2021-10-30

現象

NestJSでAWSCognitoからJWT指定でユーザ情報取得を行うと、「ConfigError: Missing region in config」が発生する。

cognito.service.ts
@Injectable()
export class CognitoService {
  private client: CognitoIdentityServiceProvider;
  protected user: GetUserResponse;
  constructor() {
    this.client = new CognitoIdentityServiceProvider();
  }
  public async getUserByToken(token: string): Promise<GetUserResponse> {
    this.user = await this.client
      .getUser({
        AccessToken: token,
      })
      .promise();
    return this.user;
  }
}

解決

CognitoIdentityServiceProviderのインスタンスを生成する際に、間違ったregionが使用されているっぽい。
なのでインスタンス生成時にregionを指定するように修正してやるとうまくいった。

cognito.service.ts
@Injectable()
export class CognitoService {
  private client: CognitoIdentityServiceProvider;
  protected user: GetUserResponse;
  constructor() {
    // ※※※※※※※※※※以下を修正※※※※※※※※※※
    // this.client = new CognitoIdentityServiceProvider();
    this.client = new CognitoIdentityServiceProvider({
      region: 'ap-northeast-1', // 自分が使用しているユーザプールのRegionを指定
    });
  }
  public async getUserByToken(token: string): Promise<GetUserResponse> {
    this.user = await this.client
      .getUser({
        AccessToken: token,
      })
      .promise();
    return this.user;
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0