Skip to content

Commit

Permalink
fix(verifyToken): remove token once used
Browse files Browse the repository at this point in the history
password & email tokens must be removed after use

fix #5
  • Loading branch information
thetutlage committed Mar 31, 2018
1 parent dc02443 commit 328c22b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Persona.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ class Persona {
return row && row.getRelated('user') ? row : null
}

/**
* Remvoes the token from the tokens table
*
* @method removeToken
*
* @param {String} token
* @param {String} type
*
* @return {void}
*/
async removeToken (token, type) {
const query = this.getModel().prototype.tokens().RelatedModel.query()
await query.where('token', token).where('type', type).delete()
}

/**
* Returns the model class
*
Expand Down Expand Up @@ -544,6 +559,7 @@ class Persona {
*/
if (user.account_status === this.config.newAccountState) {
user.account_status = this.config.verifiedAccountState
this.removeToken(token, 'email')
await user.save()
}

Expand Down Expand Up @@ -733,9 +749,10 @@ class Persona {
const user = tokenRow.getRelated('user')
this._setPassword(user, this._getPassword(payload))

this.Event.fire('password::recovered', { user })
await user.save()
await this.removeToken(token, 'password')

this.Event.fire('password::recovered', { user })
return user
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/persona.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ test.group('Persona', (group) => {

await user.reload()
assert.equal(user.account_status, 'active')

const tokens = await user.tokens().fetch()
assert.equal(tokens.size(), 0)
})

test('do not set to active when initial state is not pending', async (assert) => {
Expand Down Expand Up @@ -644,6 +647,9 @@ test.group('Persona', (group) => {
Event.restore()

await user.reload()
const tokens = await user.tokens().fetch()
assert.equal(tokens.size(), 0)

const verified = await use('Hash').verify('newsecret', user.password)
assert.isTrue(verified)
})
Expand Down

0 comments on commit 328c22b

Please sign in to comment.