-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusers.ts
More file actions
177 lines (168 loc) · 4.78 KB
/
Copy pathusers.ts
File metadata and controls
177 lines (168 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright © 2026 Fells Code, LLC
* Licensed under the GNU Affero General Public License v3.0
* See LICENSE file in the project root for full license information
*/
import { Association, DataTypes, Model, Sequelize } from 'sequelize';
import type { Credential } from './credentials.js';
import type { OAuthIdentity } from './oauthIdentities.js';
import type { OrganizationMembership } from './organizationMemberships.js';
import type { TotpCredential } from './totpCredentials.js';
export interface UserAttributes {
id?: string;
email: string;
phone: string | null;
roles?: string[];
revoked?: boolean;
emailVerificationToken?: string | null;
emailVerificationTokenExpiry?: number | null;
phoneVerificationToken?: string | null;
phoneVerificationTokenExpiry?: number | null;
emailVerified?: boolean;
phoneVerified?: boolean;
verified?: boolean;
challenge?: string | null;
challengeContext?: Record<string, unknown> | null;
lastLogin?: Date;
createdAt?: Date;
updatedAt?: Date;
credentials?: Credential[];
oauthIdentities?: OAuthIdentity[];
organizationMemberships?: OrganizationMembership[];
totpCredentials?: TotpCredential[];
}
export class User extends Model<UserAttributes> implements UserAttributes {
declare id: string;
declare email: string;
declare phone: string | null;
declare revoked: boolean;
declare emailVerificationToken: string | null;
declare emailVerificationTokenExpiry: number | null;
declare phoneVerificationToken: string | null;
declare phoneVerificationTokenExpiry: number | null;
declare emailVerified: boolean;
declare phoneVerified: boolean;
declare verified: boolean;
declare challenge: string | null;
declare challengeContext: Record<string, unknown> | null;
declare roles?: string[];
declare lastLogin?: Date;
declare readonly createdAt: Date;
declare readonly updatedAt: Date;
declare readonly credentials?: Credential[];
declare readonly oauthIdentities?: OAuthIdentity[];
declare readonly organizationMemberships?: OrganizationMembership[];
declare readonly totpCredentials?: TotpCredential[];
public static associations: {
credentials: Association<User, Credential>;
oauthIdentities: Association<User, OAuthIdentity>;
organizationMemberships: Association<User, OrganizationMembership>;
totpCredentials: Association<User, TotpCredential>;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static associate(models: any) {
User.hasMany(models.Credential, {
foreignKey: 'userId',
onDelete: 'CASCADE',
as: 'credentials',
});
User.hasMany(models.OAuthIdentity, {
foreignKey: 'userId',
onDelete: 'CASCADE',
as: 'oauthIdentities',
});
User.hasMany(models.TotpCredential, {
foreignKey: 'userId',
onDelete: 'CASCADE',
as: 'totpCredentials',
});
User.hasMany(models.OrganizationMembership, {
foreignKey: 'userId',
onDelete: 'CASCADE',
as: 'organizationMemberships',
});
}
}
const initializeUserModel = (sequelize: Sequelize) => {
User.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
phone: {
type: DataTypes.STRING,
allowNull: true,
unique: true,
},
roles: {
type: DataTypes.ARRAY(DataTypes.STRING),
allowNull: false,
defaultValue: [],
},
emailVerificationToken: {
type: DataTypes.STRING,
allowNull: true,
},
emailVerificationTokenExpiry: {
type: DataTypes.BIGINT,
allowNull: true,
},
phoneVerificationToken: {
type: DataTypes.STRING,
allowNull: true,
},
phoneVerificationTokenExpiry: {
type: DataTypes.BIGINT,
allowNull: true,
},
revoked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
verified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
emailVerified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
phoneVerified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
challenge: {
type: DataTypes.STRING,
allowNull: true,
},
challengeContext: {
type: DataTypes.JSON,
allowNull: true,
},
lastLogin: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
sequelize,
modelName: 'User',
tableName: 'users',
underscored: true,
},
);
return User;
};
export default initializeUserModel;