@@ -1,24 +1,65 @@
|
||||
import { DataTypes, sequelize, encryptedFields } from "../sequelize";
|
||||
import {
|
||||
BelongsTo,
|
||||
Column,
|
||||
DataType,
|
||||
ForeignKey,
|
||||
Table,
|
||||
Unique,
|
||||
} from "sequelize-typescript";
|
||||
import AuthenticationProvider from "./AuthenticationProvider";
|
||||
import User from "./User";
|
||||
import BaseModel from "./base/BaseModel";
|
||||
import Encrypted, {
|
||||
getEncryptedColumn,
|
||||
setEncryptedColumn,
|
||||
} from "./decorators/Encrypted";
|
||||
import Fix from "./decorators/Fix";
|
||||
|
||||
const UserAuthentication = sequelize.define("user_authentications", {
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
scopes: DataTypes.ARRAY(DataTypes.STRING),
|
||||
accessToken: encryptedFields().vault("accessToken"),
|
||||
refreshToken: encryptedFields().vault("refreshToken"),
|
||||
providerId: {
|
||||
type: DataTypes.STRING,
|
||||
unique: true,
|
||||
},
|
||||
});
|
||||
@Table({ tableName: "user_authentications", modelName: "user_authentication" })
|
||||
@Fix
|
||||
class UserAuthentication extends BaseModel {
|
||||
@Column(DataType.ARRAY(DataType.STRING))
|
||||
scopes: string[];
|
||||
|
||||
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'models' implicitly has an 'any' type.
|
||||
UserAuthentication.associate = (models) => {
|
||||
UserAuthentication.belongsTo(models.AuthenticationProvider);
|
||||
UserAuthentication.belongsTo(models.User);
|
||||
};
|
||||
@Column(DataType.BLOB)
|
||||
@Encrypted
|
||||
get accessToken() {
|
||||
return getEncryptedColumn(this, "accessToken");
|
||||
}
|
||||
|
||||
set accessToken(value: string) {
|
||||
setEncryptedColumn(this, "accessToken", value);
|
||||
}
|
||||
|
||||
@Column(DataType.BLOB)
|
||||
@Encrypted
|
||||
get refreshToken() {
|
||||
return getEncryptedColumn(this, "refreshToken");
|
||||
}
|
||||
|
||||
set refreshToken(value: string) {
|
||||
setEncryptedColumn(this, "refreshToken", value);
|
||||
}
|
||||
|
||||
@Column
|
||||
providerId: string;
|
||||
|
||||
// associations
|
||||
|
||||
@BelongsTo(() => User, "userId")
|
||||
user: User;
|
||||
|
||||
@ForeignKey(() => User)
|
||||
@Column(DataType.UUID)
|
||||
userId: string;
|
||||
|
||||
@BelongsTo(() => AuthenticationProvider, "providerId")
|
||||
authenticationProvider: AuthenticationProvider;
|
||||
|
||||
@ForeignKey(() => AuthenticationProvider)
|
||||
@Unique
|
||||
@Column(DataType.UUID)
|
||||
authenticationProviderId: string;
|
||||
}
|
||||
|
||||
export default UserAuthentication;
|
||||
|
||||
Reference in New Issue
Block a user