OpenAuth_server/src/models/user.ts

135 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-11-06 19:48:50 +00:00
import DB from "../database";
import { ModelDataBase } from "@hibas123/safe_mongo/lib/model";
import { ObjectID } from "mongodb";
import { v4 } from "uuid";
2019-01-21 10:24:20 +00:00
import { randomString } from "../helper/random";
2018-11-06 19:48:50 +00:00
export enum Gender {
none,
male,
female,
2020-08-07 14:16:39 +00:00
other,
2018-11-06 19:48:50 +00:00
}
export interface IUser extends ModelDataBase {
uid: string;
username: string;
name: string;
2020-08-07 14:16:39 +00:00
birthday?: Date;
2018-11-06 19:48:50 +00:00
gender: Gender;
admin: boolean;
password: string;
salt: string;
mails: ObjectID[];
2020-08-07 14:16:39 +00:00
phones: { phone: string; verified: boolean; primary: boolean }[];
2019-01-21 10:24:20 +00:00
encryption_key: string;
2018-11-06 19:48:50 +00:00
}
const User = DB.addModel<IUser>({
name: "user",
2020-08-07 14:16:39 +00:00
versions: [
{
migration: () => {},
schema: {
uid: { type: String, default: () => v4() },
username: { type: String },
name: { type: String },
birthday: { type: Date, optional: true },
gender: { type: Number },
admin: { type: Boolean },
password: { type: String },
salt: { type: String },
mails: { type: Array, default: () => [] },
phones: {
array: true,
model: true,
type: {
phone: { type: String },
verified: { type: Boolean },
primary: { type: Boolean },
},
},
twofactor: {
array: true,
model: true,
type: {
token: { type: String },
valid: { type: Boolean },
type: { type: Number },
},
},
2018-11-06 19:48:50 +00:00
},
2020-08-07 14:16:39 +00:00
},
{
migration: (e: IUser) => {
e.encryption_key = randomString(64);
2019-01-21 10:24:20 +00:00
},
2020-08-07 14:16:39 +00:00
schema: {
uid: { type: String, default: () => v4() },
username: { type: String },
name: { type: String },
birthday: { type: Date, optional: true },
gender: { type: Number },
admin: { type: Boolean },
password: { type: String },
salt: { type: String },
mails: { type: Array, default: () => [] },
phones: {
array: true,
model: true,
type: {
phone: { type: String },
verified: { type: Boolean },
primary: { type: Boolean },
},
},
twofactor: {
array: true,
model: true,
type: {
token: { type: String },
valid: { type: Boolean },
type: { type: Number },
},
},
encryption_key: {
type: String,
default: () => randomString(64),
},
2019-01-21 10:24:20 +00:00
},
2020-08-07 14:16:39 +00:00
},
{
migration: (e: any) => {
delete e.twofactor;
2019-03-13 01:06:09 +00:00
},
2020-08-07 14:16:39 +00:00
schema: {
uid: { type: String, default: () => v4() },
username: { type: String },
name: { type: String },
birthday: { type: Date, optional: true },
gender: { type: Number },
admin: { type: Boolean },
password: { type: String },
salt: { type: String },
mails: { type: Array, default: () => [] },
phones: {
array: true,
model: true,
type: {
phone: { type: String },
verified: { type: Boolean },
primary: { type: Boolean },
},
},
encryption_key: {
type: String,
default: () => randomString(64),
},
},
},
],
});
2018-11-06 19:48:50 +00:00
2020-08-07 14:16:39 +00:00
export default User;