feat: 修改登录
This commit is contained in:
parent
36fb790a29
commit
cca8a62dac
|
|
@ -16,3 +16,7 @@ REACT_APP_GRAPH_API_LOTTERY = "https://api.thegraph.com/subgraphs/name/pancakesw
|
||||||
|
|
||||||
REACT_APP_SNAPSHOT_BASE_URL = "https://testnet.snapshot.org"
|
REACT_APP_SNAPSHOT_BASE_URL = "https://testnet.snapshot.org"
|
||||||
REACT_APP_SNAPSHOT_VOTING_API = "https://xtjyd0liqe.execute-api.ap-northeast-1.amazonaws.com/dev/api"
|
REACT_APP_SNAPSHOT_VOTING_API = "https://xtjyd0liqe.execute-api.ap-northeast-1.amazonaws.com/dev/api"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
REACT_APP_REQUEST_URL = 'http://101.35.117.69:9090'
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@
|
||||||
"no-param-reassign": ["error", { "props": true, "ignorePropertyModificationsFor": ["state", "memo"] }],
|
"no-param-reassign": ["error", { "props": true, "ignorePropertyModificationsFor": ["state", "memo"] }],
|
||||||
"react/require-default-props": 0,
|
"react/require-default-props": 0,
|
||||||
"no-nested-ternary": 0,
|
"no-nested-ternary": 0,
|
||||||
"max-classes-per-file": 0
|
"max-classes-per-file": 0,
|
||||||
|
"react-hooks/exhaustive-deps":0
|
||||||
// End temporary rules
|
// End temporary rules
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,49 @@
|
||||||
import React from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { Menu as UikitMenu } from '@pancakeswap/uikit'
|
import { Menu as UikitMenu, ConnectorNames } from '@pancakeswap/uikit'
|
||||||
import { useWeb3React } from '@web3-react/core'
|
import { useWeb3React } from '@web3-react/core'
|
||||||
import { languageList } from 'config/localization/languages'
|
import { languageList } from 'config/localization/languages'
|
||||||
import { useTranslation } from 'contexts/Localization'
|
import { useTranslation } from 'contexts/Localization'
|
||||||
import useTheme from 'hooks/useTheme'
|
import useTheme from 'hooks/useTheme'
|
||||||
import useAuth from 'hooks/useAuth'
|
import useAuth from 'hooks/useAuth'
|
||||||
|
import { useDispatch } from 'react-redux'
|
||||||
import { usePriceCakeBusd, useProfile } from 'state/hooks'
|
import { usePriceCakeBusd, useProfile } from 'state/hooks'
|
||||||
|
import { useUnactiveAccount, useSignLogin, useAccount } from 'state/userInfo/hooks'
|
||||||
|
import useWeb3Provider from 'hooks/useActiveWeb3React'
|
||||||
|
import { clearUserInfo } from 'state/actions'
|
||||||
import config from './config'
|
import config from './config'
|
||||||
|
|
||||||
const Menu = (props) => {
|
const Menu = (props) => {
|
||||||
const { account } = useWeb3React()
|
const account = useAccount()
|
||||||
const { login, logout } = useAuth()
|
const { login, logout } = useAuth()
|
||||||
|
const [hasWalletLogin, setHasWalletLogin] = useState(false)
|
||||||
const { isDark, toggleTheme } = useTheme()
|
const { isDark, toggleTheme } = useTheme()
|
||||||
const cakePriceUsd = usePriceCakeBusd()
|
const cakePriceUsd = usePriceCakeBusd()
|
||||||
|
const { library } = useWeb3Provider()
|
||||||
|
const unActiveAccount = useUnactiveAccount()
|
||||||
|
const dispatch = useDispatch()
|
||||||
const { profile } = useProfile()
|
const { profile } = useProfile()
|
||||||
const { currentLanguage, setLanguage, t } = useTranslation()
|
const { currentLanguage, setLanguage, t } = useTranslation()
|
||||||
|
const sign = useSignLogin()
|
||||||
|
const handleLogin = async (connectorID: ConnectorNames) => {
|
||||||
|
await login(connectorID)
|
||||||
|
setHasWalletLogin(true)
|
||||||
|
}
|
||||||
|
const handleLogout = () => {
|
||||||
|
dispatch(clearUserInfo())
|
||||||
|
logout()
|
||||||
|
}
|
||||||
|
// 钱包登录后
|
||||||
|
useEffect(() => {
|
||||||
|
if (unActiveAccount && library.provider && hasWalletLogin) {
|
||||||
|
setHasWalletLogin(false)
|
||||||
|
sign()
|
||||||
|
}
|
||||||
|
}, [unActiveAccount, hasWalletLogin, library])
|
||||||
return (
|
return (
|
||||||
<UikitMenu
|
<UikitMenu
|
||||||
account={account}
|
account={account}
|
||||||
login={login}
|
login={handleLogin}
|
||||||
logout={logout}
|
logout={handleLogout}
|
||||||
isDark={isDark}
|
isDark={isDark}
|
||||||
toggleTheme={toggleTheme}
|
toggleTheme={toggleTheme}
|
||||||
currentLang={currentLanguage.code}
|
currentLang={currentLanguage.code}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,31 @@
|
||||||
import React from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
import { Button, useWalletModal } from '@pancakeswap/uikit'
|
import { Button, useWalletModal, ConnectorNames } from '@pancakeswap/uikit'
|
||||||
import useAuth from 'hooks/useAuth'
|
import useAuth from 'hooks/useAuth'
|
||||||
import { useTranslation } from 'contexts/Localization'
|
import { useTranslation } from 'contexts/Localization'
|
||||||
|
import useToast from 'hooks/useToast'
|
||||||
|
import { useUnactiveAccount, useSignLogin } from 'state/userInfo/hooks'
|
||||||
|
import useWeb3Provider from 'hooks/useActiveWeb3React'
|
||||||
|
|
||||||
const UnlockButton = (props) => {
|
const UnlockButton = (props) => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const { login, logout } = useAuth()
|
const { login, logout } = useAuth()
|
||||||
const { onPresentConnectModal } = useWalletModal(login, logout)
|
const [hasWalletLogin, setHasWalletLogin] = useState(false)
|
||||||
|
const { library } = useWeb3Provider()
|
||||||
|
const unActiveAccount = useUnactiveAccount()
|
||||||
|
const { toastInfo } = useToast()
|
||||||
|
const sign = useSignLogin()
|
||||||
|
const handleLogin = async (connectorID: ConnectorNames) => {
|
||||||
|
await login(connectorID)
|
||||||
|
setHasWalletLogin(true)
|
||||||
|
}
|
||||||
|
const { onPresentConnectModal } = useWalletModal(handleLogin, logout)
|
||||||
|
// 钱包登录后
|
||||||
|
useEffect(() => {
|
||||||
|
if (unActiveAccount && library.provider && hasWalletLogin) {
|
||||||
|
setHasWalletLogin(false)
|
||||||
|
sign()
|
||||||
|
}
|
||||||
|
}, [unActiveAccount, hasWalletLogin, library])
|
||||||
return (
|
return (
|
||||||
<Button onClick={onPresentConnectModal} {...props}>
|
<Button onClick={onPresentConnectModal} {...props}>
|
||||||
{t('Unlock Wallet')}
|
{t('Unlock Wallet')}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
import request from 'utils/request'
|
import request from 'utils/request'
|
||||||
|
|
||||||
export const login = (params) => {
|
export const login = (data) => {
|
||||||
return request.request({
|
return request.request({
|
||||||
url: '/v1/login',
|
url: '/high_city/app/api/auth/login',
|
||||||
method: 'get',
|
method: 'post',
|
||||||
params,
|
data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const queryLoginHash = (params) => {
|
export const queryLoginHash = (data) => {
|
||||||
return request.request({
|
return request.request({
|
||||||
url: '/v1/login/hash',
|
url: '/high_city/app/api/auth/msg',
|
||||||
method: 'get',
|
method: 'post',
|
||||||
params,
|
data,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,36 +2,8 @@ import request from 'utils/request'
|
||||||
|
|
||||||
export const queryUserInfo = () => {
|
export const queryUserInfo = () => {
|
||||||
return request.request({
|
return request.request({
|
||||||
url: '/v1/user',
|
url: '/high_city/app/api/user',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
export default queryUserInfo
|
||||||
export const queryUserAvatarList = () => {
|
|
||||||
return request.request({
|
|
||||||
url: '/v1/avatar/user/list',
|
|
||||||
method: 'get',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const queryUserInviteList = () => {
|
|
||||||
return request.request({
|
|
||||||
url: '/v1/user/invite/top/list',
|
|
||||||
method: 'get',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export const updateUserAvater = (params) => {
|
|
||||||
return request.request({
|
|
||||||
url: '/v1/user/update/avatar',
|
|
||||||
method: 'get',
|
|
||||||
params,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
export const updateUserInfo = (data) => {
|
|
||||||
return request.request({
|
|
||||||
url: '/v1/user/update',
|
|
||||||
method: 'post',
|
|
||||||
data,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import { setUserInfo } from 'state/actions'
|
||||||
import { CACHE_INVITE_CODE } from 'config/constants/cacheKey'
|
import { CACHE_INVITE_CODE } from 'config/constants/cacheKey'
|
||||||
import useToast from 'hooks/useToast'
|
import useToast from 'hooks/useToast'
|
||||||
import { State } from '../types'
|
import { State } from '../types'
|
||||||
|
import { fetchUserInfo } from './index'
|
||||||
|
|
||||||
export const useAccount = (ellipsis?: boolean) => {
|
export const useAccount = (ellipsis?: boolean) => {
|
||||||
const account = useSelector((state: State) => state.userInfo.account)
|
const account = useSelector((state: State) => state.userInfo.account)
|
||||||
|
|
@ -42,14 +43,15 @@ export const useSignLogin = () => {
|
||||||
const signLogin = async () => {
|
const signLogin = async () => {
|
||||||
try {
|
try {
|
||||||
const data = await queryLoginHash({ address: unActiveAccount })
|
const data = await queryLoginHash({ address: unActiveAccount })
|
||||||
const signHash = await signMessage(library, unActiveAccount, data.hash)
|
const signHash = await signMessage(library, unActiveAccount, data.msg)
|
||||||
const inviteCode = sessionStorage.getItem(CACHE_INVITE_CODE)
|
const inviteCode = sessionStorage.getItem(CACHE_INVITE_CODE)
|
||||||
const userInfo = await userLogin({
|
const userInfo = await userLogin({
|
||||||
hash: data.hash,
|
msg: data.msg,
|
||||||
signature: signHash,
|
sign: signHash,
|
||||||
invite_code: inviteCode,
|
inviteCode,
|
||||||
})
|
})
|
||||||
dispatch(setUserInfo({ ...userInfo.user_info, token: userInfo.token }))
|
await fetchUserInfo()
|
||||||
|
// dispatch(setUserInfo({ ...userInfo.user_info, token: userInfo.token }))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e)
|
console.log(e)
|
||||||
// e.message&&toastError(e)
|
// e.message&&toastError(e)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { Dispatch } from 'react'
|
import { Dispatch } from 'react'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
// import { clearUserInfo } from 'state/actions'
|
// import { clearUserInfo } from 'state/actions'
|
||||||
|
import { CACHE_TOKEN } from 'config/constants/cacheKey'
|
||||||
|
|
||||||
// create an axios instance
|
// create an axios instance
|
||||||
const request = axios.create({
|
const request = axios.create({
|
||||||
|
|
@ -11,12 +12,9 @@ let hasInit = false
|
||||||
export const initAxios = (dispatch: Dispatch<any>, toast) => {
|
export const initAxios = (dispatch: Dispatch<any>, toast) => {
|
||||||
if (hasInit) return
|
if (hasInit) return
|
||||||
hasInit = true
|
hasInit = true
|
||||||
// request interceptor
|
|
||||||
request.interceptors.request.use(
|
request.interceptors.request.use(
|
||||||
(memo: any) => {
|
(memo: any) => {
|
||||||
// do something before request is sent
|
memo.headers.token = localStorage.getItem(CACHE_TOKEN)
|
||||||
|
|
||||||
memo.headers.token = localStorage.getItem('token')
|
|
||||||
return memo
|
return memo
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
|
@ -33,8 +31,8 @@ export const initAxios = (dispatch: Dispatch<any>, toast) => {
|
||||||
toast.toastError('Login expiration')
|
toast.toastError('Login expiration')
|
||||||
return Promise.reject(new Error('Login expiration'))
|
return Promise.reject(new Error('Login expiration'))
|
||||||
}
|
}
|
||||||
toast.toastError(res.msg)
|
toast.toastError(res.message)
|
||||||
return Promise.reject(res.msg || 'Error')
|
return Promise.reject(res.message || 'Error')
|
||||||
}
|
}
|
||||||
return res.data
|
return res.data
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue