@aeriajs/builtins 0.0.135 → 0.0.136

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,
@@ -16,8 +16,17 @@ const authenticate = async (props, context) => {
16
16
  const decodedToken = token
17
17
  ? await (0, core_1.decodeToken)(token.content)
18
18
  : context.token;
19
+ const { error, result: user } = await context.collections.user.functions.get({
20
+ filters: {
21
+ _id: decodedToken.sub,
22
+ },
23
+ populate: ['picture_file'],
24
+ });
25
+ if (error) {
26
+ throw new Error();
27
+ }
19
28
  return types_1.Result.result(decodedToken.sub
20
- ? await (0, authentication_js_1.successfulAuthentication)(decodedToken.sub, context)
29
+ ? await (0, authentication_js_1.successfulAuthentication)(user, context)
21
30
  : await (0, authentication_js_1.defaultSuccessfulAuthentication)());
22
31
  }
23
32
  if (typeof props.email !== 'string') {
@@ -49,6 +58,6 @@ 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
+ return types_1.Result.result(await (0, authentication_js_1.successfulAuthentication)(user, context));
53
62
  };
54
63
  exports.authenticate = authenticate;
@@ -12,7 +12,16 @@ export const authenticate = async (props, context) => {
12
12
  });
13
13
  }
14
14
  const decodedToken = token ? await decodeToken(token.content) : context.token;
15
- return Result.result(decodedToken.sub ? await successfulAuthentication(decodedToken.sub, context) : await defaultSuccessfulAuthentication());
15
+ const { error, result: user2 } = await context.collections.user.functions.get({
16
+ filters: {
17
+ _id: decodedToken.sub
18
+ },
19
+ populate: ["picture_file"]
20
+ });
21
+ if (error) {
22
+ throw new Error();
23
+ }
24
+ return Result.result(decodedToken.sub ? await successfulAuthentication(user2, context) : await defaultSuccessfulAuthentication());
16
25
  }
17
26
  if (typeof props.email !== "string") {
18
27
  return context.error(HTTPStatus.Unauthorized, {
@@ -46,5 +55,5 @@ 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
+ return Result.result(await successfulAuthentication(user, context));
50
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/builtins",
3
- "version": "0.0.135",
3
+ "version": "0.0.136",
4
4
  "description": "## Installation",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -45,7 +45,7 @@
45
45
  "mongodb": "^6.5.0"
46
46
  },
47
47
  "peerDependencies": {
48
- "@aeriajs/core": "^0.0.135",
48
+ "@aeriajs/core": "^0.0.136",
49
49
  "@aeriajs/common": "^0.0.85",
50
50
  "@aeriajs/entrypoint": "^0.0.87",
51
51
  "@aeriajs/types": "^0.0.73",