Skip to content
Snippets Groups Projects
Commit e3da3823 authored by Louis's avatar Louis :fire:
Browse files

Add generateResetToken to user model

parent 79409d95
No related branches found
No related tags found
No related merge requests found
......@@ -74,6 +74,24 @@ module.exports = (sequelize, DataTypes) => {
return await crypto.verify(this.password, password)
}
Model.prototype.generateResetToken = async function() {
const crypto = require('core/utils/crypto')
const moment = require('moment')
const id = this.id
const expires = moment.utc()
.add(1, 'hour')
.toISOString()
const token = await crypto.encrypt(JSON.stringify({ id, expires }))
this.reset_token = token
await this.save()
return token
}
Model.createSystemUser = async function() {
const crypto = require('core/utils/crypto')
const user = Model.build({
......
......@@ -42,7 +42,7 @@ exports.resetPassword = async ctx => {
}
const { sequelize } = require('database/models')
const [{ exists }] = await sequelize.query('SELECT exists(select reset_token from users where reset_token = :token limit 1)', { replacements: { token } })
const [ [ { exists } ] ] = await sequelize.query('SELECT exists(select reset_token from users where reset_token = :token AND deleted_at is null limit 1)', { replacements: { token } })
if (!exists) {
errorData.message = resetErrorMessages.invalid
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment