Giải thích code

Giải thích về codde:

  1. Module trong package.json với cross-domain-storage để trao đổi token giữa hai trang web:
"dependencies": {
    ...
    "amazon-cognito-identity-js": "^5.2.9",
    "cross-domain-storage": "^2.0.7",
    ...
}

Cognito

  1. Hàm signOut để vô hiệu hóa tất cả các token do Cognito cung cấp trong trường hợp người dùng đăng xuất:
export function signOut(callbacks: { onSuccess: (msg: string) => void; onFailure: (err: Error) => void }) {
  if (currentUser) {
    //currentUser.signOut()
    currentUser.globalSignOut(callbacks)
  }
}

Cognito

  1. Xử lý token session từ web1 sang web2 trong trường hợp đăng nhập tại web1 và token sẽ được thiết lập cho web2 với nguồn được cho phép trên web2 trong signIn.ts của 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. Cho phép nguồn trên web2 để chấp nhận truy cập từ web1 trong App.tsx:
const App: React.FunctionComponent = () => {
  useEffect(() => {
    createHost([
      {
        origin: "http://localhost:3000",
        allowedMethods: ["set", "remove"],
      },
    ]);
  }, []);
}

Cognito