我已經通過阿多尼斯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 $ –