Explanation about the Code

Explanation about the Code:

  1. Module in package.json with cross-domain-storage for exchanging tokens between two websites:
"dependencies": {
    ...
    "amazon-cognito-identity-js": "^5.2.9",
    "cross-domain-storage": "^2.0.7",
    ...
}

Cognito

  1. The signOut function to invalidate all tokens provided by Cognito in case the user logs out:
export function signOut(callbacks: { onSuccess: (msg: string) => void; onFailure: (err: Error) => void }) {
  if (currentUser) {
    //currentUser.signOut()
    currentUser.globalSignOut(callbacks)
  }
}

Cognito

  1. Handling the session token from web1 to web2 in case of logging in at web1, and the token will be set for web2 with the source allowed on web2 in signIn.ts of web1:
const handleSendToken = () => {
  // send token
  if(!localStorage) return

  var tokenStorage = createGuest('http://localhost:3001/accessStorage');
  Object.keys(localStorage).forEach(key => {
    console.log('key', key);
    tokenStorage.set(key, localStorage[key])
  })
}

Cognito

  1. Allowing the source on web2 to accept access from web1 in App.tsx:
const App: React.FunctionComponent = () => {
  useEffect(() => {
    createHost([
      {
        origin: "http://localhost:3000",
        allowedMethods: ["set", "remove"],
      },
    ]);
  }, []);
}

Cognito