|
1 | 1 | 'use client'; |
2 | 2 | import React from 'react'; |
3 | 3 | import Image from 'next/image'; |
| 4 | +import { useSignMessage } from 'wagmi'; |
| 5 | +import { useAccount } from 'wagmi'; |
| 6 | +import Alert from '@mui/material/Alert'; |
| 7 | +import Box from '@mui/material/Box'; |
4 | 8 | import Button from '@mui/material/Button'; |
| 9 | +import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar'; |
5 | 10 |
|
| 11 | +import { siweMessage } from '@monerium/sdk'; |
6 | 12 | import { useAuth } from '@monerium/sdk-react-provider'; |
7 | 13 |
|
8 | 14 | import s from './MoneriumConnect.module.scss'; |
9 | 15 | export const MoneriumConnect = () => { |
10 | | - const { authorize } = useAuth(); |
| 16 | + const { address } = useAccount(); |
| 17 | + const { authorize, siwe, error } = useAuth(); |
| 18 | + const { signMessageAsync } = useSignMessage(); |
| 19 | + const [open, setOpen] = React.useState(!!(error as Error)?.message); |
| 20 | + |
| 21 | + const handleClose = ( |
| 22 | + event?: React.SyntheticEvent | Event, |
| 23 | + reason?: SnackbarCloseReason |
| 24 | + ) => { |
| 25 | + if (reason === 'clickaway') { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + setOpen(false); |
| 30 | + }; |
| 31 | + |
| 32 | + const message = siweMessage({ |
| 33 | + domain: 'localhost:3000', |
| 34 | + address: `${address}`, |
| 35 | + appName: 'SDK TEST APP', |
| 36 | + redirectUri: 'http://localhost:3000/dashboard', |
| 37 | + chainId: 1, |
| 38 | + privacyPolicyUrl: 'https://example.com/privacy-policy', |
| 39 | + termsOfServiceUrl: 'https://example.com/terms-of-service', |
| 40 | + }); |
| 41 | + |
| 42 | + const signInWithEthereum = () => { |
| 43 | + signMessageAsync({ |
| 44 | + message: message, |
| 45 | + }).then((signature) => { |
| 46 | + siwe({ |
| 47 | + message: message, |
| 48 | + signature: signature, |
| 49 | + }); |
| 50 | + }); |
| 51 | + }; |
11 | 52 |
|
12 | 53 | return ( |
13 | | - <div className={s.wrapper}> |
14 | | - <Button |
15 | | - href="https://monerium.com" |
16 | | - size="large" |
17 | | - variant="contained" |
18 | | - onClick={() => {}} |
19 | | - > |
20 | | - Read more |
21 | | - </Button> |
22 | | - <Button |
23 | | - size="large" |
24 | | - variant="outlined" |
25 | | - onClick={() => authorize()} |
26 | | - startIcon={ |
27 | | - <Image |
28 | | - src="/monerium-icon.png" |
29 | | - alt="Monerium icon" |
30 | | - width={16} |
31 | | - height={20} |
32 | | - priority |
33 | | - /> |
34 | | - } |
| 54 | + <> |
| 55 | + <Snackbar |
| 56 | + color="error" |
| 57 | + open={open} |
| 58 | + autoHideDuration={6000} |
| 59 | + onClose={handleClose} |
35 | 60 | > |
36 | | - Connect |
37 | | - </Button> |
38 | | - </div> |
| 61 | + <Alert |
| 62 | + onClose={handleClose} |
| 63 | + severity="error" |
| 64 | + variant="filled" |
| 65 | + sx={{ width: '100%' }} |
| 66 | + > |
| 67 | + {(error as Error)?.message} |
| 68 | + </Alert> |
| 69 | + </Snackbar> |
| 70 | + <Box className={s.wrapper}> |
| 71 | + <Box className={s.topwrapper}> |
| 72 | + <Button |
| 73 | + href="https://monerium.com" |
| 74 | + size="large" |
| 75 | + variant="contained" |
| 76 | + onClick={() => {}} |
| 77 | + > |
| 78 | + Read more |
| 79 | + </Button> |
| 80 | + <Button |
| 81 | + size="large" |
| 82 | + variant="outlined" |
| 83 | + onClick={() => authorize()} |
| 84 | + startIcon={ |
| 85 | + <Image |
| 86 | + src="/monerium-icon.png" |
| 87 | + alt="Monerium icon" |
| 88 | + width={16} |
| 89 | + height={20} |
| 90 | + priority |
| 91 | + /> |
| 92 | + } |
| 93 | + > |
| 94 | + Connect |
| 95 | + </Button> |
| 96 | + </Box> |
| 97 | + <Button |
| 98 | + size="large" |
| 99 | + variant="outlined" |
| 100 | + onClick={() => signInWithEthereum()} |
| 101 | + > |
| 102 | + Sign In with Ethereum |
| 103 | + </Button> |
| 104 | + </Box> |
| 105 | + </> |
39 | 106 | ); |
40 | 107 | }; |
0 commit comments