SteemJs登录的逻辑 / 网络研习社#57 by lemooljiang

View this thread on steempeak.com
· @lemooljiang ·
$8.23
SteemJs登录的逻辑 / 网络研习社#57
![login.jpg](https://www.steemjiang.com:8081/ipfs/Qmcz3RQuTiPPtjjiChZSGE1q7f5oyXdQYBPyDsHYzFFFa8)

Steem的登录有个麻烦事:Steem为了安全,设置了多层密码,什么posting key , active key......之类的,对于大部分人来讲,都是相当复杂的。而基于Steem设计的应用要想实现登录功能也绕不开这个麻烦事。

以前为了省事,做的SteemJiang是限定只能posting key登录!不过有些朋友反映SteemJiang登录不上去,也不知怎么查posting key。好吧,你就折腾着去查吧!

**后来做链课,也充分考虑了多项因素,还是决定任意密码都可登录!**

如上图所示,用户可以使用posting key,也可以使用密码,都可以正常使用,无非是在登录时多做了一层逻辑判断。

在用密码登录时要多做一步生成密钥的动作。在js中是这个函数` steem.auth.toWif(account, password, role)` ,以用户名和密码来生成私钥。这个是固定算法,生成的结果会一致。然后,再用这个私钥登录就可以啰。

具体代码如下,有兴趣的朋友可以参考下:

```js
steem-login(){
  //用私钥或密码登录
  this.isLoading = true
  let account = this.username
  this.steem.api.getAccounts([account], (err, result) => {
    if(result.length === 0){
      this.isLoading = false
      alert("用户不存在!")
    }else{
      //用户存在
      let pubWif = result[0].posting.key_auths[0][0]
      let privWif = this.password
      let isvalid
      //判断是私钥还是密码
      let res = this.steem.auth.isWif(privWif)
      if(res === true){
        //是私钥
        console.log(456, res, '是私钥!')
        try{
          isvalid = this.steem.auth.wifIsValid(privWif, pubWif)
        }
        catch(e){
          isvalid = 'false'
        }
        if(isvalid === true){
          this.isLoading = false
          console.log(' Welcome.', account)
          this.$store.commit('saveUser', {username: account, password: privWif})
          this.$router.push({path:'/'})
        }else{
          this.isLoading = false
          alert(`错误!请检查用户名和发贴密钥!`)
        }
      }else{
        //是密码
        console.log(458, res, '是密码')
        //获取发贴私钥
        let role = ["posting"]
        let Wif = this.steem.auth.toWif(account, privWif, role)
        try{
          isvalid = this.steem.auth.wifIsValid(Wif, pubWif)
        }
        catch(e){
          isvalid = 'false'
        }
        if(isvalid === true){
          this.isLoading = false
          console.log(' Welcome.', account)
          this.$store.commit('saveUser', {username: account, password: Wif})
          this.$router.push({path:'/'})
        }else{
          this.isLoading = false
          alert(`错误!请检查用户名和密码!`)
        }
      }
    }
  })
}
```

👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id83,420,983
authorlemooljiang
permlinknehth
categorycn
json_metadata{"tags":["cn","network-institute","steemjs","steemdev","login"],"app":"steemit\/1.0","format":"markdown"}
created2020-01-14 03:47:21
last_update2020-01-14 03:47:21
depth0
children2
net_rshares29,993,585,367,423
last_payout2020-01-21 03:47:21
cashout_time1969-12-31 23:59:59
total_payout_value4.231 SBD
curator_payout_value4.002 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2,167
author_reputation263,700,595,848,611
root_title"SteemJs登录的逻辑 / 网络研习社#57"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (53)
@ericet ·
@tipu curate 
properties (22)
post_id83,421,406
authorericet
permlinkre-lemooljiang-nehth-20200114t041553262z
categorycn
json_metadata{"community":"busy","app":"busy\/2.5.6","format":"markdown","tags":["cn"],"users":["tipu"],"links":["\/@tipu"]}
created2020-01-14 04:15:54
last_update2020-01-14 04:15:54
depth1
children1
net_rshares0
last_payout2020-01-21 04:15:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length13
author_reputation359,381,366,380,462
root_title"SteemJs登录的逻辑 / 网络研习社#57"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@tipu ·
<a href="https://tipu.online/curator?ericet" target="_blank">Upvoted &#128076;</a> (Mana: 0/4 - <a href="https://steemit.com/steem/@tipu/tipu-curate-project-update-recharging-curation-mana" target="_blank">need recharge</a>?)
properties (22)
post_id83,421,420
authortipu
permlinkre-re-lemooljiang-nehth-20200114t041553262z-20200114t041626
categorycn
json_metadata{}
created2020-01-14 04:16:45
last_update2020-01-14 04:16:45
depth2
children0
net_rshares0
last_payout2020-01-21 04:16:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length225
author_reputation55,804,171,747,699
root_title"SteemJs登录的逻辑 / 网络研习社#57"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000