現象
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;
}
}