Restructuring the folders (#1)
Co-authored-by: mifi <badmf@mifi.dev> Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
81
src/schema/strategy.ts
Normal file
81
src/schema/strategy.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Document, InferSchemaType, Model, Schema, StringSchemaDefinition, Types } from 'mongoose';
|
||||
|
||||
import { STRATEGIES } from '../constants/strategies';
|
||||
import { encrypt } from '../utils/password';
|
||||
import { COLL_AUTH } from '../constants/db';
|
||||
import { AuthDocument } from './auth';
|
||||
import { Strategy } from '..';
|
||||
|
||||
export interface Strategy {
|
||||
method: STRATEGIES;
|
||||
parent: StringSchemaDefinition | AuthDocument;
|
||||
externalId?: string;
|
||||
key: string;
|
||||
profile?: { [key: string]: string | boolean | number };
|
||||
forceReset?: boolean;
|
||||
}
|
||||
|
||||
interface StrategyBaseDocument extends Strategy, Document {
|
||||
getAuthRecord(): Promise<AuthDocument>;
|
||||
getPopulatedStrategy(): Promise<StrategyPopulatedDocument>;
|
||||
}
|
||||
|
||||
export interface StrategyDocument extends StrategyBaseDocument {
|
||||
parent: StringSchemaDefinition;
|
||||
}
|
||||
|
||||
export interface StrategyPopulatedDocument extends StrategyBaseDocument {
|
||||
parent: AuthDocument;
|
||||
}
|
||||
|
||||
export type StrategyModel = Model<StrategyDocument>;
|
||||
|
||||
export const StrategySchema = new Schema<StrategyDocument, StrategyModel>(
|
||||
{
|
||||
method: {
|
||||
type: Number,
|
||||
enum: STRATEGIES,
|
||||
index: true,
|
||||
},
|
||||
externalId: { type: String, index: true },
|
||||
forceReset: { type: Boolean },
|
||||
key: { type: String, required: true, trim: true },
|
||||
parent: {
|
||||
type: Types.ObjectId,
|
||||
ref: COLL_AUTH,
|
||||
required: true,
|
||||
},
|
||||
profile: {},
|
||||
},
|
||||
{
|
||||
minimize: true,
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
StrategySchema.methods.getPopulatedStrategy = async function (this: StrategyDocument) {
|
||||
return this.populate<StrategyPopulatedDocument>('parent');
|
||||
};
|
||||
|
||||
StrategySchema.methods.getAuthRecord = async function (this: StrategyDocument) {
|
||||
return (await this.getPopulatedStrategy()).parent;
|
||||
};
|
||||
|
||||
StrategySchema.pre('save', async function save(next) {
|
||||
if (typeof this.method === 'undefined') {
|
||||
return next(new Error(`Strategy requires a method.`));
|
||||
}
|
||||
|
||||
if (await Strategy.findOne({ method: this.method, parent: this.parent })) {
|
||||
return next(new Error(`${this.method} strategy already exists for this user.`));
|
||||
}
|
||||
|
||||
if (this.method !== STRATEGIES.LOCAL || !this.isModified('key')) {
|
||||
return next();
|
||||
}
|
||||
|
||||
this.key = encrypt(this.key);
|
||||
return next();
|
||||
});
|
||||
|
||||
export type StrategySchema = InferSchemaType<typeof StrategySchema>;
|
||||
Reference in New Issue
Block a user