import { CognitoClient } from "@dannadori/flect-amazon-chime-lib2" ; import { useMemo , useRef , useState } from "react" ; export type UseCognitoClientProps = { userPoolId : string ; userPoolClientId : string ; defaultUserId ? : string ; defaultPassword ? : string ; } ; export type CognitoClientState = { userId : string | null ; password : string | null ; idToken : string | null ; accessToken : string | null ; refreshToken : string | null ; signIn : ( userId : string , password : string ) => Promise < void > ; signUp : ( userId : string , password : string ) => Promise < void > ; verify : ( userId : string , verifyCode : string ) => Promise < void > ; resendVerification : ( userId : string ) => Promise < void > ; sendVerificationCodeForChangePassword : ( userId : string ) => Promise < void > ; changePassword : ( userId : string , verifycode : string , password : string ) => Promise < void > ; signOut : ( userId : string ) => Promise < void > ; } ; export const useCognitoClient = ( props ) => { const [ _lastUpdateTime , setLastUpdateTime ] = useState ( 0 ) ; const cognitoClient = useMemo ( ( ) => { const c = new CognitoClient ( props . userPoolId , props . userPoolClientId , props . defaultUserId , props . defaultPassword ) ; return c ; } , [ ] ) ; const signIn = async ( userId , password ) => { await cognitoClient . signIn ( userId , password ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const signUp = async ( userId , password ) => { await cognitoClient . signUp ( userId , password ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const verify = async ( userId , verifyCode ) => { await cognitoClient . verify ( userId , verifyCode ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const resendVerification = async ( userId ) => { await cognitoClient . resendVerification ( userId ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const sendVerificationCodeForChangePassword = async ( userId ) => { await cognitoClient . sendVerificationCodeForChangePassword ( userId ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const changePassword = async ( userId , verifycode , password ) => { await cognitoClient . changePassword ( userId , verifycode , password ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const signOut = async ( userId ) => { await cognitoClient . signOut ( userId ) ; setLastUpdateTime ( new Date ( ) . getTime ( ) ) ; } ; const returnValue = { userId : cognitoClient . userId , password : cognitoClient . password , idToken : cognitoClient . idToken , accessToken : cognitoClient . accessToken , refreshToken : cognitoClient . refreshToken , signIn , signUp , verify , resendVerification , sendVerificationCodeForChangePassword , changePassword , signOut , } ; return returnValue ; } ;