我已经通过阿多尼斯JS - 哈希密码
- http://adonisjs.com/docs/3.1/database-hooks#_hooks_events
- http://adonisjs.com/docs/3.1/encryption-and-hashing#_hashing_values
- https://adonisjs.svbtle.com/basic-authentication-with-adonisjs#using-hash-provider_3
- 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
}
谢谢!
阿多尼斯使用bcrypt https://en.wikipedia.org/wiki/Bcrypt有Blowfish密码。这就是为什么它始于$ 2a $ 10 $ –