2017-11-16 06:56:40 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import randomstring from "randomstring";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { DataTypes, sequelize } from "../sequelize";
|
2016-08-24 07:37:54 +00:00
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
const ApiKey = sequelize.define(
|
2021-03-23 03:50:12 +00:00
|
|
|
"apiKey",
|
2017-04-27 04:47:03 +00:00
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true,
|
2016-08-24 07:37:54 +00:00
|
|
|
},
|
2017-04-27 04:47:03 +00:00
|
|
|
name: DataTypes.STRING,
|
|
|
|
secret: { type: DataTypes.STRING, unique: true },
|
2016-08-24 07:37:54 +00:00
|
|
|
},
|
2017-04-27 04:47:03 +00:00
|
|
|
{
|
|
|
|
paranoid: true,
|
|
|
|
hooks: {
|
2020-08-09 01:53:11 +00:00
|
|
|
beforeValidate: (key) => {
|
2017-04-27 04:47:03 +00:00
|
|
|
key.secret = randomstring.generate(38);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2016-08-24 07:37:54 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
ApiKey.associate = (models) => {
|
2018-07-11 03:44:59 +00:00
|
|
|
ApiKey.belongsTo(models.User, {
|
2020-06-20 20:59:15 +00:00
|
|
|
as: "user",
|
|
|
|
foreignKey: "userId",
|
2018-07-11 03:44:59 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-26 00:21:33 +00:00
|
|
|
export default ApiKey;
|