@temboplus/afloat 0.1.2 → 0.1.3

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.
Files changed (141) hide show
  1. package/esm/_dnt.shims.d.ts +6 -0
  2. package/esm/_dnt.shims.d.ts.map +1 -0
  3. package/esm/_dnt.shims.js +61 -0
  4. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/almost_equals.d.ts.map +1 -1
  5. package/{script/deps/jsr.io/@std/assert/1.0.10 → esm/deps/jsr.io/@std/assert/1.0.11}/array_includes.d.ts.map +1 -1
  6. package/{script/deps/jsr.io/@std/assert/1.0.10 → esm/deps/jsr.io/@std/assert/1.0.11}/assert.d.ts.map +1 -1
  7. package/{script/deps/jsr.io/@std/assert/1.0.10 → esm/deps/jsr.io/@std/assert/1.0.11}/assertion_error.d.ts.map +1 -1
  8. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/equal.d.ts.map +1 -1
  9. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/equals.d.ts.map +1 -1
  10. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/exists.d.ts.map +1 -1
  11. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/fail.d.ts.map +1 -1
  12. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/false.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/greater.d.ts.map +1 -1
  14. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/greater_or_equal.d.ts.map +1 -1
  15. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/instance_of.d.ts.map +1 -1
  16. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/is_error.d.ts.map +1 -1
  17. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/less.d.ts.map +1 -1
  18. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/less_or_equal.d.ts.map +1 -1
  19. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/match.d.ts.map +1 -1
  20. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/mod.d.ts.map +1 -1
  21. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_equals.d.ts.map +1 -1
  22. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_instance_of.d.ts.map +1 -1
  23. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_match.d.ts.map +1 -1
  24. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_strict_equals.d.ts.map +1 -1
  25. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/object_match.d.ts.map +1 -1
  26. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/rejects.d.ts.map +1 -1
  27. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/strict_equals.d.ts.map +1 -1
  28. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/string_includes.d.ts.map +1 -1
  29. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/throws.d.ts.map +1 -1
  30. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/unimplemented.d.ts.map +1 -1
  31. package/esm/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/unreachable.d.ts.map +1 -1
  32. package/esm/src/features/auth/contract.d.ts +14 -25
  33. package/esm/src/features/auth/contract.d.ts.map +1 -1
  34. package/esm/src/features/auth/contract.js +8 -15
  35. package/esm/src/features/auth/identity/contract.d.ts +30 -0
  36. package/esm/src/features/auth/identity/contract.d.ts.map +1 -0
  37. package/esm/src/features/auth/identity/contract.js +18 -0
  38. package/esm/src/features/auth/identity/repository.d.ts +22 -0
  39. package/esm/src/features/auth/identity/repository.d.ts.map +1 -0
  40. package/esm/src/features/auth/identity/repository.js +30 -0
  41. package/esm/src/features/auth/manager.d.ts +50 -27
  42. package/esm/src/features/auth/manager.d.ts.map +1 -1
  43. package/esm/src/features/auth/manager.js +105 -58
  44. package/esm/src/features/auth/profile/contract.d.ts +45 -0
  45. package/esm/src/features/auth/profile/contract.d.ts.map +1 -0
  46. package/esm/src/features/auth/profile/contract.js +16 -0
  47. package/esm/src/features/auth/profile/repository.d.ts +11 -0
  48. package/esm/src/features/auth/profile/repository.d.ts.map +1 -0
  49. package/esm/src/features/auth/profile/repository.js +21 -0
  50. package/esm/src/features/auth/repository.d.ts +1 -0
  51. package/esm/src/features/auth/repository.d.ts.map +1 -1
  52. package/esm/src/features/auth/repository.js +5 -22
  53. package/esm/src/features/auth/storage/client_store.d.ts +30 -0
  54. package/esm/src/features/auth/storage/client_store.d.ts.map +1 -0
  55. package/esm/src/features/auth/storage/client_store.js +46 -0
  56. package/esm/src/features/auth/storage/client_token_handler.d.ts +32 -0
  57. package/esm/src/features/auth/storage/client_token_handler.d.ts.map +1 -0
  58. package/esm/src/features/auth/storage/client_token_handler.js +36 -0
  59. package/esm/src/features/auth/storage/server_store.d.ts +24 -0
  60. package/esm/src/features/auth/storage/server_store.d.ts.map +1 -0
  61. package/esm/src/features/auth/storage/server_store.js +34 -0
  62. package/esm/src/features/auth/storage/server_token_handler.d.ts +35 -0
  63. package/esm/src/features/auth/storage/server_token_handler.d.ts.map +1 -0
  64. package/esm/src/features/auth/storage/server_token_handler.js +68 -0
  65. package/esm/src/features/auth/storage/types.d.ts +42 -0
  66. package/esm/src/features/auth/storage/types.d.ts.map +1 -0
  67. package/esm/src/features/auth/storage/types.js +1 -0
  68. package/esm/src/models/contact/derivatives/contact.d.ts +4 -0
  69. package/esm/src/models/contact/derivatives/contact.d.ts.map +1 -1
  70. package/esm/src/models/contact/derivatives/contact.js +6 -0
  71. package/package.json +4 -4
  72. package/script/_dnt.shims.d.ts +6 -0
  73. package/script/_dnt.shims.d.ts.map +1 -0
  74. package/script/_dnt.shims.js +65 -0
  75. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/almost_equals.d.ts.map +1 -1
  76. package/{esm/deps/jsr.io/@std/assert/1.0.10 → script/deps/jsr.io/@std/assert/1.0.11}/array_includes.d.ts.map +1 -1
  77. package/{esm/deps/jsr.io/@std/assert/1.0.10 → script/deps/jsr.io/@std/assert/1.0.11}/assert.d.ts.map +1 -1
  78. package/{esm/deps/jsr.io/@std/assert/1.0.10 → script/deps/jsr.io/@std/assert/1.0.11}/assertion_error.d.ts.map +1 -1
  79. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/equal.d.ts.map +1 -1
  80. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/equals.d.ts.map +1 -1
  81. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/exists.d.ts.map +1 -1
  82. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/fail.d.ts.map +1 -1
  83. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/false.d.ts.map +1 -1
  84. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/greater.d.ts.map +1 -1
  85. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/greater_or_equal.d.ts.map +1 -1
  86. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/instance_of.d.ts.map +1 -1
  87. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/is_error.d.ts.map +1 -1
  88. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/less.d.ts.map +1 -1
  89. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/less_or_equal.d.ts.map +1 -1
  90. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/match.d.ts.map +1 -1
  91. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/mod.d.ts.map +1 -1
  92. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_equals.d.ts.map +1 -1
  93. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_instance_of.d.ts.map +1 -1
  94. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_match.d.ts.map +1 -1
  95. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/not_strict_equals.d.ts.map +1 -1
  96. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/object_match.d.ts.map +1 -1
  97. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/rejects.d.ts.map +1 -1
  98. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/strict_equals.d.ts.map +1 -1
  99. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/string_includes.d.ts.map +1 -1
  100. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/throws.d.ts.map +1 -1
  101. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/unimplemented.d.ts.map +1 -1
  102. package/script/deps/jsr.io/@std/assert/{1.0.10 → 1.0.11}/unreachable.d.ts.map +1 -1
  103. package/script/src/features/auth/contract.d.ts +14 -25
  104. package/script/src/features/auth/contract.d.ts.map +1 -1
  105. package/script/src/features/auth/contract.js +9 -16
  106. package/script/src/features/auth/identity/contract.d.ts +30 -0
  107. package/script/src/features/auth/identity/contract.d.ts.map +1 -0
  108. package/script/src/features/auth/identity/contract.js +21 -0
  109. package/script/src/features/auth/identity/repository.d.ts +22 -0
  110. package/script/src/features/auth/identity/repository.d.ts.map +1 -0
  111. package/script/src/features/auth/identity/repository.js +34 -0
  112. package/script/src/features/auth/manager.d.ts +50 -27
  113. package/script/src/features/auth/manager.d.ts.map +1 -1
  114. package/script/src/features/auth/manager.js +128 -58
  115. package/script/src/features/auth/profile/contract.d.ts +45 -0
  116. package/script/src/features/auth/profile/contract.d.ts.map +1 -0
  117. package/script/src/features/auth/profile/contract.js +19 -0
  118. package/script/src/features/auth/profile/repository.d.ts +11 -0
  119. package/script/src/features/auth/profile/repository.d.ts.map +1 -0
  120. package/script/src/features/auth/profile/repository.js +25 -0
  121. package/script/src/features/auth/repository.d.ts +1 -0
  122. package/script/src/features/auth/repository.d.ts.map +1 -1
  123. package/script/src/features/auth/repository.js +6 -23
  124. package/script/src/features/auth/storage/client_store.d.ts +30 -0
  125. package/script/src/features/auth/storage/client_store.d.ts.map +1 -0
  126. package/script/src/features/auth/storage/client_store.js +51 -0
  127. package/script/src/features/auth/storage/client_token_handler.d.ts +32 -0
  128. package/script/src/features/auth/storage/client_token_handler.d.ts.map +1 -0
  129. package/script/src/features/auth/storage/client_token_handler.js +40 -0
  130. package/script/src/features/auth/storage/server_store.d.ts +24 -0
  131. package/script/src/features/auth/storage/server_store.d.ts.map +1 -0
  132. package/script/src/features/auth/storage/server_store.js +38 -0
  133. package/script/src/features/auth/storage/server_token_handler.d.ts +35 -0
  134. package/script/src/features/auth/storage/server_token_handler.d.ts.map +1 -0
  135. package/script/src/features/auth/storage/server_token_handler.js +72 -0
  136. package/script/src/features/auth/storage/types.d.ts +42 -0
  137. package/script/src/features/auth/storage/types.d.ts.map +1 -0
  138. package/script/src/features/auth/storage/types.js +2 -0
  139. package/script/src/models/contact/derivatives/contact.d.ts +4 -0
  140. package/script/src/models/contact/derivatives/contact.d.ts.map +1 -1
  141. package/script/src/models/contact/derivatives/contact.js +6 -0
