import cookie from "cookie" ; const unauthenticatedCookies = { lastUser : null , idToken : null , accessToken : null , } ; function userIdToTokenKey ( key ) { return encodeURIComponent ( key ) . replace ( /%(2[346B]|5E|60|7C)/g , decodeURIComponent ) . replace ( /[()]/g , escape ) ; } export function getCognitoCookieInfo ( cookieString , userPoolWebClientId ? ) { if ( ! userPoolWebClientId ) throw new Error ( "Missing configuration value for userPoolWebClientId in Amplify's Auth" ) ; if ( ! cookieString ) return unauthenticatedCookies ; const cookieData = cookie . parse ( cookieString ) ; const prefix = `CognitoIdentityServiceProvider.${ userPoolWebClientId }` ; const lastUserKey = `${ prefix }.LastAuthUser` ; const lastUser = cookieData [ lastUserKey ] ? cookieData [ lastUserKey ] : null ; const idTokenKey = lastUser ? `${ prefix }.${ userIdToTokenKey ( lastUser ) }.idToken` : null ; const idToken = idTokenKey && cookieData [ idTokenKey ] ? cookieData [ idTokenKey ] : null ; const accessTokenKey = lastUser ? `${ prefix }.${ userIdToTokenKey ( lastUser ) }.accessToken` : null ; const accessToken = accessTokenKey && cookieData [ accessTokenKey ] ? cookieData [ accessTokenKey ] : null ; return { lastUser , idToken , accessToken } ; }