Breaking down mega-package. Hello auth-db 1.0.0!
This commit is contained in:
17
lib/api/authenticate.ts
Normal file
17
lib/api/authenticate.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Auth, Log } from '..';
|
||||
import { Action } from '../../constants/action';
|
||||
import { getLoginToken } from '../utils/getLoginToken';
|
||||
|
||||
export const authenticate = async (username: string, password: string) => {
|
||||
const doc = await Auth.findByUsername(username).catch();
|
||||
if (!!doc && (await doc.authenticate(password))) {
|
||||
Log.add(doc.id, Action.AUTHENTICATE);
|
||||
return { ...doc, token: getLoginToken(doc) };
|
||||
}
|
||||
|
||||
if (doc) {
|
||||
Log.add(doc.id, Action.AUTHENTICATE_FAILURE);
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
15
lib/api/deleteStrategy.ts
Normal file
15
lib/api/deleteStrategy.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { StringSchemaDefinition } from 'mongoose';
|
||||
import { Auth, Strategy } from '..';
|
||||
|
||||
export const deleteStrategy = async (id: StringSchemaDefinition) => {
|
||||
const strategy = await Strategy.findById(id);
|
||||
|
||||
if (strategy) {
|
||||
const parentId = strategy.parent;
|
||||
await strategy.deleteOne();
|
||||
await Auth.findOneAndUpdate({ id: parentId, strategies: { $pull: id } });
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
18
lib/api/resetPasswordGet.ts
Normal file
18
lib/api/resetPasswordGet.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { readOneByUsername } from '../dao/readOneByUsername';
|
||||
import { Log, Token } from '..';
|
||||
import { TokenType } from '../../constants/tokens';
|
||||
import { Action } from '../../constants/action';
|
||||
|
||||
export const resetPasswordGet = async (username: string) => {
|
||||
const doc = await readOneByUsername(username);
|
||||
|
||||
if (doc) {
|
||||
Log.add(doc._id, Action.RESET_REQUEST);
|
||||
return {
|
||||
record: doc.record,
|
||||
token: Token.getToken(TokenType.RESET, doc._id),
|
||||
};
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
38
lib/api/resetPasswordPost.ts
Normal file
38
lib/api/resetPasswordPost.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Types } from 'mongoose';
|
||||
|
||||
import { Log, Strategy, Token } from '..';
|
||||
import { STRATEGIES } from '../../constants/strategies';
|
||||
import { AuthDocument } from '../schema/auth';
|
||||
import { getLoginToken } from '../utils/getLoginToken';
|
||||
import { StrategyDocument } from '../schema/strategy';
|
||||
import { Action } from '../../constants/action';
|
||||
|
||||
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: <Types.ObjectId>parentId,
|
||||
});
|
||||
|
||||
parent = await strategy.getAuthRecord();
|
||||
parent.strategies.push(strategy._id);
|
||||
await parent.save();
|
||||
}
|
||||
|
||||
Log.add(parent._id, Action.RESET);
|
||||
return { record: parent.record, token: getLoginToken(parent) };
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
Reference in New Issue
Block a user