2016-11-22 72 views
4

我已经通过阿多尼斯JS - 哈希密码

  1. http://adonisjs.com/docs/3.1/database-hooks#_hooks_events
  2. http://adonisjs.com/docs/3.1/encryption-and-hashing#_hashing_values
  3. https://adonisjs.svbtle.com/basic-authentication-with-adonisjs#using-hash-provider_3
  4. https://auth0.com/blog/creating-your-first-app-with-adonisj-and-adding-authentication/

和几个了。

这应该是相当简单的,但我不知道为什么我无法弄清楚。我想使用Adonis的验证工具,同时“登录”。为此,我需要在保存之前对密码进行哈希处理。我被困在这里。

查看

<h1>Sign up</h1> 
{{ form.open({url: '/addNew', action: 'UserController.addNewUser'}) }} 

{{ csrfField }} 

<div class="field"> 
{{ form.label('username', 'Choose a username') }} 
{{ form.text('username') }} 
</div> 

<div class="field"> 
{{ form.label('email', 'Enter email address') }} 
{{ form.text('email') }} 
</div> 

<div class="field"> 
{{ form.label('password', 'Choose a strong password') }} 
{{ form.password('password') }} 
</div> 

<div class="button"> 
{{ form.submit('Sign Up') }} 
</div> 

{{ form.close() }} 

控制器:UserController中

'use strict' 

const Database = use('Database') 
const User = use('App/Model/User') 
const user = new User() 

class UserController { 

* index (request, response) { 
const users = yield Database.select('*').from('users') 
response.json(users) 
} 

* addNewUser (request, response){ 

user.name = request.input('username') 
user.email = request.input('email') 
user.password = request.input('password') 
user.entry = "Lorem Ipsum"; 

//Insert into database 
    const userId = yield Database 
.insert({name: user.name, email: user.email, password: user.password, entry: user.entry}) 
.into('users') 

response.json(userId) 
} 
} 
module.exports = UserController 

型号:用户

'use strict' 

const Lucid = use('Lucid') 


class User extends Lucid { 

static boot() { 
    super.boot() 
    this.addHook('beforeCreate', 'User.encryptPassword') 
    } 
} 


module.exports = User 

钩:用户

'use strict' 

const Hash = use('Hash') 
const User = exports = module.exports = {} 

User.encryptPassword = function * (next) { 
    this.password = yield Hash.make(request.input('password')) 
    yield next 
} 

谢谢!

回答

0

有一种方法可以不使用钩子或模型。只需将控制器本身的密码散列化即可。但我想用Hook来做。不管怎么说,这里是代码:

控制器:UserController中 - > AddNewUser函数()

user.name = request.input('username') 
user.email = request.input('email') 
const pswd = request.input('password') 
user.password = yield Hash.make(pswd) 

注:我不知道到底是哪加密Hash.make一样。但Adonis的encryption工具无法验证密码。还有一件事,哈希密码始终以$ 2a $ 10 $开头。伙计们请帮忙!

+1

阿多尼斯使用bcrypt https://en.wikipedia.org/wiki/Bcrypt有Blowfish密码。这就是为什么它始于$ 2a $ 10 $ –

1

您应该使用Model本身来创建记录。为什么使用Database供应商呢?

没有在文档中说新建模型,然后使用数据库提供者进行调用。所以它应该是

控制器

* addNewUser (request, response) { 
    const user = new User() 
    user.name = request.input('username') 
    user.email = request.input('email') 
    user.password = request.input('password') 
    user.entry = "Lorem Ipsum"; 
    yield user.save() 
    response.json(user.id) 
} 

而且你的钩子里,你没有访问request对象。我相信你没有理会阅读文档。

挂钩

'use strict' 

const Hash = use('Hash') 
const User = exports = module.exports = {} 

User.encryptPassword = function * (next) { 
    this.password = yield Hash.make(this.password) 
    yield next 
} 

检查文档的钩子这里http://adonisjs.com/docs/3.1/database-hooks#_basic_example

+0

很好的答案!正如你所说的,我没有去阅读文档,只是简单的编写东西! – Roy