2016-11-22 63 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