import { AuthenticationDetails , CognitoUser , CognitoUserAttribute , CognitoUserPool } from "amazon-cognito-identity-js" ; export class CognitoClient { private _userPool : CognitoUserPool ; private _userId : string | null = null ; get userId ( ) : string | null { return this . _userId ; } set userId ( val ) { this . _userId = val ; } private _password : string | null = null ; get password ( ) : string | null { return this . _password ; } private _idToken : string | null = null ; get idToken ( ) : string | null { return this . _idToken ; } set idToken ( val ) { this . _idToken = val ; } private _accessToken : string | null = null ; get accessToken ( ) : string | null { return this . _accessToken ; } set accessToken ( val ) { this . _accessToken = val ; } private _refreshToken : string | null = null ; get refreshToken ( ) : string | null { return this . _refreshToken ; } set refreshToken ( val ) { this . _refreshToken = val ; } constructor ( userPoolId , clientId , defaultUserId = null , defaultPassword = null ) { this . _userPool = new CognitoUserPool ( { UserPoolId : userPoolId , ClientId : clientId , } ) ; this . _userId = defaultUserId ; this . _password = defaultPassword ; } signIn = async ( userId , password ) => { const authenticationDetails = new AuthenticationDetails ( { Username : userId , Password : password , } ) ; const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; const p = new Promise < void > ( ( resolve , reject ) => { cognitoUser . authenticateUser ( authenticationDetails , { onSuccess : ( result ) => { this . _accessToken = result . getAccessToken ( ) . getJwtToken ( ) ; this . _idToken = result . getIdToken ( ) . getJwtToken ( ) ; this . _refreshToken = result . getRefreshToken ( ) . getToken ( ) ; this . _userId = userId ; resolve ( ) ; } , onFailure : ( err ) => { reject ( err ) ; } , } ) ; } ) ; await p ; return ; } ; signUp = async ( userId , password ) => { const attributeList = [ new CognitoUserAttribute ( { Name : "email" , Value : userId , } ) , ] ; const p = new Promise < void > ( ( resolve , reject ) => { this . _userId = userId ; this . _userPool . signUp ( userId , password , attributeList , [ ] , ( err , result ) => { if ( err ) { reject ( err ) ; return ; } resolve ( ) ; } ) ; } ) ; await p ; return ; } ; verify = async ( userId , verifyCode ) => { const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; const p = new Promise < void > ( ( resolve , reject ) => { this . _userId = userId ; cognitoUser . confirmRegistration ( verifyCode , true , ( err ) => { if ( err ) { reject ( err ) ; return ; } resolve ( ) ; } ) ; } ) ; await p ; return ; } ; resendVerification = async ( userId ) => { const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; const p = new Promise < void > ( ( resolve , reject ) => { this . _userId = userId ; cognitoUser . resendConfirmationCode ( ( err ) => { if ( err ) { reject ( err ) ; return ; } resolve ( ) ; } ) ; } ) ; await p ; return ; } ; sendVerificationCodeForChangePassword = async ( userId ) => { const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; const p = new Promise < void > ( ( resolve , reject ) => { this . _userId = userId ; cognitoUser . forgotPassword ( { onSuccess : ( data ) => { resolve ( ) ; } , onFailure : ( err ) => { reject ( err ) ; } , } ) ; } ) ; await p ; return ; } ; changePassword = async ( userId , verifycode , password ) => { const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; const p = new Promise < void > ( ( resolve , reject ) => { this . _userId = userId ; cognitoUser . confirmPassword ( verifycode , password , { onSuccess : ( ) => { resolve ( ) ; } , onFailure : ( err ) => { reject ( err ) ; } , } ) ; } ) ; await p ; return ; } ; signOut = async ( userId ) => { const cognitoUser = new CognitoUser ( { Username : userId , Pool : this . _userPool , } ) ; cognitoUser . signOut ( ) ; } ; }