首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Adonisjs恢复令牌模型迁移

Adonisjs恢复令牌模型迁移
EN

Stack Overflow用户
提问于 2021-04-22 07:22:42
回答 1查看 30关注 0票数 0

默认情况下,当您创建一个新的Adonisjs项目时,已经有了一个具有所需值的现成令牌模型迁移。我把它删了。如何恢复此迁移?

EN

回答 1

Stack Overflow用户

发布于 2021-04-22 21:28:56

好吧,我必须创建一个新的项目,并从那里开始(我只是想有某种命令来恢复令牌迁移)。令牌迁移:

代码语言:javascript
复制
'use strict'

/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')

class TokensSchema extends Schema {
  up () {
    this.create('tokens', (table) => {
      table.increments()
      table.integer('user_id').unsigned().references('id').inTable('users')
      table.string('token', 255).notNullable().unique().index()
      table.string('type', 80).notNullable()
      table.boolean('is_revoked').defaultTo(false)
      table.timestamps()
    })
  }

  down () {
    this.drop('tokens')
  }
}

module.exports = TokensSchema

用户模型:

代码语言:javascript
复制
'use strict'

/** @type {import('@adonisjs/framework/src/Hash')} */
const Hash = use('Hash')

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class User extends Model {
  static boot () {
    super.boot()

    /**
     * A hook to hash the user password before saving
     * it to the database.
     */
    this.addHook('beforeSave', async (userInstance) => {
      if (userInstance.dirty.password) {
        userInstance.password = await Hash.make(userInstance.password)
      }
    })
  }

  /**
   * A relationship on tokens is required for auth to
   * work. Since features like `refreshTokens` or
   * `rememberToken` will be saved inside the
   * tokens table.
   *
   * @method tokens
   *
   * @return {Object}
   */
  tokens () {
    return this.hasMany('App/Models/Token')
  }
}

module.exports = User
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67204684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档