Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
8e52e6a5
Unverified
Commit
8e52e6a5
authored
Apr 15, 2025
by
tom goriunov
Committed by
GitHub
Apr 15, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Auth: Switch the chain before signing the message when the user logs in with their wallet (#2673)
Fixes #2345
parent
0dd6087f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
14 additions
and
8 deletions
+14
-8
rewards.tsx
lib/contexts/rewards.tsx
+4
-2
useCallMethodWalletClient.ts
ui/address/contract/methods/useCallMethodWalletClient.ts
+1
-1
AddressVerificationStepSignature.tsx
...ssVerification/steps/AddressVerificationStepSignature.tsx
+5
-3
useSignInWithWallet.ts
ui/snippets/auth/useSignInWithWallet.ts
+4
-2
No files found.
lib/contexts/rewards.tsx
View file @
8e52e6a5
...
...
@@ -3,7 +3,7 @@ import { useQueryClient } from '@tanstack/react-query';
import
{
useToggle
}
from
'
@uidotdev/usehooks
'
;
import
{
useRouter
}
from
'
next/router
'
;
import
React
,
{
createContext
,
useContext
,
useEffect
,
useMemo
,
useCallback
}
from
'
react
'
;
import
{
useSignMessage
}
from
'
wagmi
'
;
import
{
useSignMessage
,
useSwitchChain
}
from
'
wagmi
'
;
import
type
{
RewardsUserBalancesResponse
,
RewardsUserDailyCheckResponse
,
...
...
@@ -118,6 +118,7 @@ export function RewardsContextProvider({ children }: Props) {
const
apiFetch
=
useApiFetch
();
const
{
address
}
=
useAccount
();
const
{
signMessageAsync
}
=
useSignMessage
();
const
{
switchChainAsync
}
=
useSwitchChain
();
const
profileQuery
=
useProfileQuery
();
const
[
isLoginModalOpen
,
setIsLoginModalOpen
]
=
useToggle
(
false
);
...
...
@@ -220,6 +221,7 @@ export function RewardsContextProvider({ children }: Props) {
reward
:
null
,
};
}
await
switchChainAsync
({
chainId
:
Number
(
config
.
chain
.
id
)
});
const
message
=
getMessageToSign
(
address
,
nonceResponse
.
nonce
,
checkUserQuery
.
data
?.
exists
,
refCode
);
const
signature
=
await
signMessageAsync
({
message
});
const
loginResponse
=
await
apiFetch
(
'
rewards_login
'
,
{
...
...
@@ -241,7 +243,7 @@ export function RewardsContextProvider({ children }: Props) {
errorToast
(
_error
);
throw
_error
;
}
},
[
a
piFetch
,
address
,
signMessageAsync
,
errorToast
,
saveApiToken
,
checkUserQuery
]);
},
[
a
ddress
,
apiFetch
,
checkUserQuery
.
data
?.
exists
,
switchChainAsync
,
signMessageAsync
,
saveApiToken
,
errorToast
]);
// Claim daily reward
const
claim
=
useCallback
(
async
()
=>
{
...
...
ui/address/contract/methods/useCallMethodWalletClient.ts
View file @
8e52e6a5
...
...
@@ -29,7 +29,7 @@ export default function useCallMethodWalletClient(): (params: Params) => Promise
}
if
(
chainId
&&
String
(
chainId
)
!==
config
.
chain
.
id
)
{
await
switchChainAsync
?.
({
chainId
:
Number
(
config
.
chain
.
id
)
});
await
switchChainAsync
({
chainId
:
Number
(
config
.
chain
.
id
)
});
}
const
address
=
getAddress
(
addressHash
);
...
...
ui/addressVerification/steps/AddressVerificationStepSignature.tsx
View file @
8e52e6a5
...
...
@@ -3,7 +3,7 @@ import { useAppKit } from '@reown/appkit/react';
import
React
from
'
react
'
;
import
type
{
SubmitHandler
}
from
'
react-hook-form
'
;
import
{
FormProvider
,
useForm
}
from
'
react-hook-form
'
;
import
{
useSignMessage
,
useAccount
}
from
'
wagmi
'
;
import
{
useSignMessage
,
useAccount
,
useSwitchChain
}
from
'
wagmi
'
;
import
type
{
AddressVerificationFormSecondStepFields
,
...
...
@@ -84,6 +84,7 @@ const AddressVerificationStepSignature = ({ address, signingMessage, contractCre
const
onSubmit
=
handleSubmit
(
onFormSubmit
);
const
{
signMessage
,
isPending
:
isSigning
}
=
useSignMessage
();
const
{
switchChainAsync
}
=
useSwitchChain
();
const
handleSignMethodChange
=
React
.
useCallback
(({
value
}:
{
value
:
string
|
null
})
=>
{
if
(
!
value
)
{
...
...
@@ -99,13 +100,14 @@ const AddressVerificationStepSignature = ({ address, signingMessage, contractCre
openWeb3Modal
();
},
[
clearErrors
,
openWeb3Modal
]);
const
handleWeb3SignClick
=
React
.
useCallback
(()
=>
{
const
handleWeb3SignClick
=
React
.
useCallback
(
async
()
=>
{
clearErrors
(
'
root
'
);
if
(
!
isConnected
)
{
return
setError
(
'
root
'
,
{
type
:
'
manual
'
,
message
:
'
Please connect to your Web3 wallet first
'
});
}
await
switchChainAsync
({
chainId
:
Number
(
config
.
chain
.
id
)
});
const
message
=
getValues
(
'
message
'
);
signMessage
({
message
},
{
onSuccess
:
(
data
)
=>
{
...
...
@@ -116,7 +118,7 @@ const AddressVerificationStepSignature = ({ address, signingMessage, contractCre
return
setError
(
'
root
'
,
{
type
:
'
SIGNING_FAIL
'
,
message
:
(
error
as
Error
)?.
message
||
'
Oops! Something went wrong
'
});
},
});
},
[
clearErrors
,
isConnected
,
getValues
,
signMessage
,
setError
,
setValue
,
onSubmit
]);
},
[
clearErrors
,
isConnected
,
getValues
,
signMessage
,
setError
,
setValue
,
onSubmit
,
switchChainAsync
]);
const
handleManualSignClick
=
React
.
useCallback
(()
=>
{
clearErrors
(
'
root
'
);
...
...
ui/snippets/auth/useSignInWithWallet.ts
View file @
8e52e6a5
import
React
from
'
react
'
;
import
{
useSignMessage
}
from
'
wagmi
'
;
import
{
useSignMessage
,
useSwitchChain
}
from
'
wagmi
'
;
import
type
{
UserInfo
}
from
'
types/api/account
'
;
import
type
{
RewardsCheckUserResponse
,
RewardsConfigResponse
,
RewardsLoginResponse
,
RewardsNonceResponse
}
from
'
types/api/rewards
'
;
...
...
@@ -53,6 +53,7 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth, log
const
apiFetch
=
useApiFetch
();
const
web3Wallet
=
useWeb3Wallet
({
source
});
const
{
signMessageAsync
}
=
useSignMessage
();
const
{
switchChainAsync
}
=
useSwitchChain
();
const
getSiweMessage
=
React
.
useCallback
(
async
(
address
:
string
)
=>
{
try
{
...
...
@@ -99,6 +100,7 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth, log
const
proceedToAuth
=
React
.
useCallback
(
async
(
address
:
string
)
=>
{
try
{
await
switchChainAsync
({
chainId
:
Number
(
config
.
chain
.
id
)
});
const
siweMessage
=
await
getSiweMessage
(
address
);
const
signature
=
await
signMessageAsync
({
message
:
siweMessage
.
message
});
const
recaptchaToken
=
await
executeRecaptchaAsync
();
...
...
@@ -143,7 +145,7 @@ function useSignInWithWallet({ onSuccess, onError, source = 'Login', isAuth, log
}
finally
{
setIsPending
(
false
);
}
},
[
getSiweMessage
,
signMessageAsync
,
executeRecaptchaAsync
,
isAuth
,
apiFetch
,
onSuccess
,
onError
]);
},
[
getSiweMessage
,
s
witchChainAsync
,
s
ignMessageAsync
,
executeRecaptchaAsync
,
isAuth
,
apiFetch
,
onSuccess
,
onError
]);
const
start
=
React
.
useCallback
(()
=>
{
setIsPending
(
true
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment