import { Types } from 'mongoose'; import { Action } from '@mifi/auth-common/lib/enums/action'; import { STRATEGIES } from '@mifi/auth-common/lib/enums/strategies'; import { generateLoginToken } from '@mifi/auth-common/lib/utils/generateLoginToken'; import { AuthDocument } from '../schema/auth'; import { StrategyDocument } from '../schema/strategy'; import { Log, Strategy, Token } from '..'; export const resetPasswordPost = async (token: string, password: string) => { const parentId = await Token.validateResetToken(token); if (parentId) { let parent: AuthDocument; let strategy: StrategyDocument | null = await Strategy.findOne({ parent: parentId, method: STRATEGIES.LOCAL, }); if (strategy) { parent = await strategy.getAuthRecord(); strategy.key = password; await strategy.save(); } else { strategy = await Strategy.create({ key: password, method: STRATEGIES.LOCAL, parent: parentId, }); parent = await strategy.getAuthRecord(); parent.strategies.push(strategy._id); await parent.save(); } Log.add(parent._id, Action.RESET); return { record: parent.record, token: generateLoginToken(parent) }; } return false; };