@@ -1,80 +1,147 @@
1
- import { create, useStore, } from "zustand";
2
- import { createJSONStorage, persist } from "zustand/middleware";
1
+ import * as dntShim from "../../../_dnt.shims.js";
3
2
  import { AuthRepository } from "./repository.js";
4
- import { User } from "../../models/index.js";
5
- const AUTH_STORE_SESSION_STORAGE_KEY = "auth-store";
3
+ import { createClientStore, useClientUser, } from "./storage/client_store.js";
4
+ import { ClientTokenHandler } from "./storage/client_token_handler.js";
5
+ import { ServerStore } from "./storage/server_store.js";
6
+ import { ServerTokenHandler } from "./storage/server_token_handler.js";
6
7
  /**
7
- * Singleton class responsible for user authentication and session management.
8
+ * Main authentication class that works in both client and server environments.
9
+ * Provides authentication functionality and user management.
8
10
  */
9
11
  export class AfloatAuth {
10
12
  /**
11
13
  * Private constructor to prevent direct instantiation.
14
+ * @param {AuthStore} store - The auth store implementation to use
15
+ * @param {TokenHandler} tokenHandler - The token handler implementation to use
12
16
  */
13
- constructor() { }
17
+ constructor(store, tokenHandler) {
18
+ /** The auth store implementation */
19
+ Object.defineProperty(this, "store", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ /** The token handler implementation */
26
+ Object.defineProperty(this, "tokenHandler", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ this.store = store;
33
+ this.tokenHandler = tokenHandler;
34
+ }
35
+ /**
36
+ * Initializes AfloatAuth for client-side use.
37
+ * This should be called once at application startup in client environments.
38
+ * @returns {AfloatAuth} The singleton instance configured for client-side
39
+ */
40
+ static initializeClient() {
41
+ if (!this._instance) {
42
+ this._instance = new AfloatAuth(createClientStore(), ClientTokenHandler.instance);
43
+ }
44
+ return this._instance;
45
+ }
46
+ /**
47
+ * Creates a new instance of AfloatAuth configured for server-side use.
48
+ * Initializes the user by fetching necessary data using the provided token.
49
+ * @param {string} token - Authentication token
50
+ * @returns {Promise<AfloatAuth>} A new instance configured for server-side
51
+ * @throws {Error} If token is invalid or required data cannot be fetched
52
+ */
53
+ static async initializeServer(token) {
54
+ if (!token) {
55
+ throw new Error("Token is required for server initialization");
56
+ }
57
+ const tokenHandler = new ServerTokenHandler(token);
58
+ const store = new ServerStore();
59
+ try {
60
+ // Fetch and construct user data
61
+ const user = await tokenHandler.constructUser();
62
+ store.setUser(user);
63
+ // Create and initialize auth instance
64
+ return new AfloatAuth(store, tokenHandler);
65
+ }
66
+ catch (error) {
67
+ if (error instanceof Error) {
68
+ throw new Error(`Failed to initialize server auth: ${error.message}`);
69
+ }
70
+ throw new Error("Failed to initialize server auth");
71
+ }
72
+ }
14
73
  /**
15
74
  * Gets the singleton instance of AfloatAuth.
16
- * @returns {AfloatAuth} The singleton instance.
75
+ * @throws {Error} If AfloatAuth hasn't been initialized
76
+ * @returns {AfloatAuth} The singleton instance
17
77
  */
18
78
  static get instance() {
19
- return this._instance || (this._instance = new this());
79
+ if (!this._instance) {
80
+ throw new Error("AfloatAuth not initialized. Call initializeClient() or initializeServer() first");
81
+ }
82
+ return this._instance;
20
83
  }
21
84
  /**
22
- * Fetches the authentication repository.
85
+ * Gets the authentication repository instance.
23
86
  * @private
24
- * @returns {AuthRepository} The repository instance for authentication-related API calls.
87
+ * @returns {AuthRepository} The repository for auth operations
25
88
  */
26
89
  get repo() {
27
90
  return new AuthRepository();
28
91
  }
29
92
  /**
30
- * Retrieves the current user's authentication token.
31
- * @returns {string | undefined} The token of the currently authenticated user.
93
+ * Gets the current authentication token.
94
+ * @returns {string | undefined} The current token or undefined if not authenticated
32
95
  */
33
96
  getUserToken() {
34
- return this.currentUser?.token;
97
+ return this.tokenHandler.getUserToken();
35
98
  }
36
99
  /**
37
- * Gets the currently logged-in user.
38
- * @returns {User | undefined} The current user object, or `undefined` if no user is logged in.
100
+ * Gets the currently authenticated user.
101
+ * @returns {User | undefined} The current user or undefined if not authenticated
39
102
  */
40
103
  get currentUser() {
41
- return store.getState().getUser();
104
+ return this.store.getUser();
42
105
  }
43
106
  /**
44
- * React hook that retrieves the currently authenticated user from the global store.
45
- *
46
- * @returns {User | undefined} The current user object if authenticated, or `undefined` if no user is logged in.
107
+ * React hook for accessing the current user in client-side code.
108
+ * @throws {Error} If called in a server environment
109
+ * @returns {User | undefined} The current user or undefined if not authenticated
47
110
  */
48
111
  useCurrentUser() {
49
- return useStore(store).getUser();
112
+ if (typeof dntShim.dntGlobalThis === "undefined") {
113
+ throw new Error("useCurrentUser can only be called in browser environment");
114
+ }
115
+ return useClientUser();
50
116
  }
51
117
  /**
52
- * Checks if the current user has the specified permission.
53
- * @param {Permission} perm - The permission to check.
54
- * @returns {boolean} `true` if the user has the permission, otherwise `false`.
118
+ * Checks if the current user has a specific permission.
119
+ * @param {Permission} perm - The permission to check
120
+ * @returns {boolean} True if the user has the permission, false otherwise
55
121
  */
56
122
  checkPermission(perm) {
57
123
  return this.currentUser?.can(perm) ?? false;
58
124
  }
59
125
  /**
60
- * Logs in a user with the provided email and password.
61
- * @param {string} email - The user's email address.
62
- * @param {string} password - The user's password.
63
- * @returns {Promise<User>} A promise that resolves to the authenticated user.
64
- * @throws Will throw an error if authentication fails.
126
+ * Authenticates a user with email and password.
127
+ * @param {string} email - The user's email
128
+ * @param {string} password - The user's password
129
+ * @returns {Promise<User>} Promise resolving to the authenticated user
130
+ * @throws Will throw an error if authentication fails
65
131
  */
66
132
  async logIn(email, password) {
67
133
  const user = await this.repo.logIn(email, password);
68
134
  this.clearSavedData();
69
- store.getState().setUser(user);
135
+ this.store.setUser(user);
136
+ this.tokenHandler.setUserToken(user.token);
70
137
  return user;
71
138
  }
72
139
  /**
73
- * Resets the user's password.
74
- * @param {string} current - The current password.
75
- * @param {string} updated - The new password.
76
- * @returns {Promise<boolean>} A promise that resolves to `true` if the operation is successful.
77
- * @throws Will throw an error if the reset fails.
140
+ * Updates the user's password.
141
+ * @param {string} current - The current password
142
+ * @param {string} updated - The new password
143
+ * @returns {Promise<boolean>} Promise resolving to true if successful
144
+ * @throws Will throw an error if the password update fails
78
145
  */
79
146
  async resetPassword(current, updated) {
80
147
  await this.repo.updatePassword(current, updated);
@@ -82,37 +149,17 @@ export class AfloatAuth {
82
149
  return true;
83
150
  }
84
151
  /**
85
- * Logs out the current user and clears session data.
152
+ * Logs out the current user.
86
153
  */
87
154
  logOut() {
88
155
  this.clearSavedData();
89
156
  }
90
157
  /**
91
- * Clears saved authentication data from the store and session storage.
158
+ * Clears all authentication data.
92
159
  * @private
93
160
  */
94
161
  clearSavedData() {
95
- store.getState().refresh();
96
- sessionStorage.removeItem(AUTH_STORE_SESSION_STORAGE_KEY);
162
+ this.store.refresh();
163
+ this.tokenHandler.clearToken();
97
164
  }
98
165
  }
99
- const initialState = { user: undefined };
100
- const store = create(persist((set, get) => ({
101
- ...initialState,
102
- getUser: () => {
103
- try {
104
- const jsonUser = get().user;
105
- if (jsonUser)
106
- return User.fromJSON(jsonUser);
107
- }
108
- catch (_) {
109
- console.log(_);
110
- }
111
- return undefined;
112
- },
113
- setUser: (user) => set({ user: user.toJSON() }),
114
- refresh: () => set(initialState),
115
- }), {
116
- name: AUTH_STORE_SESSION_STORAGE_KEY,
117
- storage: createJSONStorage(() => sessionStorage),
118
- }));
@@ -0,0 +1,45 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * Profile API contract
4
+ */
5
+ export declare const profileContract: {
6
+ getCurrentProfile: {
7
+ method: "GET";
8
+ path: "/me";
9
+ headers: z.ZodObject<{
10
+ token: z.ZodString;
11
+ }, "strip", z.ZodTypeAny, {
12
+ token: string;
13
+ }, {
14
+ token: string;
15
+ }>;
16
+ responses: {
17
+ 200: z.ZodObject<{
18
+ id: z.ZodString;
19
+ firstName: z.ZodString;
20
+ lastName: z.ZodString;
21
+ displayName: z.ZodString;
22
+ phone: z.ZodString;
23
+ accountNo: z.ZodString;
24
+ email: z.ZodString;
25
+ }, z.UnknownKeysParam, z.ZodTypeAny, {
26
+ id: string;
27
+ firstName: string;
28
+ lastName: string;
29
+ displayName: string;
30
+ phone: string;
31
+ accountNo: string;
32
+ email: string;
33
+ }, {
34
+ id: string;
35
+ firstName: string;
36
+ lastName: string;
37
+ displayName: string;
38
+ phone: string;
39
+ accountNo: string;
40
+ email: string;
41
+ }>;
42
+ };
43
+ };
44
+ };
45
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/profile/contract.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAS1B,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { initContract } from "@ts-rest/core";
2
+ import { profileSchema } from "../../../models/index.js";
3
+ import { z } from "zod";
4
+ /**
5
+ * Profile API contract
6
+ */
7
+ export const profileContract = initContract().router({
8
+ getCurrentProfile: {
9
+ method: "GET",
10
+ path: "/me",
11
+ headers: z.object({ token: z.string() }),
12
+ responses: {
13
+ 200: profileSchema,
14
+ },
15
+ },
16
+ });
@@ -0,0 +1,11 @@
1
+ import type { Profile } from "../../../models/index.js";
2
+ import { BaseRepository } from "../../../shared/base_repository.js";
3
+ import { profileContract } from "./contract.js";
4
+ export declare class ProfileRepository extends BaseRepository<typeof profileContract> {
5
+ /**
6
+ * Initializes an instance of ProfileRepository.
7
+ */
8
+ constructor();
9
+ getCurrentProfile(token: string): Promise<Profile>;
10
+ }
11
+ //# sourceMappingURL=repository.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/profile/repository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,qBAAa,iBAAkB,SAAQ,cAAc,CAAC,OAAO,eAAe,CAAC;IAC3E;;OAEG;;IAKG,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAUzD"}
@@ -0,0 +1,21 @@
1
+ import { BaseRepository } from "../../../shared/base_repository.js";
2
+ import { APIError } from "../../../errors/api_error.js";
3
+ import { profileContract } from "./contract.js";
4
+ export class ProfileRepository extends BaseRepository {
5
+ /**
6
+ * Initializes an instance of ProfileRepository.
7
+ */
8
+ constructor() {
9
+ super("profile", profileContract);
10
+ }
11
+ async getCurrentProfile(token) {
12
+ const headers = { token };
13
+ const result = await this.client.getCurrentProfile({ headers });
14
+ if (result.status === 200)
15
+ return result.body;
16
+ throw new APIError({
17
+ message: "An error occurred while trying to get the current profile",
18
+ statusCode: 502,
19
+ });
20
+ }
21
+ }
@@ -26,5 +26,6 @@ export declare class AuthRepository extends BaseRepository<typeof authContract>
26
26
  * @throws {APIError} If the current password is invalid or another error occurs during the update process.
27
27
  */
28
28
  updatePassword(currentPassword: string, newPassword: string): Promise<boolean>;
29
+ getAccessList(token: string): Promise<string[]>;
29
30
  }
30
31
  //# sourceMappingURL=repository.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../../src/src/features/auth/repository.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,YAAY,EAAoB,MAAM,eAAe,CAAC;AAO/D;;;GAGG;AACH,qBAAa,cAAe,SAAQ,cAAc,CAAC,OAAO,YAAY,CAAC;IACrE;;OAEG;;IAKH;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB3D;;;;;;OAMG;IACG,cAAc,CAClB,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,OAAO,CAAC;CAiBpB"}
1
+ {"version":3,"file":"repository.d.ts","sourceRoot":"","sources":["../../../../src/src/features/auth/repository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAI7C;;;GAGG;AACH,qBAAa,cAAe,SAAQ,cAAc,CAAC,OAAO,YAAY,CAAC;IACrE;;OAEG;;IAKH;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB3D;;;;;;OAMG;IACG,cAAc,CAClB,eAAe,EAAE,MAAM,EACvB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,OAAO,CAAC;IAkBb,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAStD"}
@@ -1,7 +1,8 @@
1
1
  import { User } from "../../models/index.js";
2
2
  import { BaseRepository } from "../../shared/base_repository.js";
3
- import { authContract, identityContract } from "./contract.js";
3
+ import { authContract } from "./contract.js";
4
4
  import { APIError } from "../../errors/api_error.js";
5
+ import { LoginRepository } from "./identity/repository.js";
5
6
  /**
6
7
  * Class representing the AuthRepository.
7
8
  * Provides methods to handle authentication-related operations, such as login and password updates.
@@ -64,30 +65,12 @@ export class AuthRepository extends BaseRepository {
64
65
  statusCode: 502,
65
66
  });
66
67
  }
67
- }
68
- /**
69
- * Class representing the LoginRepository.
70
- * Provides methods to retrieve user identity-related information.
71
- */
72
- class LoginRepository extends BaseRepository {
73
- /**
74
- * Initializes an instance of LoginRepository.
75
- */
76
- constructor() {
77
- super("login", identityContract);
78
- }
79
- /**
80
- * Retrieves the user's login credentials.
81
- * @returns A promise that resolves to the user's login credentials on success.
82
- * @throws {APIError} If an error occurs while retrieving the credentials.
83
- */
84
- async getIdentity(token) {
85
- const headers = { token };
86
- const result = await this.client.getUserCredentials({ headers });
68
+ async getAccessList(token) {
69
+ const result = await this.client.access({ headers: { token: token } });
87
70
  if (result.status === 200)
88
71
  return result.body;
89
72
  throw new APIError({
90
- message: "An error occurred while trying to get login credentials",
73
+ message: "An error occurred while trying to get access list",
91
74
  statusCode: 502,
92
75
  });
93
76
  }
@@ -0,0 +1,30 @@
1
+ import { User } from "../../../models/user/index.js";
2
+ import { type StoreApi, type UseBoundStore } from "zustand";
3
+ import type { AuthStore } from "./types.js";
4
+ /** Type definition for the store's state */
5
+ type State = {
6
+ user: string | undefined;
7
+ };
8
+ /** Type definition for the store's actions */
9
+ interface Actions {
10
+ setUser: (user: User) => void;
11
+ getUser: () => User | undefined;
12
+ refresh: () => void;
13
+ }
14
+ /**
15
+ * Creates and exports the Zustand store directly for reactive hooks
16
+ * @internal This should only be used by AfloatAuth
17
+ */
18
+ export declare const clientStore: UseBoundStore<StoreApi<State & Actions>>;
19
+ /**
20
+ * Creates a client-side authentication store using Zustand.
21
+ * @returns {AuthStore} An implementation of AuthStore for client-side use
22
+ */
23
+ export declare const createClientStore: () => AuthStore;
24
+ /**
25
+ * React hook to access the current user with reactive updates.
26
+ * @returns {User | undefined} The current user or undefined if not authenticated
27
+ */
28
+ export declare const useClientUser: () => User | undefined;
29
+ export {};
30
+ //# sourceMappingURL=client_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client_store.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/storage/client_store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AACrD,OAAO,EAAU,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAEpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAK5C,4CAA4C;AAC5C,KAAK,KAAK,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAE1C,8CAA8C;AAC9C,UAAU,OAAO;IACf,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IAC9B,OAAO,EAAE,MAAM,IAAI,GAAG,SAAS,CAAC;IAChC,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,aAAa,CAAC,QAAQ,CAAC,KAAK,GAAG,OAAO,CAAC,CA2BhE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QAAO,SAMpC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,aAAa,QAAO,IAAI,GAAG,SAEvC,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { User } from "../../../models/user/index.js";
2
+ import { create } from "zustand";
3
+ import { createJSONStorage, persist } from "zustand/middleware";
4
+ /** Key used for storing auth data in session storage */
5
+ const AUTH_STORE_SESSION_STORAGE_KEY = "auth-store";
6
+ /**
7
+ * Creates and exports the Zustand store directly for reactive hooks
8
+ * @internal This should only be used by AfloatAuth
9
+ */
10
+ export const clientStore = create(persist((set, get) => ({
11
+ user: undefined,
12
+ getUser: () => {
13
+ try {
14
+ const jsonUser = get().user;
15
+ if (jsonUser)
16
+ return User.fromJSON(jsonUser);
17
+ }
18
+ catch (_) {
19
+ console.log(_);
20
+ }
21
+ return undefined;
22
+ },
23
+ setUser: (user) => set({ user: user.toJSON() }),
24
+ refresh: () => set({ user: undefined }),
25
+ }), {
26
+ name: AUTH_STORE_SESSION_STORAGE_KEY,
27
+ storage: createJSONStorage(() => sessionStorage),
28
+ }));
29
+ /**
30
+ * Creates a client-side authentication store using Zustand.
31
+ * @returns {AuthStore} An implementation of AuthStore for client-side use
32
+ */
33
+ export const createClientStore = () => {
34
+ return {
35
+ getUser: () => clientStore.getState().getUser(),
36
+ setUser: (user) => clientStore.getState().setUser(user),
37
+ refresh: () => clientStore.getState().refresh(),
38
+ };
39
+ };
40
+ /**
41
+ * React hook to access the current user with reactive updates.
42
+ * @returns {User | undefined} The current user or undefined if not authenticated
43
+ */
44
+ export const useClientUser = () => {
45
+ return clientStore((state) => state.getUser());
46
+ };
@@ -0,0 +1,32 @@
1
+ import type { TokenHandler } from "./types.js";
2
+ /**
3
+ * Client-side implementation of TokenHandler.
4
+ * Manages tokens using sessionStorage.
5
+ * @implements {TokenHandler}
6
+ */
7
+ export declare class ClientTokenHandler implements TokenHandler {
8
+ /** Singleton instance */
9
+ private static _instance;
10
+ /** Private constructor to enforce singleton pattern */
11
+ private constructor();
12
+ /**
13
+ * Gets the singleton instance of ClientTokenHandler.
14
+ * @returns {ClientTokenHandler} The singleton instance
15
+ */
16
+ static get instance(): ClientTokenHandler;
17
+ /**
18
+ * Retrieves the token from sessionStorage.
19
+ * @returns {string | undefined} The stored token or undefined if not present
20
+ */
21
+ getUserToken(): string | undefined;
22
+ /**
23
+ * Stores the token in sessionStorage.
24
+ * @param {string} token - The token to store
25
+ */
26
+ setUserToken(token: string): void;
27
+ /**
28
+ * Removes the token from sessionStorage.
29
+ */
30
+ clearToken(): void;
31
+ }
32
+ //# sourceMappingURL=client_token_handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client_token_handler.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/storage/client_token_handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;GAIG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IACrD,yBAAyB;IACzB,OAAO,CAAC,MAAM,CAAC,SAAS,CAAqB;IAE7C,uDAAuD;IACvD,OAAO;IAEP;;;OAGG;IACH,MAAM,KAAK,QAAQ,IAAI,kBAAkB,CAExC;IAED;;;OAGG;IACH,YAAY,IAAI,MAAM,GAAG,SAAS;IAIlC;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAIjC;;OAEG;IACH,UAAU,IAAI,IAAI;CAGnB"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Client-side implementation of TokenHandler.
3
+ * Manages tokens using sessionStorage.
4
+ * @implements {TokenHandler}
5
+ */
6
+ export class ClientTokenHandler {
7
+ /** Private constructor to enforce singleton pattern */
8
+ constructor() { }
9
+ /**
10
+ * Gets the singleton instance of ClientTokenHandler.
11
+ * @returns {ClientTokenHandler} The singleton instance
12
+ */
13
+ static get instance() {
14
+ return this._instance || (this._instance = new this());
15
+ }
16
+ /**
17
+ * Retrieves the token from sessionStorage.
18
+ * @returns {string | undefined} The stored token or undefined if not present
19
+ */
20
+ getUserToken() {
21
+ return sessionStorage.getItem("auth_token") || undefined;
22
+ }
23
+ /**
24
+ * Stores the token in sessionStorage.
25
+ * @param {string} token - The token to store
26
+ */
27
+ setUserToken(token) {
28
+ sessionStorage.setItem("auth_token", token);
29
+ }
30
+ /**
31
+ * Removes the token from sessionStorage.
32
+ */
33
+ clearToken() {
34
+ sessionStorage.removeItem("auth_token");
35
+ }
36
+ }
@@ -0,0 +1,24 @@
1
+ import type { User } from "../../../models/user/index.js";
2
+ import type { AuthStore } from "./types.js";
3
+ /**
4
+ * Server-side implementation of AuthStore.
5
+ * Maintains user state in memory for the duration of a request.
6
+ */
7
+ export declare class ServerStore implements AuthStore {
8
+ private user?;
9
+ /**
10
+ * Retrieves the current user from memory.
11
+ * @returns {User | undefined} The current user or undefined if not set
12
+ */
13
+ getUser(): User | undefined;
14
+ /**
15
+ * Sets the current user in memory.
16
+ * @param {User} user - The user to store
17
+ */
18
+ setUser(user: User): void;
19
+ /**
20
+ * Clears the stored user from memory.
21
+ */
22
+ refresh(): void;
23
+ }
24
+ //# sourceMappingURL=server_store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server_store.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/storage/server_store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,+BAA+B,CAAC;AAC1D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;;GAGG;AACH,qBAAa,WAAY,YAAW,SAAS;IAC3C,OAAO,CAAC,IAAI,CAAC,CAAO;IAEpB;;;OAGG;IACH,OAAO,IAAI,IAAI,GAAG,SAAS;IAI3B;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAIzB;;OAEG;IACH,OAAO,IAAI,IAAI;CAGhB"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Server-side implementation of AuthStore.
3
+ * Maintains user state in memory for the duration of a request.
4
+ */
5
+ export class ServerStore {
6
+ constructor() {
7
+ Object.defineProperty(this, "user", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ }
14
+ /**
15
+ * Retrieves the current user from memory.
16
+ * @returns {User | undefined} The current user or undefined if not set
17
+ */
18
+ getUser() {
19
+ return this.user;
20
+ }
21
+ /**
22
+ * Sets the current user in memory.
23
+ * @param {User} user - The user to store
24
+ */
25
+ setUser(user) {
26
+ this.user = user;
27
+ }
28
+ /**
29
+ * Clears the stored user from memory.
30
+ */
31
+ refresh() {
32
+ this.user = undefined;
33
+ }
34
+ }
@@ -0,0 +1,35 @@
1
+ import type { TokenHandler } from "./types.js";
2
+ import { User } from "../../../models/index.js";
3
+ /**
4
+ * Server-side implementation of TokenHandler.
5
+ * Manages tokens in memory for the duration of a request.
6
+ * @implements {TokenHandler}
7
+ */
8
+ export declare class ServerTokenHandler implements TokenHandler {
9
+ private token?;
10
+ /**
11
+ * Creates a new instance of ServerTokenHandler.
12
+ * @param {string} [token] - Optional initial token value
13
+ */
14
+ constructor(token?: string | undefined);
15
+ /**
16
+ * Returns the stored token.
17
+ * @returns {string | undefined} The current token or undefined if not set
18
+ */
19
+ getUserToken(): string | undefined;
20
+ /**
21
+ * Stores the provided token in memory.
22
+ * @param {string} token - The token to store
23
+ */
24
+ setUserToken(token: string): void;
25
+ /**
26
+ * Clears the stored token from memory.
27
+ */
28
+ clearToken(): void;
29
+ /**
30
+ * Fetches and constructs the full user data
31
+ * @returns {Promise<User>}
32
+ */
33
+ constructUser(): Promise<User>;
34
+ }
35
+ //# sourceMappingURL=server_token_handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server_token_handler.d.ts","sourceRoot":"","sources":["../../../../../src/src/features/auth/storage/server_token_handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAKhD;;;;GAIG;AACH,qBAAa,kBAAmB,YAAW,YAAY;IAKzC,OAAO,CAAC,KAAK,CAAC;IAJ1B;;;OAGG;gBACiB,KAAK,CAAC,EAAE,MAAM,YAAA;IAElC;;;OAGG;IACH,YAAY,IAAI,MAAM,GAAG,SAAS;IAIlC;;;OAGG;IACH,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAIjC;;OAEG;IACH,UAAU,IAAI,IAAI;IAIlB;;;OAGG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;CAyBrC"}