import Sequelize from "sequelize"; import bcrypt from "bcrypt"; import {validateFindOptions, validateMutation, getContextFromOptions} from "@vostro/c2-utils/lib/auth"; import waterfall from "@vostro/c2-utils/lib/waterfall"; import { C2Utils } from '@vostro/c2-utils/lib/types'; export default { name: "UserAuth", comment: "This is the user auth table", comments: { fields: { name: "This is the name of user auth that will be displayed.", type: "This is the type of user auth for organized identification.", token: "This is the token that is being generated for every user type and user.", }, }, define: { name: { type: Sequelize.STRING, allowNull: true, comment: "This is the name of user auth that will be displayed.", }, type: { type: Sequelize.STRING, allowNull: false, comment: "This is the type of user auth for organized identification.", }, token: { type: Sequelize.TEXT, allowNull: false, comment: "This is the token that is being generated for every user type and user.", }, }, relationships: [ { type: "belongsTo", model: "User", name: "user", options: { foreignKey: "userId", target: "id", }, }, ], options: { tableName: "user-auths", indexes: [{ unique: true, fields: ["userId", "type", "token"], }], hooks: { beforeFind: [async function beforeFind(options: C2Utils.FindOptions) { return validateFindOptions("UserAuth", options, "userId"); }], beforeCreate: [async function beforeCreate(instance: any, options: C2Utils.FindOptions) { instance = await validateMutation("UserAuth", "create", options, instance, undefined, false); if (instance.type === "local") { instance._token = instance.token; //eslint-disable-line const salt = await bcrypt.genSalt(5); instance.token = await bcrypt.hash(instance.token, salt); } return instance; }], beforeUpdate: [async function beforeUpdate(instance: any, options: C2Utils.FindOptions) { instance = await validateMutation("UserAuth", "update", options, instance, undefined, false); if (instance.type === "local") { instance._token = instance.token; //eslint-disable-line const salt = await bcrypt.genSalt(5); instance.token = await bcrypt.hash(instance.token, salt); } return instance; }], beforeDestroy: [async function beforeDestroy(instance: any, options: C2Utils.FindOptions) { return validateMutation("UserAuth", "destroy", options, instance, undefined, false); }], afterFind: [async function afterFind(instance: any, options: C2Utils.FindOptions) { const context = await getContextFromOptions(options); if (context.override || options.override) { return instance; } let role: any = {}; if (context) { if (context.getUser) { const user = await context.getUser(); role = await user.getRole({override: true}); } } if (role.name !== "admin") { if (Array.isArray(instance)) { await waterfall(instance, async(value) => { value.token = value.type === "token" ? value.token : ""; return value; }); } else { if (instance) { instance.token = instance.type === "token" ? instance.token : ""; } } } return instance; }], }, classMethods: {}, instanceMethods: {}, }, };