|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import { Amplify } from 'aws-amplify'; |
| 4 | +import { fetchAuthSession } from 'aws-amplify/auth'; |
| 5 | +import { |
| 6 | + Button, |
| 7 | + Text, |
| 8 | + Loader, |
| 9 | + useAuthenticator, |
| 10 | + withAuthenticator, |
| 11 | +} from '@aws-amplify/ui-react'; |
| 12 | +import { StorageImage } from '@aws-amplify/ui-react-storage'; |
| 13 | +import '@aws-amplify/ui-react/styles.css'; |
| 14 | +import awsExports from './aws-exports'; |
| 15 | + |
| 16 | +Amplify.configure(awsExports); |
| 17 | + |
| 18 | +export function StorageImageExample() { |
| 19 | + const [isFirstImgLoaded, setIsFirstImgLoaded] = React.useState(false); |
| 20 | + const [isSecondImgLoaded, setIsSecondImgLoaded] = React.useState(false); |
| 21 | + const [identityId, setIdentityId] = React.useState<string>(); |
| 22 | + const { signOut } = useAuthenticator((context) => [context.signOut]); |
| 23 | + |
| 24 | + React.useEffect(() => { |
| 25 | + const fetchIdentityId = async () => { |
| 26 | + const { identityId } = await fetchAuthSession(); |
| 27 | + console.log(identityId); |
| 28 | + setIdentityId(identityId); |
| 29 | + }; |
| 30 | + |
| 31 | + fetchIdentityId(); |
| 32 | + }, []); |
| 33 | + |
| 34 | + return ( |
| 35 | + <> |
| 36 | + <StorageImage |
| 37 | + alt="protected cat 1" |
| 38 | + path={({ identityId }) => `protected/${identityId}/protected-e2e.jpeg`} |
| 39 | + onLoad={() => setIsFirstImgLoaded(true)} |
| 40 | + /> |
| 41 | + {isFirstImgLoaded ? ( |
| 42 | + <Text>The first protected image is loaded.</Text> |
| 43 | + ) : ( |
| 44 | + <Loader testId="Loader1" /> |
| 45 | + )} |
| 46 | + {identityId && ( |
| 47 | + <StorageImage |
| 48 | + alt="protected cat 2" |
| 49 | + path={`protected/${identityId}/protected-e2e.jpeg`} |
| 50 | + onLoad={() => setIsSecondImgLoaded(true)} |
| 51 | + /> |
| 52 | + )} |
| 53 | + {isSecondImgLoaded ? ( |
| 54 | + <Text>The second protected image is loaded.</Text> |
| 55 | + ) : ( |
| 56 | + <Loader testId="Loader2" /> |
| 57 | + )} |
| 58 | + <Button onClick={signOut}>Sign out</Button> |
| 59 | + </> |
| 60 | + ); |
| 61 | +} |
| 62 | +export default withAuthenticator(StorageImageExample); |
0 commit comments