@loomcore/api 0.2.28 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { type IModelSpec } from "@loomcore/common/models";
1
+ import { type ILoginResponse, type IModelSpec } from "@loomcore/common/models";
2
2
  import type { Application, Request, Response } from "express";
3
3
  import type { IDatabase } from "../databases/models/index.js";
4
4
  import { OrganizationService, UserService } from "../services/index.js";
@@ -21,7 +21,7 @@ export declare class AuthController {
21
21
  login(req: Request, res: Response): Promise<void>;
22
22
  requestTokenUsingRefreshToken(req: Request, res: Response): Promise<void>;
23
23
  getUserContext(req: Request, res: Response): Promise<void>;
24
- afterAuth(_req: Request, _res: Response, _loginResponse: any): void;
24
+ afterAuth(_req: Request, _res: Response, _loginResponse: ILoginResponse): Promise<void>;
25
25
  changePassword(req: Request, res: Response): Promise<void>;
26
26
  forgotPassword(req: Request, res: Response): Promise<void>;
27
27
  resetPassword(req: Request, res: Response): Promise<void>;
@@ -77,7 +77,7 @@ let AuthController = (() => {
77
77
  }
78
78
  mapRoutes(app) {
79
79
  const authorize = (method) => authorizeMethod(this, method);
80
- app.post(`/api/auth/login`, this.login.bind(this), this.afterAuth.bind(this));
80
+ app.post(`/api/auth/login`, this.login.bind(this));
81
81
  app.get(`/api/auth/refresh`, this.requestTokenUsingRefreshToken.bind(this));
82
82
  app.get(`/api/auth/get-user-context`, authorize('getUserContext'), this.getUserContext.bind(this));
83
83
  app.patch(`/api/auth/change-password`, authorize('changePassword'), this.changePassword.bind(this));
@@ -106,6 +106,7 @@ let AuthController = (() => {
106
106
  res.set("Content-Type", "application/json");
107
107
  const deviceId = getAndSetDeviceIdCookie(req, res);
108
108
  const loginResponse = await attemptLogin(this.database, email, password, deviceId, organization, this.userService);
109
+ await this.afterAuth(req, res, loginResponse);
109
110
  apiUtils.apiResponse(res, 200, { data: loginResponse }, this.loginResponseSpec);
110
111
  }
111
112
  async requestTokenUsingRefreshToken(req, res) {
@@ -128,9 +129,7 @@ let AuthController = (() => {
128
129
  const userContext = req.userContext;
129
130
  apiUtils.apiResponse(res, 200, { data: userContext }, this.userContextSpec);
130
131
  }
131
- afterAuth(_req, _res, _loginResponse) {
132
- console.log("in afterAuth");
133
- }
132
+ async afterAuth(_req, _res, _loginResponse) { }
134
133
  async changePassword(req, res) {
135
134
  const userContext = req.userContext;
136
135
  if (!userContext) {
@@ -0,0 +1,2 @@
1
+ import type { IUserContext } from "@loomcore/common/models";
2
+ export declare function assertAdminOrFeature(userContext: IUserContext, features: string[], requireAll?: boolean): void;
@@ -0,0 +1,8 @@
1
+ import { isAdmin } from "./is-admin.util.js";
2
+ import { assertUserHasFeature } from "./assert-user-has-feature.util.js";
3
+ export function assertAdminOrFeature(userContext, features, requireAll = false) {
4
+ if (isAdmin(userContext)) {
5
+ return;
6
+ }
7
+ assertUserHasFeature(userContext, features, requireAll);
8
+ }
@@ -1,2 +1,2 @@
1
1
  import { IUserContext } from "@loomcore/common/models";
2
- export declare function assertUserHasFeature(userContext: IUserContext, features: string[]): void;
2
+ export declare function assertUserHasFeature(userContext: IUserContext, features: string[], requireAll?: boolean): void;
@@ -1,10 +1,18 @@
1
1
  import { UnauthorizedError } from "../../errors/index.js";
2
- export function assertUserHasFeature(userContext, features) {
2
+ export function assertUserHasFeature(userContext, features, requireAll = false) {
3
3
  if (features.length === 0) {
4
4
  return;
5
5
  }
6
6
  const missingFeatures = features.filter(feature => !userContext.features.includes(feature));
7
- if (missingFeatures.length > 0) {
8
- throw new UnauthorizedError(missingFeatures);
7
+ if (requireAll) {
8
+ if (missingFeatures.length > 0) {
9
+ throw new UnauthorizedError(missingFeatures);
10
+ }
9
11
  }
12
+ else {
13
+ if (missingFeatures.length === features.length) {
14
+ throw new UnauthorizedError(missingFeatures);
15
+ }
16
+ }
17
+ return;
10
18
  }
@@ -1,4 +1,4 @@
1
1
  import { type ILoginResponse, type IOrganization } from "@loomcore/common/models";
2
2
  import type { IDatabase } from "../../databases/models/index.js";
3
3
  import { UserService } from "../../services/user.service.js";
4
- export declare function attemptLogin(database: IDatabase, email: string, password: string, deviceId: string, organization: IOrganization | null, userService?: UserService): Promise<ILoginResponse | null>;
4
+ export declare function attemptLogin(database: IDatabase, email: string, password: string, deviceId: string, organization: IOrganization | null, userService?: UserService): Promise<ILoginResponse>;
@@ -1,4 +1,5 @@
1
1
  export * from "./assert-admin.util.js";
2
+ export * from "./assert-admin-or-feature.util.js";
2
3
  export * from "./assert-owner-or-admin.util.js";
3
4
  export * from "./assert-user-or-admin.util.js";
4
5
  export * from "./attempt-login.util.js";
@@ -1,4 +1,5 @@
1
1
  export * from "./assert-admin.util.js";
2
+ export * from "./assert-admin-or-feature.util.js";
2
3
  export * from "./assert-owner-or-admin.util.js";
3
4
  export * from "./assert-user-or-admin.util.js";
4
5
  export * from "./attempt-login.util.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.2.28",
3
+ "version": "0.2.30",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {