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
67acc9a4
Commit
67acc9a4
authored
Sep 27, 2024
by
Max Alekseenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement login
parent
fb5726b7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
143 additions
and
5 deletions
+143
-5
resources.ts
lib/api/resources.ts
+22
-0
cookies.ts
lib/cookies.ts
+1
-0
rewards.ts
types/api/rewards.ts
+12
-0
RewardsLoginModal.tsx
ui/rewards/RewardsLoginModal.tsx
+1
-1
CongratsStepContent.tsx
ui/rewards/steps/CongratsStepContent.tsx
+3
-1
LoginStepContent.tsx
ui/rewards/steps/LoginStepContent.tsx
+29
-3
useLogin.tsx
ui/rewards/useLogin.tsx
+75
-0
No files found.
lib/api/resources.ts
View file @
67acc9a4
...
@@ -90,6 +90,7 @@ import type {
...
@@ -90,6 +90,7 @@ import type {
OptimismL2BatchBlocks
,
OptimismL2BatchBlocks
,
}
from
'
types/api/optimisticL2
'
;
}
from
'
types/api/optimisticL2
'
;
import
type
{
RawTracesResponse
}
from
'
types/api/rawTrace
'
;
import
type
{
RawTracesResponse
}
from
'
types/api/rawTrace
'
;
import
type
{
RewardsNonceResponse
,
RewardsCheckUserResponse
,
RewardsLoginResponse
}
from
'
types/api/rewards
'
;
import
type
{
SearchRedirectResult
,
SearchResult
,
SearchResultFilters
,
SearchResultItem
}
from
'
types/api/search
'
;
import
type
{
SearchRedirectResult
,
SearchResult
,
SearchResultFilters
,
SearchResultItem
}
from
'
types/api/search
'
;
import
type
{
ShibariumWithdrawalsResponse
,
ShibariumDepositsResponse
}
from
'
types/api/shibarium
'
;
import
type
{
ShibariumWithdrawalsResponse
,
ShibariumDepositsResponse
}
from
'
types/api/shibarium
'
;
import
type
{
HomeStats
}
from
'
types/api/stats
'
;
import
type
{
HomeStats
}
from
'
types/api/stats
'
;
...
@@ -319,6 +320,24 @@ export const RESOURCES = {
...
@@ -319,6 +320,24 @@ export const RESOURCES = {
basePath
:
marketplaceApi
?.
basePath
,
basePath
:
marketplaceApi
?.
basePath
,
},
},
// REWARDS SERVICE
rewards_nonce
:
{
path
:
'
/api/v1/auth/nonce
'
,
endpoint
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
endpoint
,
basePath
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
basePath
,
},
rewards_check_user
:
{
path
:
'
/api/v1/auth/user/:address
'
,
pathParams
:
[
'
address
'
as
const
],
endpoint
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
endpoint
,
basePath
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
basePath
,
},
rewards_login
:
{
path
:
'
/api/v1/auth/login
'
,
endpoint
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
endpoint
,
basePath
:
getFeaturePayload
(
config
.
features
.
rewards
)?.
api
.
basePath
,
},
// BLOCKS, TXS
// BLOCKS, TXS
blocks
:
{
blocks
:
{
path
:
'
/api/v2/blocks
'
,
path
:
'
/api/v2/blocks
'
,
...
@@ -1169,6 +1188,9 @@ Q extends 'address_mud_records' ? AddressMudRecords :
...
@@ -1169,6 +1188,9 @@ Q extends 'address_mud_records' ? AddressMudRecords :
Q
extends
'
address_mud_record
'
?
AddressMudRecord
:
Q
extends
'
address_mud_record
'
?
AddressMudRecord
:
Q
extends
'
withdrawals
'
?
WithdrawalsResponse
:
Q
extends
'
withdrawals
'
?
WithdrawalsResponse
:
Q
extends
'
withdrawals_counters
'
?
WithdrawalsCounters
:
Q
extends
'
withdrawals_counters
'
?
WithdrawalsCounters
:
Q
extends
'
rewards_nonce
'
?
RewardsNonceResponse
:
Q
extends
'
rewards_check_user
'
?
RewardsCheckUserResponse
:
Q
extends
'
rewards_login
'
?
RewardsLoginResponse
:
never
;
never
;
/* eslint-enable @typescript-eslint/indent */
/* eslint-enable @typescript-eslint/indent */
...
...
lib/cookies.ts
View file @
67acc9a4
...
@@ -5,6 +5,7 @@ import isBrowser from './isBrowser';
...
@@ -5,6 +5,7 @@ import isBrowser from './isBrowser';
export
enum
NAMES
{
export
enum
NAMES
{
NAV_BAR_COLLAPSED
=
'
nav_bar_collapsed
'
,
NAV_BAR_COLLAPSED
=
'
nav_bar_collapsed
'
,
API_TOKEN
=
'
_explorer_key
'
,
API_TOKEN
=
'
_explorer_key
'
,
REWARDS_API_TOKEN
=
'
rewards_api_token
'
,
INVALID_SESSION
=
'
invalid_session
'
,
INVALID_SESSION
=
'
invalid_session
'
,
CONFIRM_EMAIL_PAGE_VIEWED
=
'
confirm_email_page_viewed
'
,
CONFIRM_EMAIL_PAGE_VIEWED
=
'
confirm_email_page_viewed
'
,
TXS_SORT
=
'
txs_sort
'
,
TXS_SORT
=
'
txs_sort
'
,
...
...
types/api/rewards.ts
0 → 100644
View file @
67acc9a4
export
type
RewardsNonceResponse
=
{
nonce
:
string
;
};
export
type
RewardsCheckUserResponse
=
{
exists
:
boolean
;
};
export
type
RewardsLoginResponse
=
{
created
:
boolean
;
token
:
string
;
};
ui/rewards/RewardsLoginModal.tsx
View file @
67acc9a4
...
@@ -36,7 +36,7 @@ const RewardsLoginModal = () => {
...
@@ -36,7 +36,7 @@ const RewardsLoginModal = () => {
<
ModalCloseButton
top=
{
6
}
right=
{
6
}
/>
<
ModalCloseButton
top=
{
6
}
right=
{
6
}
/>
<
ModalBody
mb=
{
0
}
>
<
ModalBody
mb=
{
0
}
>
{
isLoginStep
?
{
isLoginStep
?
<
LoginStepContent
goNext=
{
setIsLoginStep
.
off
}
/>
:
<
LoginStepContent
goNext=
{
setIsLoginStep
.
off
}
closeModal=
{
closeLoginModal
}
/>
:
<
CongratsStepContent
/>
<
CongratsStepContent
/>
}
}
</
ModalBody
>
</
ModalBody
>
...
...
ui/rewards/steps/CongratsStepContent.tsx
View file @
67acc9a4
import
{
Text
,
Box
,
Flex
,
useColorModeValue
,
Button
}
from
'
@chakra-ui/react
'
;
import
{
Text
,
Box
,
Flex
,
useColorModeValue
,
Button
}
from
'
@chakra-ui/react
'
;
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
{
route
}
from
'
nextjs-routes
'
;
import
IconSvg
from
'
ui/shared/IconSvg
'
;
import
IconSvg
from
'
ui/shared/IconSvg
'
;
import
AvailableSoonLabel
from
'
../AvailableSoonLabel
'
;
import
AvailableSoonLabel
from
'
../AvailableSoonLabel
'
;
...
@@ -69,7 +71,7 @@ const CongratsStepContent = () => {
...
@@ -69,7 +71,7 @@ const CongratsStepContent = () => {
Explore your current merits balance, find activities to boost your merits,
Explore your current merits balance, find activities to boost your merits,
and view your capybara NFT badge collection on the dashboard
and view your capybara NFT badge collection on the dashboard
</
Text
>
</
Text
>
<
Button
mt=
{
3
}
>
<
Button
mt=
{
3
}
as=
"a"
href=
{
route
({
pathname
:
'
/account/rewards
'
})
}
>
Open
Open
</
Button
>
</
Button
>
</
Flex
>
</
Flex
>
...
...
ui/rewards/steps/LoginStepContent.tsx
View file @
67acc9a4
import
{
Text
,
Button
,
useColorModeValue
,
Image
,
Box
,
Flex
,
Switch
,
useBoolean
,
Input
,
FormControl
}
from
'
@chakra-ui/react
'
;
import
{
Text
,
Button
,
useColorModeValue
,
Image
,
Box
,
Flex
,
Switch
,
useBoolean
,
Input
,
FormControl
}
from
'
@chakra-ui/react
'
;
import
React
from
'
react
'
;
import
{
useRouter
}
from
'
next/router
'
;
import
React
,
{
useCallback
}
from
'
react
'
;
import
InputPlaceholder
from
'
ui/shared/InputPlaceholder
'
;
import
InputPlaceholder
from
'
ui/shared/InputPlaceholder
'
;
import
LinkExternal
from
'
ui/shared/links/LinkExternal
'
;
import
LinkExternal
from
'
ui/shared/links/LinkExternal
'
;
import
useWallet
from
'
ui/snippets/walletMenu/useWallet
'
;
import
useWallet
from
'
ui/snippets/walletMenu/useWallet
'
;
const
LoginStepContent
=
({
goNext
}:
{
goNext
:
()
=>
void
})
=>
{
import
useLogin
from
'
../useLogin
'
;
type
Props
=
{
goNext
:
()
=>
void
;
closeModal
:
()
=>
void
;
};
const
LoginStepContent
=
({
goNext
,
closeModal
}:
Props
)
=>
{
const
router
=
useRouter
();
const
{
connect
,
isWalletConnected
}
=
useWallet
({
source
:
'
Merits
'
});
const
{
connect
,
isWalletConnected
}
=
useWallet
({
source
:
'
Merits
'
});
const
[
isSwitchChecked
,
setIsSwitchChecked
]
=
useBoolean
(
false
);
const
[
isSwitchChecked
,
setIsSwitchChecked
]
=
useBoolean
(
false
);
const
[
isLoading
,
setIsLoading
]
=
useBoolean
(
false
);
const
dividerColor
=
useColorModeValue
(
'
blackAlpha.200
'
,
'
whiteAlpha.200
'
);
const
dividerColor
=
useColorModeValue
(
'
blackAlpha.200
'
,
'
whiteAlpha.200
'
);
const
login
=
useLogin
();
const
handleLogin
=
useCallback
(
async
()
=>
{
try
{
setIsLoading
.
on
();
const
{
isNewUser
}
=
await
login
();
if
(
isNewUser
)
{
goNext
();
}
else
{
closeModal
();
router
.
push
({
pathname
:
'
/account/rewards
'
},
undefined
,
{
shallow
:
true
});
}
}
catch
(
error
)
{}
setIsLoading
.
off
();
},
[
login
,
goNext
,
setIsLoading
,
router
,
closeModal
]);
return
(
return
(
<>
<>
...
@@ -46,7 +71,8 @@ const LoginStepContent = ({ goNext }: { goNext: () => void }) => {
...
@@ -46,7 +71,8 @@ const LoginStepContent = ({ goNext }: { goNext: () => void }) => {
w=
"full"
w=
"full"
mt=
{
isWalletConnected
?
6
:
0
}
mt=
{
isWalletConnected
?
6
:
0
}
mb=
{
4
}
mb=
{
4
}
onClick=
{
isWalletConnected
?
goNext
:
connect
}
onClick=
{
isWalletConnected
?
handleLogin
:
connect
}
isLoading=
{
isLoading
}
>
>
{
isWalletConnected
?
'
Get started
'
:
'
Connect wallet
'
}
{
isWalletConnected
?
'
Get started
'
:
'
Connect wallet
'
}
</
Button
>
</
Button
>
...
...
ui/rewards/useLogin.tsx
0 → 100644
View file @
67acc9a4
import
{
useCallback
}
from
'
react
'
;
import
{
useAccount
,
useSignMessage
}
from
'
wagmi
'
;
import
type
{
RewardsNonceResponse
,
RewardsCheckUserResponse
,
RewardsLoginResponse
}
from
'
types/api/rewards
'
;
import
config
from
'
configs/app
'
;
import
type
{
ResourceError
}
from
'
lib/api/resources
'
;
import
useApiFetch
from
'
lib/api/useApiFetch
'
;
import
*
as
cookies
from
'
lib/cookies
'
;
import
useToast
from
'
lib/hooks/useToast
'
;
function
getMessageToSign
(
address
:
string
,
nonce
:
string
,
isLogin
?:
boolean
,
refCode
?:
string
)
{
const
signInText
=
'
Sign-In for the Blockscout points program.
'
;
const
signUpText
=
'
Sign-Up for the Blockscout points program. I accept Terms of Service: https://points.blockscout.com/tos. I love capybaras.
'
;
const
referralText
=
refCode
?
` Referral code:
${
refCode
}
`
:
''
;
const
body
=
isLogin
?
signInText
:
signUpText
+
referralText
;
return
[
`
${
/*window.location.hostname*/
'
blockscout.com
'
}
wants you to sign in with your Ethereum account:`
,
address
,
''
,
body
,
''
,
`URI:
${
/*window.location.origin*/
'
https://blockscout.com
'
}
`
,
'
Version: 1
'
,
`Chain ID:
${
config
.
chain
.
id
}
`
,
`Nonce:
${
nonce
}
`
,
`Issued At:
${
new
Date
().
toISOString
()
}
`
,
].
join
(
'
\n
'
);
}
export
default
function
useLogin
()
{
const
apiFetch
=
useApiFetch
();
const
toast
=
useToast
();
const
{
address
}
=
useAccount
();
const
{
signMessageAsync
}
=
useSignMessage
();
return
useCallback
(
async
()
=>
{
try
{
const
[
nonceResponse
,
userResponse
]
=
await
Promise
.
all
([
apiFetch
<
'
rewards_nonce
'
,
RewardsNonceResponse
>
(
'
rewards_nonce
'
),
apiFetch
<
'
rewards_check_user
'
,
RewardsCheckUserResponse
>
(
'
rewards_check_user
'
,
{
pathParams
:
{
address
}
}),
]);
if
(
!
address
||
!
(
'
nonce
'
in
nonceResponse
)
||
!
(
'
exists
'
in
userResponse
))
{
throw
new
Error
();
}
const
message
=
getMessageToSign
(
address
,
nonceResponse
.
nonce
,
userResponse
.
exists
);
const
signature
=
await
signMessageAsync
({
message
});
const
loginResponse
=
await
apiFetch
<
'
rewards_login
'
,
RewardsLoginResponse
>
(
'
rewards_login
'
,
{
fetchParams
:
{
method
:
'
POST
'
,
body
:
{
nonce
:
nonceResponse
.
nonce
,
message
,
signature
,
},
},
});
if
(
!
(
'
created
'
in
loginResponse
))
{
throw
loginResponse
;
}
cookies
.
set
(
cookies
.
NAMES
.
REWARDS_API_TOKEN
,
loginResponse
.
token
);
return
{
isNewUser
:
loginResponse
.
created
};
}
catch
(
_error
)
{
toast
({
position
:
'
top-right
'
,
title
:
'
Error
'
,
description
:
(
_error
as
ResourceError
<
{
message
:
string
}
>
)?.
payload
?.
message
||
'
Something went wrong. Try again later.
'
,
status
:
'
error
'
,
variant
:
'
subtle
'
,
isClosable
:
true
,
});
throw
_error
;
}
},
[
apiFetch
,
address
,
signMessageAsync
,
toast
]);
}
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