修改登录流程

This commit is contained in:
cebgcontract 2022-02-23 16:48:04 +08:00
parent 826aa3a6bc
commit 6bfd9eee28
7 changed files with 98 additions and 26 deletions

View File

@ -12,10 +12,10 @@
<li :class="{'active': currentSection === 'roadmap'}"><a href="/roadmap">Roadmap</a></li> <li :class="{'active': currentSection === 'roadmap'}"><a href="/roadmap">Roadmap</a></li>
</ul> </ul>
<div class="right-part"> <div class="right-part">
<div class="login-btn" v-if="!accountId" @click="collectToWallet"> <div class="login-btn" v-if="!logined" @click="collectToWallet">
<img src="@/assets/202202/loginbutton@2x.png" alt="login"/> <img src="@/assets/202202/loginbutton@2x.png" alt="login"/>
</div> </div>
<div class="login-btn" v-if="accountId" @click="disconnectWallet"> <div class="login-btn" v-if="logined" @click="disconnectWallet">
Logout Logout
</div> </div>
<div class="market-btn" @click="comingSoon"> <div class="market-btn" @click="comingSoon">
@ -30,7 +30,7 @@ import { Component, Vue } from 'vue-property-decorator'
import { Message } from 'element-ui' import { Message } from 'element-ui'
import { AppModule } from '@/store/modules/app' import { AppModule } from '@/store/modules/app'
import { BlockChain } from '@/utils/blockchain' import { BlockChain } from '@/utils/blockchain'
import { loginWithSign } from '@/utils/login' import { UserModule } from '@/store/modules/user'
declare module 'vue/types/vue' { declare module 'vue/types/vue' {
interface Vue { interface Vue {
@ -62,11 +62,15 @@ export default class extends Vue {
async collectToWallet() { async collectToWallet() {
await this.bc.connect() await this.bc.connect()
await loginWithSign(this.bc, AppModule.accountId, AppModule.chainId) await UserModule.Login({
bcInstance: this.bc,
account: AppModule.accountId,
chainId: AppModule.chainId
})
} }
toggleDrop() { get logined() {
this.dropShow = !this.dropShow return !!UserModule.token
} }
get accountId() { get accountId() {

View File

@ -24,7 +24,7 @@
<!-- <li data-menuanchor="chip" @click="changeSection" :class="{'active': currentSection === 'chip'}">--> <!-- <li data-menuanchor="chip" @click="changeSection" :class="{'active': currentSection === 'chip'}">-->
<!-- <a href="#chip_section">Chip</a>--> <!-- <a href="#chip_section">Chip</a>-->
<!-- </li>--> <!-- </li>-->
<li v-if="accountId"> <li v-if="logined">
<a href="mynft" >Mine</a> <a href="mynft" >Mine</a>
</li> </li>
<li data-menuanchor="market" @click.stop="comingSoon"> <li data-menuanchor="market" @click.stop="comingSoon">
@ -49,16 +49,16 @@
<!-- <li data-menuanchor="chip" @click="changeSection" :class="{'active': currentSection === 'chip'}">--> <!-- <li data-menuanchor="chip" @click="changeSection" :class="{'active': currentSection === 'chip'}">-->
<!-- <a href="#chip_section">CHIP</a>--> <!-- <a href="#chip_section">CHIP</a>-->
<!-- </li>--> <!-- </li>-->
<li v-if="accountId"> <li v-if="logined">
<a href="mynft" >Mine</a> <a href="mynft" >Mine</a>
</li> </li>
<li data-menuanchor="market" @click.stop="comingSoon"> <li data-menuanchor="market" @click.stop="comingSoon">
<a href="javascript:void(0)">MARKETPLACE</a> <a href="javascript:void(0)">MARKETPLACE</a>
</li> </li>
<li v-if="accountId" @click.stop="disconnectWallet"> <li v-if="logined" @click.stop="disconnectWallet">
<a href="javascript:void(0)" >Logout</a> <a href="javascript:void(0)" >Logout</a>
</li> </li>
<li v-if="!accountId" @click="collectToWallet"> <li v-if="!logined" @click="collectToWallet">
<a href="javascript:void(0)" >Connect Wallet</a> <a href="javascript:void(0)" >Connect Wallet</a>
</li> </li>
</ul> </ul>
@ -71,6 +71,7 @@ import { Component, Vue } from 'vue-property-decorator'
import { Message } from 'element-ui' import { Message } from 'element-ui'
import { AppModule } from '@/store/modules/app' import { AppModule } from '@/store/modules/app'
import { BlockChain } from '@/utils/blockchain' import { BlockChain } from '@/utils/blockchain'
import { UserModule } from '@/store/modules/user'
declare module 'vue/types/vue' { declare module 'vue/types/vue' {
interface Vue { interface Vue {
@ -101,7 +102,16 @@ export default class extends Vue {
} }
async collectToWallet() { async collectToWallet() {
return this.bc.connect() await this.bc.connect()
await UserModule.Login({
bcInstance: this.bc,
account: AppModule.accountId,
chainId: AppModule.chainId
})
}
get logined() {
return !!UserModule.token
} }
toggleDrop() { toggleDrop() {

View File

@ -29,6 +29,7 @@ import { Message } from 'element-ui'
import { BlockChain } from '@/utils/blockchain' import { BlockChain } from '@/utils/blockchain'
import { loginWithSign } from '@/utils/login' import { loginWithSign } from '@/utils/login'
import { AppModule } from '@/store/modules/app' import { AppModule } from '@/store/modules/app'
import { UserModule } from '@/store/modules/user'
@Component({ @Component({
name: 'MobileHeader', name: 'MobileHeader',
@ -66,7 +67,16 @@ export default class MobileHeader extends Vue {
async collectToWallet() { async collectToWallet() {
await this.bc.connect() await this.bc.connect()
await loginWithSign(this.bc, AppModule.accountId, AppModule.chainId) console.log(`AppModule.accountId: ${AppModule.accountId}, chainId: ${AppModule.chainId}`)
await UserModule.Login({
bcInstance: this.bc,
account: AppModule.accountId,
chainId: AppModule.chainId
})
}
get logined() {
return !!UserModule.token
} }
get accountId() { get accountId() {

View File

@ -6,9 +6,14 @@ import {
VuexModule VuexModule
} from 'vuex-module-decorators' } from 'vuex-module-decorators'
import { getToken, removeToken, setToken } from '@/utils/cookies' import { getToken, removeToken, setToken } from '@/utils/cookies'
import { getUserInfo, login, logout } from '@/api/User' import { getNonce, getUserInfo, login } from '@/api/User'
import store from '@/store' import store from '@/store'
const EIP721_DOMAIN_DATA = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' }
]
export interface IUserState { export interface IUserState {
token: string token: string
name: string name: string
@ -36,12 +41,42 @@ class User extends VuexModule implements IUserState {
public account = '' public account = ''
@Action @Action
public async Login(userInfo: { username: string, password: string }) { public async Login({ bcInstance, account, chainId }: {bcInstance: any, account: string, chainId: string | number}) {
let { username, password } = userInfo chainId += ''
username = username.trim() const preRequest: any = await getNonce({ account, net_id: chainId })
const { data } = await login({ username, password }) const tips = 'This signature is only used for verify your account'
setToken(data.token) const signMsg = {
this.SET_TOKEN(data.token) tips,
nonce: preRequest.nonce + ''
}
const signObj = {
types: {
EIP712Domain: EIP721_DOMAIN_DATA,
set: [
{ name: 'tips', type: 'string' },
{ name: 'nonce', type: 'string' }
]
},
primaryType: 'set',
domain: {
name: 'Auth',
version: '1'
},
message: signMsg
}
const signature = await bcInstance.signData(signObj, account)
const authData = {
account,
nonce: preRequest.nonce,
signature,
tips,
net_id: chainId
}
console.log('login data: ', authData)
const res: any = await login(authData)
setToken(res.token)
this.SET_TOKEN(res.token)
} }
@Action @Action
@ -82,7 +117,7 @@ class User extends VuexModule implements IUserState {
if (this.token === '') { if (this.token === '') {
throw Error('LogOut: token is undefined!') throw Error('LogOut: token is undefined!')
} }
await logout() // await logout()
removeToken() removeToken()
this.SET_TOKEN('') this.SET_TOKEN('')
this.SET_ROLES([]) this.SET_ROLES([])
@ -146,6 +181,10 @@ class User extends VuexModule implements IUserState {
} }
this.permissions = results this.permissions = results
} }
get encodeToken() {
return encodeURIComponent(this.token)
}
} }
export const UserModule = getModule(User) export const UserModule = getModule(User)

View File

@ -6,6 +6,7 @@ import Web3 from 'web3'
import { MessageBox } from 'element-ui' import { MessageBox } from 'element-ui'
import { ERC20ABI, MALL_ADDRESS } from '@/utils/config_chain' import { ERC20ABI, MALL_ADDRESS } from '@/utils/config_chain'
import { EventBus, NEED_LOGIN } from '@/utils/event-bus' import { EventBus, NEED_LOGIN } from '@/utils/event-bus'
import { UserModule } from '@/store/modules/user'
const EIP721_DOMAIN_DATA = [ const EIP721_DOMAIN_DATA = [
{ name: 'name', type: 'string' }, { name: 'name', type: 'string' },
@ -87,13 +88,14 @@ export class BlockChain {
return this.disconnect() return this.disconnect()
} }
} }
console.log('chainId: ', chainId)
console.log('accountsLogin: ', accounts)
if (accounts && accounts.length > 0) { if (accounts && accounts.length > 0) {
AppModule.updateAccount(accounts[0]) AppModule.updateAccount(accounts[0])
} }
AppModule.updateWalletStatus(true) AppModule.updateWalletStatus(true)
console.log('chainId: ', chainId)
console.log('accountsLogin: ', accounts, AppModule.accountId)
return { account: accounts[0], chainId }
} catch (err) { } catch (err) {
} }
} }
@ -118,6 +120,7 @@ export class BlockChain {
public async disconnect() { public async disconnect() {
try { try {
await UserModule.LogOut()
await this.provider?.disconnect() await this.provider?.disconnect()
} catch (err) { } catch (err) {
} }

View File

@ -14,6 +14,6 @@ export const setMini = (mini: string) => Cookies.set(miniKey, mini)
// User // User
const tokenKey = 'vue_typescript_access_token' const tokenKey = 'vue_typescript_access_token'
export const getToken = () => Cookies.get(tokenKey) export const getToken = () => localStorage.getItem(tokenKey)
export const setToken = (token: string) => Cookies.set(tokenKey, token) export const setToken = (token: string) => localStorage.setItem(tokenKey, token)
export const removeToken = () => Cookies.remove(tokenKey) export const removeToken = () => localStorage.removeItem(tokenKey)

View File

@ -12,7 +12,13 @@ service.interceptors.request.use(
(config) => { (config) => {
// Add X-Access-Token header to every request, you can add other custom headers here // Add X-Access-Token header to every request, you can add other custom headers here
if (UserModule.token) { if (UserModule.token) {
config.headers.authorization = 'Bearer ' + UserModule.token if (config.url) {
if (config.url.indexOf('?') > 0) {
config.url += `&token=${UserModule.encodeToken}`
} else {
config.url += `?token=${UserModule.encodeToken}`
}
}
} }
config.headers['Content-Type'] = 'application/json' config.headers['Content-Type'] = 'application/json'
return config return config