@aeriajs/builtins 0.0.135 → 0.0.137

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,9 +1,9 @@
1
1
  import type { RouteContext, SchemaWithId, TokenRecipient } from '@aeriajs/types';
2
- import type { ObjectId } from '@aeriajs/core';
3
2
  import type { description } from './collections/user/description.js';
3
+ export type TokenableUser = Pick<SchemaWithId<typeof description>, '_id' | 'name' | 'email' | 'roles' | 'active'>;
4
4
  export type SuccessfulAuthentication = {
5
- user: Pick<SchemaWithId<typeof description>, 'name' | 'email' | 'roles' | 'active'> & {
6
- _id: ObjectId | null;
5
+ user: TokenableUser & {
6
+ _id: null;
7
7
  };
8
8
  token: TokenRecipient;
9
9
  };
@@ -11,7 +11,7 @@ export declare enum AuthenticationError {
11
11
  InvalidCredentials = "INVALID_CREDENTIALS",
12
12
  InactiveUser = "INACTIVE_USER"
13
13
  }
14
- export declare const successfulAuthentication: (userId: ObjectId, context: RouteContext) => Promise<SuccessfulAuthentication>;
14
+ export declare const successfulAuthentication: <TUser extends TokenableUser>(user: TUser, context: RouteContext) => Promise<SuccessfulAuthentication>;
15
15
  export declare const defaultSuccessfulAuthentication: () => Promise<{
16
16
  user: {
17
17
  _id: null;
@@ -7,16 +7,7 @@ var AuthenticationError;
7
7
  AuthenticationError["InvalidCredentials"] = "INVALID_CREDENTIALS";
8
8
  AuthenticationError["InactiveUser"] = "INACTIVE_USER";
9
9
  })(AuthenticationError || (exports.AuthenticationError = AuthenticationError = {}));
10
- const successfulAuthentication = async (userId, context) => {
11
- const { error, result: user } = await context.collections.user.functions.get({
12
- filters: {
13
- _id: userId,
14
- },
15
- populate: ['picture_file'],
16
- });
17
- if (error) {
18
- throw new Error();
19
- }
10
+ const successfulAuthentication = async (user, context) => {
20
11
  const tokenContent = {
21
12
  sub: user._id,
22
13
  roles: user.roles,
@@ -46,7 +37,7 @@ const successfulAuthentication = async (userId, context) => {
46
37
  }
47
38
  const token = await (0, core_1.signToken)(tokenContent);
48
39
  return {
49
- user: user,
40
+ user,
50
41
  token: {
51
42
  type: 'bearer',
52
43
  content: token,
@@ -5,16 +5,7 @@ export var AuthenticationError = /* @__PURE__ */ ((AuthenticationError2) => {
5
5
  AuthenticationError2["InactiveUser"] = "INACTIVE_USER";
6
6
  return AuthenticationError2;
7
7
  })(AuthenticationError || {});
8
- export const successfulAuthentication = async (userId, context) => {
9
- const { error, result: user } = await context.collections.user.functions.get({
10
- filters: {
11
- _id: userId
12
- },
13
- populate: ["picture_file"]
14
- });
15
- if (error) {
16
- throw new Error();
17
- }
8
+ export const successfulAuthentication = async (user, context) => {
18
9
  const tokenContent = {
19
10
  sub: user._id,
20
11
  roles: user.roles,
@@ -4,6 +4,7 @@ exports.authenticate = void 0;
4
4
  const types_1 = require("@aeriajs/types");
5
5
  const bcrypt_1 = require("bcrypt");
6
6
  const core_1 = require("@aeriajs/core");
7
+ const common_1 = require("@aeriajs/common");
7
8
  const authentication_js_1 = require("../../authentication.js");
8
9
  const authenticate = async (props, context) => {
9
10
  if ('revalidate' in props) {
@@ -16,8 +17,17 @@ const authenticate = async (props, context) => {
16
17
  const decodedToken = token
17
18
  ? await (0, core_1.decodeToken)(token.content)
18
19
  : context.token;
20
+ const { error, result: user } = await context.collections.user.functions.get({
21
+ filters: {
22
+ _id: decodedToken.sub,
23
+ },
24
+ populate: ['picture_file'],
25
+ });
26
+ if (error) {
27
+ throw new Error();
28
+ }
19
29
  return types_1.Result.result(decodedToken.sub
20
- ? await (0, authentication_js_1.successfulAuthentication)(decodedToken.sub, context)
30
+ ? await (0, authentication_js_1.successfulAuthentication)(user, context)
21
31
  : await (0, authentication_js_1.defaultSuccessfulAuthentication)());
22
32
  }
23
33
  if (typeof props.email !== 'string') {
@@ -34,7 +44,6 @@ const authenticate = async (props, context) => {
34
44
  email: props.email,
35
45
  }, {
36
46
  projection: {
37
- email: 1,
38
47
  password: 1,
39
48
  active: 1,
40
49
  },
@@ -49,6 +58,12 @@ const authenticate = async (props, context) => {
49
58
  code: authentication_js_1.AuthenticationError.InactiveUser,
50
59
  });
51
60
  }
52
- return types_1.Result.result(await (0, authentication_js_1.successfulAuthentication)(user._id, context));
61
+ const completeUser = (0, common_1.throwIfError)(await (0, core_1.get)({
62
+ filters: {
63
+ _id: user._id,
64
+ },
65
+ populate: ['picture_file'],
66
+ }, context));
67
+ return types_1.Result.result(await (0, authentication_js_1.successfulAuthentication)(completeUser, context));
53
68
  };
54
69
  exports.authenticate = authenticate;
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  import { Result, HTTPStatus, ACError } from "@aeriajs/types";
3
3
  import { compare as bcryptCompare } from "bcrypt";
4
- import { decodeToken } from "@aeriajs/core";
4
+ import { decodeToken, get } from "@aeriajs/core";
5
+ import { throwIfError } from "@aeriajs/common";
5
6
  import { successfulAuthentication, defaultSuccessfulAuthentication, AuthenticationError } from "../../authentication.mjs";
6
7
  export const authenticate = async (props, context) => {
7
8
  if ("revalidate" in props) {
@@ -12,7 +13,16 @@ export const authenticate = async (props, context) => {
12
13
  });
13
14
  }
14
15
  const decodedToken = token ? await decodeToken(token.content) : context.token;
15
- return Result.result(decodedToken.sub ? await successfulAuthentication(decodedToken.sub, context) : await defaultSuccessfulAuthentication());
16
+ const { error, result: user2 } = await context.collections.user.functions.get({
17
+ filters: {
18
+ _id: decodedToken.sub
19
+ },
20
+ populate: ["picture_file"]
21
+ });
22
+ if (error) {
23
+ throw new Error();
24
+ }
25
+ return Result.result(decodedToken.sub ? await successfulAuthentication(user2, context) : await defaultSuccessfulAuthentication());
16
26
  }
17
27
  if (typeof props.email !== "string") {
18
28
  return context.error(HTTPStatus.Unauthorized, {
@@ -30,7 +40,6 @@ export const authenticate = async (props, context) => {
30
40
  },
31
41
  {
32
42
  projection: {
33
- email: 1,
34
43
  password: 1,
35
44
  active: 1
36
45
  }
@@ -46,5 +55,11 @@ export const authenticate = async (props, context) => {
46
55
  code: AuthenticationError.InactiveUser
47
56
  });
48
57
  }
49
- return Result.result(await successfulAuthentication(user._id, context));
58
+ const completeUser = throwIfError(await get({
59
+ filters: {
60
+ _id: user._id
61
+ },
62
+ populate: ["picture_file"]
63
+ }, context));
64
+ return Result.result(await successfulAuthentication(completeUser, context));
50
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/builtins",
3
- "version": "0.0.135",
3
+ "version": "0.0.137",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -45,11 +45,11 @@
45
45
  "mongodb": "^6.5.0"
46
46
  },
47
47
  "peerDependencies": {
48
- "@aeriajs/core": "^0.0.135",
49
- "@aeriajs/common": "^0.0.85",
50
- "@aeriajs/entrypoint": "^0.0.87",
51
- "@aeriajs/types": "^0.0.73",
52
- "@aeriajs/validation": "^0.0.88"
48
+ "@aeriajs/core": "^0.0.137",
49
+ "@aeriajs/common": "^0.0.86",
50
+ "@aeriajs/entrypoint": "^0.0.88",
51
+ "@aeriajs/types": "^0.0.74",
52
+ "@aeriajs/validation": "^0.0.89"
53
53
  },
54
54
  "scripts": {
55
55
  "test": "echo skipping",