mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
added authentication, $schema, fixed media adapter mcp
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { DB } from "bknd";
|
||||
import type { DB, PrimaryFieldType } from "bknd";
|
||||
import * as AuthPermissions from "auth/auth-permissions";
|
||||
import type { AuthStrategy } from "auth/authenticate/strategies/Strategy";
|
||||
import type { PasswordStrategy } from "auth/authenticate/strategies/PasswordStrategy";
|
||||
@@ -87,6 +87,7 @@ export class AppAuth extends Module<AppAuthSchema> {
|
||||
super.setBuilt();
|
||||
|
||||
this._controller = new AuthController(this);
|
||||
this._controller.registerMcp();
|
||||
this.ctx.server.route(this.config.basepath, this._controller.getController());
|
||||
this.ctx.guard.registerPermissions(AuthPermissions);
|
||||
}
|
||||
@@ -176,6 +177,32 @@ export class AppAuth extends Module<AppAuthSchema> {
|
||||
return created;
|
||||
}
|
||||
|
||||
async changePassword(userId: PrimaryFieldType, newPassword: string) {
|
||||
const users_entity = this.config.entity_name as "users";
|
||||
const { data: user } = await this.em.repository(users_entity).findId(userId);
|
||||
if (!user) {
|
||||
throw new Error("User not found");
|
||||
} else if (user.strategy !== "password") {
|
||||
throw new Error("User is not using password strategy");
|
||||
}
|
||||
|
||||
const togglePw = (visible: boolean) => {
|
||||
const field = this.em.entity(users_entity).field("strategy_value")!;
|
||||
|
||||
field.config.hidden = !visible;
|
||||
field.config.fillable = visible;
|
||||
};
|
||||
|
||||
const pw = this.authenticator.strategy("password" as const) as PasswordStrategy;
|
||||
togglePw(true);
|
||||
await this.em.mutator(users_entity).updateOne(user.id, {
|
||||
strategy_value: await pw.hash(newPassword),
|
||||
});
|
||||
togglePw(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
override toJSON(secrets?: boolean): AppAuthSchema {
|
||||
if (!this.config.enabled) {
|
||||
return this.configDefault;
|
||||
|
||||
Reference in New Issue
Block a user