@alwatr/nitrobase-user-management 7.10.1 → 9.1.0
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.
- package/dist/logger.d.ts.map +1 -1
- package/dist/main.js +5 -0
- package/dist/main.js.map +11 -0
- package/package.json +53 -49
- package/src/logger.ts +4 -0
- package/src/main.ts +1 -0
- package/src/user-management.ts +248 -0
- package/CHANGELOG.md +0 -105
- package/dist/main.cjs +0 -4
- package/dist/main.cjs.map +0 -7
- package/dist/main.mjs +0 -4
- package/dist/main.mjs.map +0 -7
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,MAAM,uCAAiD,CAAC"}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* 📦 @alwatr/nitrobase-user-management v9.1.0 */
|
|
2
|
+
import{dirname as O,resolve as A}from"node:path";import{createLogger as Q}from"@alwatr/logger";import{Region as J}from"@alwatr/nitrobase-types";import{existsSync as V,makeEmptyFile as K}from"@alwatr/node-fs";import{createLogger as L}from"@alwatr/logger";var H=L("@alwatr/nitrobase-user-management");class B{nitrobase_;static nitrobaseIds={userList:{name:"user-list",region:J.Managers,schemaVer:1},userInfo:{name:"user-info",region:J.PerUser}};static async initialize(_){H.logMethod?.("NitrobaseUserManagement.initialize"),_.newCollection(this.nitrobaseIds.userList)}logger_=Q("user-management");userListCollection_;constructor(_){this.nitrobase_=_;this.logger_.logMethod?.("constructor"),this.userListCollection_=this.nitrobase_.openCollection(B.nitrobaseIds.userList)}async newUser(_){this.logger_.logMethodArgs?.("newUser",_),(await this.userListCollection_).addItem(_.userId,{}),this.newUserInfoDocument_(_.userId,_.data),await this.makeTokenFile_(_.userId,_.userToken,_.isManager)}async hasUser(_){let q=(await this.userListCollection_).hasItem(_);return this.logger_.logMethodFull?.("hasUser",{userId:_},q),q}async getUserData(_){return this.logger_.logMethodArgs?.("getUserData",_),(await this.openUserInfoDocument_(_)).getData()}async getUserMeta(_){return this.logger_.logMethodArgs?.("getUserMeta",_),(await this.openUserInfoDocument_(_)).getStoreMeta()}save(_){this.logger_.logMethodArgs?.("save",_),this.openUserInfoDocument_(_).then((q)=>{q.save()})}saveImmediate(_){this.logger_.logMethodArgs?.("saveImmediate",_),this.openUserInfoDocument_(_).then((q)=>{q.saveImmediate()})}async userIds(){return(await this.userListCollection_).ids()}async verifyUserToken(_,q){let z=V(await this.getUserTokenFilePath_(_,q));return this.logger_.logMethodFull?.("verifyUserToken",{userId:_,token:q.slice(0,12)+"..."},z),z}async getUserDirectory(_){let q=await this.openUserInfoDocument_(_),z=O(A(this.nitrobase_.config.rootPath,q.path));return this.logger_.logMethodFull?.("getUserDirectory",{userId:_},z),z}newUserInfoDocument_(_,q){this.logger_.logMethodArgs?.("newUserInfoDocument",{userId:_,userInfo:q});let z={...B.nitrobaseIds.userInfo,ownerId:_};if(this.nitrobase_.hasStore(z)){this.logger_.accident?.("newUserInfoDocument_","store_already_exists",z);return}this.nitrobase_.newDocument(z,q)}openUserInfoDocument_(_){return this.logger_.logMethodArgs?.("openUserInfoDocument_",_),this.nitrobase_.openDocument({...B.nitrobaseIds.userInfo,ownerId:_})}async makeTokenFile_(_,q,z){this.logger_.logMethodArgs?.("makeTokenFile",{userId:_,token:q.slice(0,12)+"...",isManager:z});let C=await this.getUserDirectory(_);if(await K(A(C,`.token/${q}.asn`)),z===!0)await K(A(C,".auth/manager.asn"))}async getUserTokenFilePath_(_,q){let z=A(await this.getUserDirectory(_),`.token/${q}.asn`);return this.logger_.logMethodFull?.("getTokenFilePath",{userId:_,token:q.slice(0,12)+"..."},z),z}}export{B as NitrobaseUserManagement};
|
|
3
|
+
|
|
4
|
+
//# debugId=503C03B8A55F958F64756E2164756E21
|
|
5
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/user-management.ts", "../src/logger.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {dirname, resolve} from 'node:path';\n\nimport {createLogger} from '@alwatr/logger';\nimport {Region, type AlwatrAuth, type StoreFileId, type StoreFileMeta} from '@alwatr/nitrobase-types';\nimport {existsSync, makeEmptyFile} from '@alwatr/node-fs';\n\nimport {logger} from './logger.js';\n\nimport type {AlwatrNitrobase} from '@alwatr/nitrobase-engine';\nimport type {DocumentReference} from '@alwatr/nitrobase-reference';\n\n/**\n * New user data type.\n */\nexport type NewUser<TUser extends JsonObject> = AlwatrAuth & {\n data: TUser;\n isManager?: true;\n};\n\n/**\n * Nitrobase User Management Interface.\n */\ninterface NitrobaseUserManagementInterface<TUser extends JsonObject> {\n /**\n * Create a new user.\n * @param user The new user data.\n */\n newUser(user: NewUser<TUser>): Promise<void>;\n\n /**\n * Check if a user exists.\n * @param userId The user ID.\n * @returns True if the user exists, false otherwise.\n */\n hasUser(userId: string): Promise<boolean>;\n\n /**\n * Get user data.\n * @param userId The user ID.\n * @returns The user data.\n */\n getUserData(userId: string): Promise<TUser>;\n\n /**\n * Get user meta data.\n * @param userId The user ID.\n * @returns The user meta data.\n */\n getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>>;\n\n /**\n * Verify user token.\n * @param userId The user ID.\n * @param token The user token.\n * @returns True if the token is valid, false otherwise.\n */\n verifyUserToken(userId: string, token: string): Promise<boolean>;\n\n /**\n * Save user data to storage.\n * @param userId The user ID.\n */\n save(userId: string): void;\n\n /**\n * Immediately save user data to storage.\n * @param userId The user ID.\n */\n saveImmediate(userId: string): void;\n\n /**\n * Get all user IDs.\n * @returns An async generator of user IDs.\n */\n userIds(): Promise<Generator<string, void, void>>;\n\n /**\n * Get the user directory path.\n * @param userId The user ID.\n * @returns The user directory path.\n */\n getUserDirectory(userId: string): Promise<string>;\n}\n\n/**\n * Nitrobase User Management Class.\n * Manages user data and authentication.\n */\nexport class NitrobaseUserManagement<TUser extends JsonObject> implements NitrobaseUserManagementInterface<TUser> {\n /**\n * Default Nitrobase store IDs used by this class.\n */\n public static readonly nitrobaseIds = {\n userList: {\n name: 'user-list',\n region: Region.Managers,\n schemaVer: 1,\n } as Readonly<StoreFileId>,\n\n userInfo: {\n name: 'user-info',\n region: Region.PerUser,\n } as Readonly<StoreFileId>,\n } as const;\n\n /**\n * Initialize the user management module.\n * @param nitrobase The Nitrobase instance.\n */\n public static async initialize(nitrobase: AlwatrNitrobase): Promise<void> {\n logger.logMethod?.('NitrobaseUserManagement.initialize');\n nitrobase.newCollection(this.nitrobaseIds.userList);\n }\n\n protected logger_ = createLogger('user-management');\n\n protected userListCollection_;\n\n /**\n * Create a new instance of NitrobaseUserManagement.\n * @param nitrobase_ The Nitrobase instance.\n */\n constructor(protected readonly nitrobase_: AlwatrNitrobase) {\n this.logger_.logMethod?.('constructor');\n this.userListCollection_ = this.nitrobase_.openCollection(NitrobaseUserManagement.nitrobaseIds.userList);\n }\n\n // Implementation of NitrobaseUserManagementInterface methods with JSDoc comments\n\n public async newUser(user: NewUser<TUser>): Promise<void> {\n this.logger_.logMethodArgs?.('newUser', user);\n\n (await this.userListCollection_).addItem(user.userId, {});\n\n this.newUserInfoDocument_(user.userId, user.data);\n\n await this.makeTokenFile_(user.userId, user.userToken, user.isManager);\n }\n\n public async hasUser(userId: string): Promise<boolean> {\n const userExists = (await this.userListCollection_).hasItem(userId);\n this.logger_.logMethodFull?.('hasUser', {userId}, userExists);\n return userExists;\n }\n\n public async getUserData(userId: string): Promise<TUser> {\n this.logger_.logMethodArgs?.('getUserData', userId);\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n return userInfoDocument.getData();\n }\n\n public async getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>> {\n this.logger_.logMethodArgs?.('getUserMeta', userId);\n const document = await this.openUserInfoDocument_(userId);\n return document.getStoreMeta();\n }\n\n public save(userId: string): void {\n this.logger_.logMethodArgs?.('save', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.save();\n });\n }\n\n public saveImmediate(userId: string): void {\n this.logger_.logMethodArgs?.('saveImmediate', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.saveImmediate();\n });\n }\n\n public async userIds(): Promise<Generator<string, void, void>> {\n return (await this.userListCollection_).ids();\n }\n\n public async verifyUserToken(userId: string, token: string): Promise<boolean> {\n const tokenFileExist = existsSync(await this.getUserTokenFilePath_(userId, token));\n this.logger_.logMethodFull?.('verifyUserToken', {userId, token: token.slice(0, 12) + '...'}, tokenFileExist);\n return tokenFileExist;\n }\n\n public async getUserDirectory(userId: string): Promise<string> {\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n const userDirectoryPath = dirname(resolve(this.nitrobase_.config.rootPath, userInfoDocument.path));\n this.logger_.logMethodFull?.('getUserDirectory', {userId}, userDirectoryPath);\n return userDirectoryPath;\n }\n\n /**\n * Create a new user info document.\n * @param userId The user ID.\n * @param userInfo The user info data.\n */\n protected newUserInfoDocument_(userId: string, userInfo: TUser): void {\n this.logger_.logMethodArgs?.('newUserInfoDocument', {userId, userInfo});\n const storeId: StoreFileId = {\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n };\n if (this.nitrobase_.hasStore(storeId)) {\n this.logger_.accident?.('newUserInfoDocument_', 'store_already_exists', storeId);\n return;\n }\n this.nitrobase_.newDocument<TUser>(storeId, userInfo);\n }\n\n /**\n * Open the user info document.\n * @param userId The user ID.\n * @returns A promise resolving to the user info document.\n */\n protected openUserInfoDocument_(userId: string): Promise<DocumentReference<TUser>> {\n this.logger_.logMethodArgs?.('openUserInfoDocument_', userId);\n return this.nitrobase_.openDocument<TUser>({\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n });\n }\n\n /**\n * Create a token file for the user.\n * @param userId The user ID.\n * @param token The user token.\n * @param isManager Whether the user is a manager.\n */\n protected async makeTokenFile_(userId: string, token: string, isManager?: true): Promise<void> {\n this.logger_.logMethodArgs?.('makeTokenFile', {userId, token: token.slice(0, 12) + '...', isManager});\n const userDirectory = await this.getUserDirectory(userId);\n\n await makeEmptyFile(resolve(userDirectory, `.token/${token}.asn`));\n\n if (isManager === true) {\n await makeEmptyFile(resolve(userDirectory, '.auth/manager.asn'));\n }\n }\n\n /**\n * Get the path to the user token file.\n * @param userId The user ID.\n * @param token The user token.\n * @returns The path to the user token file.\n */\n protected async getUserTokenFilePath_(userId: string, token: string): Promise<string> {\n const userTokenFilePath = resolve(await this.getUserDirectory(userId), `.token/${token}.asn`);\n this.logger_.logMethodFull?.('getTokenFilePath', {userId, token: token.slice(0, 12) + '...'}, userTokenFilePath);\n return userTokenFilePath;\n }\n}\n",
|
|
6
|
+
"import {createLogger} from '@alwatr/logger';\n\n\nexport const logger = /* #__PURE__ */ createLogger(__package_name__);\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AAAA,kBAAQ,aAAS,kBAEjB,uBAAQ,uBACR,iBAAQ,gCACR,qBAAQ,mBAAY,wBCJpB,uBAAQ,uBAGD,IAAM,EAAyB,EAAa,mCAAgB,EDqF5D,MAAM,CAAqG,CAkCjF,iBA9BR,cAAe,CACpC,SAAU,CACR,KAAM,YACN,OAAQ,EAAO,SACf,UAAW,CACb,EAEA,SAAU,CACR,KAAM,YACN,OAAQ,EAAO,OACjB,CACF,cAMoB,WAAU,CAAC,EAA2C,CACxE,EAAO,YAAY,oCAAoC,EACvD,EAAU,cAAc,KAAK,aAAa,QAAQ,EAG1C,QAAU,EAAa,iBAAiB,EAExC,oBAMV,WAAW,CAAoB,EAA6B,CAA7B,kBAC7B,KAAK,QAAQ,YAAY,aAAa,EACtC,KAAK,oBAAsB,KAAK,WAAW,eAAe,EAAwB,aAAa,QAAQ,OAK5F,QAAO,CAAC,EAAqC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1
|
{
|
|
2
2
|
"name": "@alwatr/nitrobase-user-management",
|
|
3
|
+
"version": "9.1.0",
|
|
3
4
|
"description": "",
|
|
4
|
-
"
|
|
5
|
-
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"@alwatr/nitrobase-types": "^7.10.1",
|
|
12
|
-
"@alwatr/node-fs": "^5.5.29",
|
|
13
|
-
"@alwatr/type-helper": "^7.0.0"
|
|
14
|
-
},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
"@alwatr/nanolib": "^7.3.4",
|
|
17
|
-
"@types/node": "^24.10.4",
|
|
18
|
-
"typescript": "^5.9.3"
|
|
5
|
+
"license": "MPL-2.0",
|
|
6
|
+
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/Alwatr/alwatr",
|
|
11
|
+
"directory": "pkg/nitrobase-old/user-management"
|
|
19
12
|
},
|
|
13
|
+
"homepage": "https://github.com/Alwatr/alwatr/tree/main/pkg/nitrobase-old/user-management#readme",
|
|
14
|
+
"bugs": "https://github.com/Alwatr/alwatr/issues",
|
|
20
15
|
"exports": {
|
|
21
16
|
".": {
|
|
22
17
|
"types": "./dist/main.d.ts",
|
|
23
|
-
"import": "./dist/main.
|
|
24
|
-
"
|
|
18
|
+
"import": "./dist/main.js",
|
|
19
|
+
"default": "./dist/main.js"
|
|
25
20
|
}
|
|
26
21
|
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@alwatr/logger": "9.1.0",
|
|
25
|
+
"@alwatr/nitrobase-engine": "^9.1.0",
|
|
26
|
+
"@alwatr/nitrobase-reference": "^9.1.0",
|
|
27
|
+
"@alwatr/nitrobase-types": "^9.1.0",
|
|
28
|
+
"@alwatr/node-fs": "9.1.0",
|
|
29
|
+
"@alwatr/type-helper": "9.1.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@alwatr/nano-build": "9.1.0",
|
|
33
|
+
"@alwatr/tsconfig-base": "9.1.0",
|
|
34
|
+
"@alwatr/type-helper": "workspace:*",
|
|
35
|
+
"@types/node": "^25.5.0",
|
|
36
|
+
"typescript": "^6.0.2"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"b": "bun run build",
|
|
40
|
+
"build": "bun run build:ts && bun run build:es",
|
|
41
|
+
"build:es": "nano-build --preset=module src/main.ts",
|
|
42
|
+
"build:ts": "tsc --build",
|
|
43
|
+
"cl": "bun run clean",
|
|
44
|
+
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
45
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
46
|
+
"lint": "eslint src/ --ext .ts",
|
|
47
|
+
"t": "bun run test",
|
|
48
|
+
"test": "ALWATR_DEBUG=0 bun test",
|
|
49
|
+
"w": "bun run watch",
|
|
50
|
+
"watch": "bun run watch:ts & bun run watch:es",
|
|
51
|
+
"watch:es": "bun run build:es --watch",
|
|
52
|
+
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
53
|
+
},
|
|
27
54
|
"files": [
|
|
28
|
-
"
|
|
29
|
-
"
|
|
55
|
+
"dist",
|
|
56
|
+
"src/**/*.ts",
|
|
57
|
+
"!src/**/*.test.ts",
|
|
58
|
+
"README.md",
|
|
59
|
+
"LICENSE"
|
|
30
60
|
],
|
|
31
|
-
"
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
32
64
|
"keywords": [
|
|
33
65
|
"alwatr",
|
|
34
66
|
"data",
|
|
@@ -44,34 +76,6 @@
|
|
|
44
76
|
"user",
|
|
45
77
|
"user-management"
|
|
46
78
|
],
|
|
47
|
-
"license": "MPL-2.0",
|
|
48
|
-
"main": "./dist/main.cjs",
|
|
49
|
-
"module": "./dist/main.mjs",
|
|
50
79
|
"prettier": "@alwatr/nanolib/prettier-config",
|
|
51
|
-
"
|
|
52
|
-
"access": "public"
|
|
53
|
-
},
|
|
54
|
-
"repository": {
|
|
55
|
-
"type": "git",
|
|
56
|
-
"url": "https://github.com/Alwatr/nitrobase",
|
|
57
|
-
"directory": "packages/user-management"
|
|
58
|
-
},
|
|
59
|
-
"scripts": {
|
|
60
|
-
"b": "yarn run build",
|
|
61
|
-
"build": "yarn run build:ts & yarn run build:es",
|
|
62
|
-
"build:es": "nano-build --preset=module",
|
|
63
|
-
"build:ts": "tsc --build",
|
|
64
|
-
"c": "yarn run clean",
|
|
65
|
-
"cb": "yarn run clean && yarn run build",
|
|
66
|
-
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
67
|
-
"d": "yarn run build:es && yarn node",
|
|
68
|
-
"w": "yarn run watch",
|
|
69
|
-
"watch": "yarn run watch:ts & yarn run watch:es",
|
|
70
|
-
"watch:es": "yarn run build:es --watch",
|
|
71
|
-
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput"
|
|
72
|
-
},
|
|
73
|
-
"sideEffects": false,
|
|
74
|
-
"type": "module",
|
|
75
|
-
"types": "./dist/main.d.ts",
|
|
76
|
-
"gitHead": "e23943a279d6b0905b205c0c17b2b3249efab226"
|
|
80
|
+
"gitHead": "4a25cd3e0499cf61dd761e87d3711abf9b0cd208"
|
|
77
81
|
}
|
package/src/logger.ts
ADDED
package/src/main.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './user-management.js';
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import {dirname, resolve} from 'node:path';
|
|
2
|
+
|
|
3
|
+
import {createLogger} from '@alwatr/logger';
|
|
4
|
+
import {Region, type AlwatrAuth, type StoreFileId, type StoreFileMeta} from '@alwatr/nitrobase-types';
|
|
5
|
+
import {existsSync, makeEmptyFile} from '@alwatr/node-fs';
|
|
6
|
+
|
|
7
|
+
import {logger} from './logger.js';
|
|
8
|
+
|
|
9
|
+
import type {AlwatrNitrobase} from '@alwatr/nitrobase-engine';
|
|
10
|
+
import type {DocumentReference} from '@alwatr/nitrobase-reference';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* New user data type.
|
|
14
|
+
*/
|
|
15
|
+
export type NewUser<TUser extends JsonObject> = AlwatrAuth & {
|
|
16
|
+
data: TUser;
|
|
17
|
+
isManager?: true;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Nitrobase User Management Interface.
|
|
22
|
+
*/
|
|
23
|
+
interface NitrobaseUserManagementInterface<TUser extends JsonObject> {
|
|
24
|
+
/**
|
|
25
|
+
* Create a new user.
|
|
26
|
+
* @param user The new user data.
|
|
27
|
+
*/
|
|
28
|
+
newUser(user: NewUser<TUser>): Promise<void>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check if a user exists.
|
|
32
|
+
* @param userId The user ID.
|
|
33
|
+
* @returns True if the user exists, false otherwise.
|
|
34
|
+
*/
|
|
35
|
+
hasUser(userId: string): Promise<boolean>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get user data.
|
|
39
|
+
* @param userId The user ID.
|
|
40
|
+
* @returns The user data.
|
|
41
|
+
*/
|
|
42
|
+
getUserData(userId: string): Promise<TUser>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get user meta data.
|
|
46
|
+
* @param userId The user ID.
|
|
47
|
+
* @returns The user meta data.
|
|
48
|
+
*/
|
|
49
|
+
getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Verify user token.
|
|
53
|
+
* @param userId The user ID.
|
|
54
|
+
* @param token The user token.
|
|
55
|
+
* @returns True if the token is valid, false otherwise.
|
|
56
|
+
*/
|
|
57
|
+
verifyUserToken(userId: string, token: string): Promise<boolean>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Save user data to storage.
|
|
61
|
+
* @param userId The user ID.
|
|
62
|
+
*/
|
|
63
|
+
save(userId: string): void;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Immediately save user data to storage.
|
|
67
|
+
* @param userId The user ID.
|
|
68
|
+
*/
|
|
69
|
+
saveImmediate(userId: string): void;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get all user IDs.
|
|
73
|
+
* @returns An async generator of user IDs.
|
|
74
|
+
*/
|
|
75
|
+
userIds(): Promise<Generator<string, void, void>>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get the user directory path.
|
|
79
|
+
* @param userId The user ID.
|
|
80
|
+
* @returns The user directory path.
|
|
81
|
+
*/
|
|
82
|
+
getUserDirectory(userId: string): Promise<string>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Nitrobase User Management Class.
|
|
87
|
+
* Manages user data and authentication.
|
|
88
|
+
*/
|
|
89
|
+
export class NitrobaseUserManagement<TUser extends JsonObject> implements NitrobaseUserManagementInterface<TUser> {
|
|
90
|
+
/**
|
|
91
|
+
* Default Nitrobase store IDs used by this class.
|
|
92
|
+
*/
|
|
93
|
+
public static readonly nitrobaseIds = {
|
|
94
|
+
userList: {
|
|
95
|
+
name: 'user-list',
|
|
96
|
+
region: Region.Managers,
|
|
97
|
+
schemaVer: 1,
|
|
98
|
+
} as Readonly<StoreFileId>,
|
|
99
|
+
|
|
100
|
+
userInfo: {
|
|
101
|
+
name: 'user-info',
|
|
102
|
+
region: Region.PerUser,
|
|
103
|
+
} as Readonly<StoreFileId>,
|
|
104
|
+
} as const;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Initialize the user management module.
|
|
108
|
+
* @param nitrobase The Nitrobase instance.
|
|
109
|
+
*/
|
|
110
|
+
public static async initialize(nitrobase: AlwatrNitrobase): Promise<void> {
|
|
111
|
+
logger.logMethod?.('NitrobaseUserManagement.initialize');
|
|
112
|
+
nitrobase.newCollection(this.nitrobaseIds.userList);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
protected logger_ = createLogger('user-management');
|
|
116
|
+
|
|
117
|
+
protected userListCollection_;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Create a new instance of NitrobaseUserManagement.
|
|
121
|
+
* @param nitrobase_ The Nitrobase instance.
|
|
122
|
+
*/
|
|
123
|
+
constructor(protected readonly nitrobase_: AlwatrNitrobase) {
|
|
124
|
+
this.logger_.logMethod?.('constructor');
|
|
125
|
+
this.userListCollection_ = this.nitrobase_.openCollection(NitrobaseUserManagement.nitrobaseIds.userList);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Implementation of NitrobaseUserManagementInterface methods with JSDoc comments
|
|
129
|
+
|
|
130
|
+
public async newUser(user: NewUser<TUser>): Promise<void> {
|
|
131
|
+
this.logger_.logMethodArgs?.('newUser', user);
|
|
132
|
+
|
|
133
|
+
(await this.userListCollection_).addItem(user.userId, {});
|
|
134
|
+
|
|
135
|
+
this.newUserInfoDocument_(user.userId, user.data);
|
|
136
|
+
|
|
137
|
+
await this.makeTokenFile_(user.userId, user.userToken, user.isManager);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public async hasUser(userId: string): Promise<boolean> {
|
|
141
|
+
const userExists = (await this.userListCollection_).hasItem(userId);
|
|
142
|
+
this.logger_.logMethodFull?.('hasUser', {userId}, userExists);
|
|
143
|
+
return userExists;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public async getUserData(userId: string): Promise<TUser> {
|
|
147
|
+
this.logger_.logMethodArgs?.('getUserData', userId);
|
|
148
|
+
const userInfoDocument = await this.openUserInfoDocument_(userId);
|
|
149
|
+
return userInfoDocument.getData();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public async getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>> {
|
|
153
|
+
this.logger_.logMethodArgs?.('getUserMeta', userId);
|
|
154
|
+
const document = await this.openUserInfoDocument_(userId);
|
|
155
|
+
return document.getStoreMeta();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public save(userId: string): void {
|
|
159
|
+
this.logger_.logMethodArgs?.('save', userId);
|
|
160
|
+
this.openUserInfoDocument_(userId).then((document) => {
|
|
161
|
+
document.save();
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public saveImmediate(userId: string): void {
|
|
166
|
+
this.logger_.logMethodArgs?.('saveImmediate', userId);
|
|
167
|
+
this.openUserInfoDocument_(userId).then((document) => {
|
|
168
|
+
document.saveImmediate();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public async userIds(): Promise<Generator<string, void, void>> {
|
|
173
|
+
return (await this.userListCollection_).ids();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async verifyUserToken(userId: string, token: string): Promise<boolean> {
|
|
177
|
+
const tokenFileExist = existsSync(await this.getUserTokenFilePath_(userId, token));
|
|
178
|
+
this.logger_.logMethodFull?.('verifyUserToken', {userId, token: token.slice(0, 12) + '...'}, tokenFileExist);
|
|
179
|
+
return tokenFileExist;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public async getUserDirectory(userId: string): Promise<string> {
|
|
183
|
+
const userInfoDocument = await this.openUserInfoDocument_(userId);
|
|
184
|
+
const userDirectoryPath = dirname(resolve(this.nitrobase_.config.rootPath, userInfoDocument.path));
|
|
185
|
+
this.logger_.logMethodFull?.('getUserDirectory', {userId}, userDirectoryPath);
|
|
186
|
+
return userDirectoryPath;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Create a new user info document.
|
|
191
|
+
* @param userId The user ID.
|
|
192
|
+
* @param userInfo The user info data.
|
|
193
|
+
*/
|
|
194
|
+
protected newUserInfoDocument_(userId: string, userInfo: TUser): void {
|
|
195
|
+
this.logger_.logMethodArgs?.('newUserInfoDocument', {userId, userInfo});
|
|
196
|
+
const storeId: StoreFileId = {
|
|
197
|
+
...NitrobaseUserManagement.nitrobaseIds.userInfo,
|
|
198
|
+
ownerId: userId,
|
|
199
|
+
};
|
|
200
|
+
if (this.nitrobase_.hasStore(storeId)) {
|
|
201
|
+
this.logger_.accident?.('newUserInfoDocument_', 'store_already_exists', storeId);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.nitrobase_.newDocument<TUser>(storeId, userInfo);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Open the user info document.
|
|
209
|
+
* @param userId The user ID.
|
|
210
|
+
* @returns A promise resolving to the user info document.
|
|
211
|
+
*/
|
|
212
|
+
protected openUserInfoDocument_(userId: string): Promise<DocumentReference<TUser>> {
|
|
213
|
+
this.logger_.logMethodArgs?.('openUserInfoDocument_', userId);
|
|
214
|
+
return this.nitrobase_.openDocument<TUser>({
|
|
215
|
+
...NitrobaseUserManagement.nitrobaseIds.userInfo,
|
|
216
|
+
ownerId: userId,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Create a token file for the user.
|
|
222
|
+
* @param userId The user ID.
|
|
223
|
+
* @param token The user token.
|
|
224
|
+
* @param isManager Whether the user is a manager.
|
|
225
|
+
*/
|
|
226
|
+
protected async makeTokenFile_(userId: string, token: string, isManager?: true): Promise<void> {
|
|
227
|
+
this.logger_.logMethodArgs?.('makeTokenFile', {userId, token: token.slice(0, 12) + '...', isManager});
|
|
228
|
+
const userDirectory = await this.getUserDirectory(userId);
|
|
229
|
+
|
|
230
|
+
await makeEmptyFile(resolve(userDirectory, `.token/${token}.asn`));
|
|
231
|
+
|
|
232
|
+
if (isManager === true) {
|
|
233
|
+
await makeEmptyFile(resolve(userDirectory, '.auth/manager.asn'));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Get the path to the user token file.
|
|
239
|
+
* @param userId The user ID.
|
|
240
|
+
* @param token The user token.
|
|
241
|
+
* @returns The path to the user token file.
|
|
242
|
+
*/
|
|
243
|
+
protected async getUserTokenFilePath_(userId: string, token: string): Promise<string> {
|
|
244
|
+
const userTokenFilePath = resolve(await this.getUserDirectory(userId), `.token/${token}.asn`);
|
|
245
|
+
this.logger_.logMethodFull?.('getTokenFilePath', {userId, token: token.slice(0, 12) + '...'}, userTokenFilePath);
|
|
246
|
+
return userTokenFilePath;
|
|
247
|
+
}
|
|
248
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [7.10.1](https://github.com/Alwatr/nitrobase/compare/v7.10.0...v7.10.1) (2025-12-14)
|
|
7
|
-
|
|
8
|
-
### 🧹 Miscellaneous Chores
|
|
9
|
-
|
|
10
|
-
* update tsconfig extends to use @alwatr/nanolib ([ffbc8ce](https://github.com/Alwatr/nitrobase/commit/ffbc8ce74d47d7f4e1ff0061722df10f68af11cc))
|
|
11
|
-
|
|
12
|
-
### 🔗 Dependencies update
|
|
13
|
-
|
|
14
|
-
* replace '@alwatr/nano-build' with '@alwatr/nanolib' in logger imports ([9a355b9](https://github.com/Alwatr/nitrobase/commit/9a355b92b27434a3cd132e7e52df5c2e112ca97e))
|
|
15
|
-
* update @alwatr/logger and @alwatr/node-fs versions; switch prettier config to @alwatr/nanolib ([a46a819](https://github.com/Alwatr/nitrobase/commit/a46a81911b320e18c627c0be8fb4d103268c6662))
|
|
16
|
-
* update `@alwatr/nanolib` and `@types/node` dependencies across all packages. ([bc6d96f](https://github.com/Alwatr/nitrobase/commit/bc6d96f4fd423bcfb817d74b526596669d8a8aed))
|
|
17
|
-
|
|
18
|
-
## [7.10.0](https://github.com/Alwatr/nitrobase/compare/v7.9.0...v7.10.0) (2025-09-21)
|
|
19
|
-
|
|
20
|
-
### 🐛 Bug Fixes
|
|
21
|
-
|
|
22
|
-
* add "sideEffects": false to package.json files for better tree-shaking ([cb6f75b](https://github.com/Alwatr/nitrobase/commit/cb6f75b86eb7dba6812aaad5136fcd867e885562))
|
|
23
|
-
* add type imports for nano-build and type-helper in logger files ([6ca3627](https://github.com/Alwatr/nitrobase/commit/6ca362761c0d30a2813369bda49c927960c787f8))
|
|
24
|
-
* remove packageTracer import and related code from logger.ts ([b288082](https://github.com/Alwatr/nitrobase/commit/b2880826cdf8f88e5ce512d839b66b5a29935b35))
|
|
25
|
-
* update dependencies in package.json for user-management ([dd53ff3](https://github.com/Alwatr/nitrobase/commit/dd53ff380a86a0ac80e56af57b20ebff2475148a))
|
|
26
|
-
* update logger import and clean up unused lines in user-management.ts ([4fef1b9](https://github.com/Alwatr/nitrobase/commit/4fef1b9192bb5b090c44a610a6d783b77f99097d))
|
|
27
|
-
|
|
28
|
-
### 🔨 Code Refactoring
|
|
29
|
-
|
|
30
|
-
* update access modifiers to public for class properties and methods in nitrobase and user management ([a988d63](https://github.com/Alwatr/nitrobase/commit/a988d63b61828f6ce360e7179cbaccfa19dc1ae2))
|
|
31
|
-
|
|
32
|
-
### 🧹 Miscellaneous Chores
|
|
33
|
-
|
|
34
|
-
* remove unused types from tsconfig.json files and update references ([b504ced](https://github.com/Alwatr/nitrobase/commit/b504ced4a8f98de4fe79a2c974937278bd4e46cb))
|
|
35
|
-
* Update license from AGPL-3.0 to MPL-2.0 ([af1746e](https://github.com/Alwatr/nitrobase/commit/af1746ee9a3786c1cb2e0014f138e9f3b7086b8d))
|
|
36
|
-
* update package.json files across all packages for consistency and improved metadata ([647900d](https://github.com/Alwatr/nitrobase/commit/647900d9966f8df275c4e2ef0550c501be38ece0))
|
|
37
|
-
|
|
38
|
-
### 🔗 Dependencies update
|
|
39
|
-
|
|
40
|
-
* update dependencies in package.json files across all packages to latest versions ([72c20ef](https://github.com/Alwatr/nitrobase/commit/72c20efbc1cac9825fb4880772e3fad797d74a23))
|
|
41
|
-
|
|
42
|
-
## [7.8.0](https://github.com/Alwatr/nitrobase/compare/v7.7.0...v7.8.0) (2025-03-06)
|
|
43
|
-
|
|
44
|
-
### Dependencies update
|
|
45
|
-
|
|
46
|
-
* **deps-dev:** bump the dependencies group across 1 directory with 5 updates ([fe6ab37](https://github.com/Alwatr/nitrobase/commit/fe6ab37263f609f8bce8398462b7105b8e5a55fe)) by @dependabot[bot]
|
|
47
|
-
* update @alwatr/nanolib, @alwatr/nano-build, and @alwatr/type-helper to latest versions ([5f3f89b](https://github.com/Alwatr/nitrobase/commit/5f3f89b1236f66e9167957d60d43c8d0abff108d)) by @
|
|
48
|
-
|
|
49
|
-
## [7.7.0](https://github.com/Alwatr/nitrobase/compare/v7.6.1...v7.7.0) (2025-02-26)
|
|
50
|
-
|
|
51
|
-
**Note:** Version bump only for package @alwatr/nitrobase-user-management
|
|
52
|
-
|
|
53
|
-
## [7.6.0](https://github.com/Alwatr/nitrobase/compare/v7.5.9...v7.6.0) (2025-02-26)
|
|
54
|
-
|
|
55
|
-
### Miscellaneous Chores
|
|
56
|
-
|
|
57
|
-
* rollback all versions ([9ff6c11](https://github.com/Alwatr/nitrobase/commit/9ff6c11ef3e44ea669a814da73ed39ddc2c8e971)) by @alimd
|
|
58
|
-
|
|
59
|
-
## [7.5.8](https://github.com/Alwatr/nitrobase/compare/v7.5.7...v7.5.8) (2025-02-18)
|
|
60
|
-
|
|
61
|
-
### Dependencies update
|
|
62
|
-
|
|
63
|
-
* bump @alwatr/nanolib, @alwatr/nano-build, and @types/node to latest versions across multiple packages ([3e89c62](https://github.com/Alwatr/nitrobase/commit/3e89c62ad20f3afe0c38c6ae36d91bcb6199b231)) by @
|
|
64
|
-
* **deps-dev:** bump the dependencies group across 1 directory with 2 updates ([03715d0](https://github.com/Alwatr/nitrobase/commit/03715d0b5534b127dfb57ffe0ea713fb168eefe6)) by @dependabot[bot]
|
|
65
|
-
|
|
66
|
-
## [7.5.7](https://github.com/Alwatr/nitrobase/compare/v7.5.6...v7.5.7) (2025-02-03)
|
|
67
|
-
|
|
68
|
-
### Dependencies update
|
|
69
|
-
|
|
70
|
-
* update [@alwatr](https://github.com/alwatr) dependencies to latest versions ([f8bb10f](https://github.com/Alwatr/nitrobase/commit/f8bb10fa3343f25e2a442e80fbddb88c55fac813)) by @
|
|
71
|
-
|
|
72
|
-
## [7.5.6](https://github.com/Alwatr/nitrobase/compare/v7.5.5...v7.5.6) (2025-02-03)
|
|
73
|
-
|
|
74
|
-
**Note:** Version bump only for package @alwatr/nitrobase-user-management
|
|
75
|
-
|
|
76
|
-
## [7.5.5](https://github.com/Alwatr/nitrobase/compare/v7.5.4...v7.5.5) (2025-02-02)
|
|
77
|
-
|
|
78
|
-
### Dependencies update
|
|
79
|
-
|
|
80
|
-
* **deps-dev:** bump @types/node ([d60ee96](https://github.com/Alwatr/nitrobase/commit/d60ee96716b8df7837b3d7f9fe4b8705c94e5af0)) by @dependabot[bot]
|
|
81
|
-
* update ([0b9eccd](https://github.com/Alwatr/nitrobase/commit/0b9eccd3b54dade17e3eeb94bd08612c57356801)) by @alimd
|
|
82
|
-
|
|
83
|
-
## [7.5.4](https://github.com/Alwatr/nitrobase/compare/v7.5.3...v7.5.4) (2025-01-12)
|
|
84
|
-
|
|
85
|
-
### Dependencies update
|
|
86
|
-
|
|
87
|
-
* **deps-dev:** bump the dependencies group across 1 directory with 11 updates ([fdd30a6](https://github.com/Alwatr/nitrobase/commit/fdd30a6639ae7ead4e8dbfaca0295cb2bf0e6649)) by @dependabot[bot]
|
|
88
|
-
|
|
89
|
-
## [7.5.0](https://github.com/Alwatr/nitrobase/compare/v7.4.1...v7.5.0) (2024-11-09)
|
|
90
|
-
|
|
91
|
-
### Features
|
|
92
|
-
|
|
93
|
-
* add `user-management` package ([dd0037c](https://github.com/Alwatr/nitrobase/commit/dd0037c9443459520c41f17adbac579069821f95)) by @mohammadhonarvar
|
|
94
|
-
* **user-manaement:** add the initial sturcture ([2897b2c](https://github.com/Alwatr/nitrobase/commit/2897b2c43ce65f5683078fc51022ea3d5cf952af)) by @mohammadhonarvar
|
|
95
|
-
|
|
96
|
-
### Bug Fixes
|
|
97
|
-
|
|
98
|
-
* **user-management:** complete codes & types ([838bfd2](https://github.com/Alwatr/nitrobase/commit/838bfd2ca19fb0443beb68903b60cd8aa9c2f791)) by @mohammadhonarvar
|
|
99
|
-
* **user-management:** review's feedback ([77b2796](https://github.com/Alwatr/nitrobase/commit/77b2796095f4ad87895df9de5f559dae07537020)) by @mohammadhonarvar
|
|
100
|
-
* **user-management:** update `directory` & `homepage` ([32f54f6](https://github.com/Alwatr/nitrobase/commit/32f54f681541373ea3fc8c460d711c04b64c7871)) by @mohammadhonarvar
|
|
101
|
-
* **user-management:** update version & name ([1db77da](https://github.com/Alwatr/nitrobase/commit/1db77daa422efe05a1ec4e6156bd6a053bea14ae)) by @mohammadhonarvar
|
|
102
|
-
|
|
103
|
-
### Code Refactoring
|
|
104
|
-
|
|
105
|
-
* **user-management:** remove commented-out code and clean up interface ([f32ac45](https://github.com/Alwatr/nitrobase/commit/f32ac45c14ffe2e5c1b2eb664c1add182a975d1c)) by @AliMD
|
package/dist/main.cjs
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
/** 📦 @alwatr/nitrobase-user-management v7.10.1 */
|
|
2
|
-
__dev_mode__: console.debug("📦 @alwatr/nitrobase-user-management v7.10.1");
|
|
3
|
-
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{NitrobaseUserManagement:()=>NitrobaseUserManagement});module.exports=__toCommonJS(main_exports);var import_node_path=require("node:path");var import_logger2=require("@alwatr/logger");var import_nitrobase_types=require("@alwatr/nitrobase-types");var import_node_fs=require("@alwatr/node-fs");var import_logger=require("@alwatr/logger");var logger=(0,import_logger.createLogger)("@alwatr/nitrobase-user-management");var NitrobaseUserManagement=class _NitrobaseUserManagement{constructor(nitrobase_){this.nitrobase_=nitrobase_;this.logger_=(0,import_logger2.createLogger)("user-management");this.logger_.logMethod?.("constructor");this.userListCollection_=this.nitrobase_.openCollection(_NitrobaseUserManagement.nitrobaseIds.userList)}static{this.nitrobaseIds={userList:{name:"user-list",region:import_nitrobase_types.Region.Managers,schemaVer:1},userInfo:{name:"user-info",region:import_nitrobase_types.Region.PerUser}}}static async initialize(nitrobase){logger.logMethod?.("NitrobaseUserManagement.initialize");nitrobase.newCollection(this.nitrobaseIds.userList)}async newUser(user){this.logger_.logMethodArgs?.("newUser",user);(await this.userListCollection_).addItem(user.userId,{});this.newUserInfoDocument_(user.userId,user.data);await this.makeTokenFile_(user.userId,user.userToken,user.isManager)}async hasUser(userId){const userExists=(await this.userListCollection_).hasItem(userId);this.logger_.logMethodFull?.("hasUser",{userId},userExists);return userExists}async getUserData(userId){this.logger_.logMethodArgs?.("getUserData",userId);const userInfoDocument=await this.openUserInfoDocument_(userId);return userInfoDocument.getData()}async getUserMeta(userId){this.logger_.logMethodArgs?.("getUserMeta",userId);const document=await this.openUserInfoDocument_(userId);return document.getStoreMeta()}save(userId){this.logger_.logMethodArgs?.("save",userId);this.openUserInfoDocument_(userId).then(document=>{document.save()})}saveImmediate(userId){this.logger_.logMethodArgs?.("saveImmediate",userId);this.openUserInfoDocument_(userId).then(document=>{document.saveImmediate()})}async userIds(){return(await this.userListCollection_).ids()}async verifyUserToken(userId,token){const tokenFileExist=(0,import_node_fs.existsSync)(await this.getUserTokenFilePath_(userId,token));this.logger_.logMethodFull?.("verifyUserToken",{userId,token:token.slice(0,12)+"..."},tokenFileExist);return tokenFileExist}async getUserDirectory(userId){const userInfoDocument=await this.openUserInfoDocument_(userId);const userDirectoryPath=(0,import_node_path.dirname)((0,import_node_path.resolve)(this.nitrobase_.config.rootPath,userInfoDocument.path));this.logger_.logMethodFull?.("getUserDirectory",{userId},userDirectoryPath);return userDirectoryPath}newUserInfoDocument_(userId,userInfo){this.logger_.logMethodArgs?.("newUserInfoDocument",{userId,userInfo});const storeId={..._NitrobaseUserManagement.nitrobaseIds.userInfo,ownerId:userId};if(this.nitrobase_.hasStore(storeId)){this.logger_.accident?.("newUserInfoDocument_","store_already_exists",storeId);return}this.nitrobase_.newDocument(storeId,userInfo)}openUserInfoDocument_(userId){this.logger_.logMethodArgs?.("openUserInfoDocument_",userId);return this.nitrobase_.openDocument({..._NitrobaseUserManagement.nitrobaseIds.userInfo,ownerId:userId})}async makeTokenFile_(userId,token,isManager){this.logger_.logMethodArgs?.("makeTokenFile",{userId,token:token.slice(0,12)+"...",isManager});const userDirectory=await this.getUserDirectory(userId);await(0,import_node_fs.makeEmptyFile)((0,import_node_path.resolve)(userDirectory,`.token/${token}.asn`));if(isManager===true){await(0,import_node_fs.makeEmptyFile)((0,import_node_path.resolve)(userDirectory,".auth/manager.asn"))}}async getUserTokenFilePath_(userId,token){const userTokenFilePath=(0,import_node_path.resolve)(await this.getUserDirectory(userId),`.token/${token}.asn`);this.logger_.logMethodFull?.("getTokenFilePath",{userId,token:token.slice(0,12)+"..."},userTokenFilePath);return userTokenFilePath}};0&&(module.exports={NitrobaseUserManagement});
|
|
4
|
-
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/main.ts", "../src/user-management.ts", "../src/logger.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './user-management.js';\n", "import {dirname, resolve} from 'node:path';\n\nimport {createLogger} from '@alwatr/logger';\nimport {Region, type AlwatrAuth, type StoreFileId, type StoreFileMeta} from '@alwatr/nitrobase-types';\nimport {existsSync, makeEmptyFile} from '@alwatr/node-fs';\n\nimport {logger} from './logger.js';\n\nimport type {AlwatrNitrobase} from '@alwatr/nitrobase-engine';\nimport type {DocumentReference} from '@alwatr/nitrobase-reference';\n\n/**\n * New user data type.\n */\nexport type NewUser<TUser extends JsonObject> = AlwatrAuth & {\n data: TUser;\n isManager?: true;\n};\n\n/**\n * Nitrobase User Management Interface.\n */\ninterface NitrobaseUserManagementInterface<TUser extends JsonObject> {\n /**\n * Create a new user.\n * @param user The new user data.\n */\n newUser(user: NewUser<TUser>): Promise<void>;\n\n /**\n * Check if a user exists.\n * @param userId The user ID.\n * @returns True if the user exists, false otherwise.\n */\n hasUser(userId: string): Promise<boolean>;\n\n /**\n * Get user data.\n * @param userId The user ID.\n * @returns The user data.\n */\n getUserData(userId: string): Promise<TUser>;\n\n /**\n * Get user meta data.\n * @param userId The user ID.\n * @returns The user meta data.\n */\n getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>>;\n\n /**\n * Verify user token.\n * @param userId The user ID.\n * @param token The user token.\n * @returns True if the token is valid, false otherwise.\n */\n verifyUserToken(userId: string, token: string): Promise<boolean>;\n\n /**\n * Save user data to storage.\n * @param userId The user ID.\n */\n save(userId: string): void;\n\n /**\n * Immediately save user data to storage.\n * @param userId The user ID.\n */\n saveImmediate(userId: string): void;\n\n /**\n * Get all user IDs.\n * @returns An async generator of user IDs.\n */\n userIds(): Promise<Generator<string, void, void>>;\n\n /**\n * Get the user directory path.\n * @param userId The user ID.\n * @returns The user directory path.\n */\n getUserDirectory(userId: string): Promise<string>;\n}\n\n/**\n * Nitrobase User Management Class.\n * Manages user data and authentication.\n */\nexport class NitrobaseUserManagement<TUser extends JsonObject> implements NitrobaseUserManagementInterface<TUser> {\n /**\n * Default Nitrobase store IDs used by this class.\n */\n public static readonly nitrobaseIds = {\n userList: {\n name: 'user-list',\n region: Region.Managers,\n schemaVer: 1,\n } as Readonly<StoreFileId>,\n\n userInfo: {\n name: 'user-info',\n region: Region.PerUser,\n } as Readonly<StoreFileId>,\n } as const;\n\n /**\n * Initialize the user management module.\n * @param nitrobase The Nitrobase instance.\n */\n public static async initialize(nitrobase: AlwatrNitrobase): Promise<void> {\n logger.logMethod?.('NitrobaseUserManagement.initialize');\n nitrobase.newCollection(this.nitrobaseIds.userList);\n }\n\n protected logger_ = createLogger('user-management');\n\n protected userListCollection_;\n\n /**\n * Create a new instance of NitrobaseUserManagement.\n * @param nitrobase_ The Nitrobase instance.\n */\n constructor(protected readonly nitrobase_: AlwatrNitrobase) {\n this.logger_.logMethod?.('constructor');\n this.userListCollection_ = this.nitrobase_.openCollection(NitrobaseUserManagement.nitrobaseIds.userList);\n }\n\n // Implementation of NitrobaseUserManagementInterface methods with JSDoc comments\n\n public async newUser(user: NewUser<TUser>): Promise<void> {\n this.logger_.logMethodArgs?.('newUser', user);\n\n (await this.userListCollection_).addItem(user.userId, {});\n\n this.newUserInfoDocument_(user.userId, user.data);\n\n await this.makeTokenFile_(user.userId, user.userToken, user.isManager);\n }\n\n public async hasUser(userId: string): Promise<boolean> {\n const userExists = (await this.userListCollection_).hasItem(userId);\n this.logger_.logMethodFull?.('hasUser', {userId}, userExists);\n return userExists;\n }\n\n public async getUserData(userId: string): Promise<TUser> {\n this.logger_.logMethodArgs?.('getUserData', userId);\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n return userInfoDocument.getData();\n }\n\n public async getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>> {\n this.logger_.logMethodArgs?.('getUserMeta', userId);\n const document = await this.openUserInfoDocument_(userId);\n return document.getStoreMeta();\n }\n\n public save(userId: string): void {\n this.logger_.logMethodArgs?.('save', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.save();\n });\n }\n\n public saveImmediate(userId: string): void {\n this.logger_.logMethodArgs?.('saveImmediate', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.saveImmediate();\n });\n }\n\n public async userIds(): Promise<Generator<string, void, void>> {\n return (await this.userListCollection_).ids();\n }\n\n public async verifyUserToken(userId: string, token: string): Promise<boolean> {\n const tokenFileExist = existsSync(await this.getUserTokenFilePath_(userId, token));\n this.logger_.logMethodFull?.('verifyUserToken', {userId, token: token.slice(0, 12) + '...'}, tokenFileExist);\n return tokenFileExist;\n }\n\n public async getUserDirectory(userId: string): Promise<string> {\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n const userDirectoryPath = dirname(resolve(this.nitrobase_.config.rootPath, userInfoDocument.path));\n this.logger_.logMethodFull?.('getUserDirectory', {userId}, userDirectoryPath);\n return userDirectoryPath;\n }\n\n /**\n * Create a new user info document.\n * @param userId The user ID.\n * @param userInfo The user info data.\n */\n protected newUserInfoDocument_(userId: string, userInfo: TUser): void {\n this.logger_.logMethodArgs?.('newUserInfoDocument', {userId, userInfo});\n const storeId: StoreFileId = {\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n };\n if (this.nitrobase_.hasStore(storeId)) {\n this.logger_.accident?.('newUserInfoDocument_', 'store_already_exists', storeId);\n return;\n }\n this.nitrobase_.newDocument<TUser>(storeId, userInfo);\n }\n\n /**\n * Open the user info document.\n * @param userId The user ID.\n * @returns A promise resolving to the user info document.\n */\n protected openUserInfoDocument_(userId: string): Promise<DocumentReference<TUser>> {\n this.logger_.logMethodArgs?.('openUserInfoDocument_', userId);\n return this.nitrobase_.openDocument<TUser>({\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n });\n }\n\n /**\n * Create a token file for the user.\n * @param userId The user ID.\n * @param token The user token.\n * @param isManager Whether the user is a manager.\n */\n protected async makeTokenFile_(userId: string, token: string, isManager?: true): Promise<void> {\n this.logger_.logMethodArgs?.('makeTokenFile', {userId, token: token.slice(0, 12) + '...', isManager});\n const userDirectory = await this.getUserDirectory(userId);\n\n await makeEmptyFile(resolve(userDirectory, `.token/${token}.asn`));\n\n if (isManager === true) {\n await makeEmptyFile(resolve(userDirectory, '.auth/manager.asn'));\n }\n }\n\n /**\n * Get the path to the user token file.\n * @param userId The user ID.\n * @param token The user token.\n * @returns The path to the user token file.\n */\n protected async getUserTokenFilePath_(userId: string, token: string): Promise<string> {\n const userTokenFilePath = resolve(await this.getUserDirectory(userId), `.token/${token}.asn`);\n this.logger_.logMethodFull?.('getTokenFilePath', {userId, token: token.slice(0, 12) + '...'}, userTokenFilePath);\n return userTokenFilePath;\n }\n}\n", "import {createLogger} from '@alwatr/logger';\n\nimport type {} from '@alwatr/nanolib';\nimport type {} from '@alwatr/type-helper';\n\nexport const logger = /* #__PURE__ */ createLogger(__package_name__);\n"],
|
|
5
|
-
"mappings": ";;qqBAAA,2ICAA,qBAA+B,qBAE/B,IAAAA,eAA2B,0BAC3B,2BAA4E,mCAC5E,mBAAwC,2BCJxC,kBAA2B,0BAKpB,IAAM,UAAyB,4BAAa,mCAAgB,EDmF5D,IAAM,wBAAN,MAAM,wBAAqG,CAkChH,YAA+B,WAA6B,CAA7B,2BAR/B,KAAU,WAAU,6BAAa,iBAAiB,EAShD,KAAK,QAAQ,YAAY,aAAa,EACtC,KAAK,oBAAsB,KAAK,WAAW,eAAe,yBAAwB,aAAa,QAAQ,CACzG,CAjCA,YAAuB,aAAe,CACpC,SAAU,CACR,KAAM,YACN,OAAQ,8BAAO,SACf,UAAW,CACb,EAEA,SAAU,CACR,KAAM,YACN,OAAQ,8BAAO,OACjB,CACF,EAMA,aAAoB,WAAW,UAA2C,CACxE,OAAO,YAAY,oCAAoC,EACvD,UAAU,cAAc,KAAK,aAAa,QAAQ,CACpD,CAiBA,MAAa,QAAQ,KAAqC,CACxD,KAAK,QAAQ,gBAAgB,UAAW,IAAI,GAE3C,MAAM,KAAK,qBAAqB,QAAQ,KAAK,OAAQ,CAAC,CAAC,EAExD,KAAK,qBAAqB,KAAK,OAAQ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
/** 📦 @alwatr/nitrobase-user-management v7.10.1 */
|
|
2
|
-
__dev_mode__: console.debug("📦 @alwatr/nitrobase-user-management v7.10.1");
|
|
3
|
-
import{dirname,resolve}from"node:path";import{createLogger as createLogger2}from"@alwatr/logger";import{Region}from"@alwatr/nitrobase-types";import{existsSync,makeEmptyFile}from"@alwatr/node-fs";import{createLogger}from"@alwatr/logger";var logger=createLogger("@alwatr/nitrobase-user-management");var NitrobaseUserManagement=class _NitrobaseUserManagement{constructor(nitrobase_){this.nitrobase_=nitrobase_;this.logger_=createLogger2("user-management");this.logger_.logMethod?.("constructor");this.userListCollection_=this.nitrobase_.openCollection(_NitrobaseUserManagement.nitrobaseIds.userList)}static{this.nitrobaseIds={userList:{name:"user-list",region:Region.Managers,schemaVer:1},userInfo:{name:"user-info",region:Region.PerUser}}}static async initialize(nitrobase){logger.logMethod?.("NitrobaseUserManagement.initialize");nitrobase.newCollection(this.nitrobaseIds.userList)}async newUser(user){this.logger_.logMethodArgs?.("newUser",user);(await this.userListCollection_).addItem(user.userId,{});this.newUserInfoDocument_(user.userId,user.data);await this.makeTokenFile_(user.userId,user.userToken,user.isManager)}async hasUser(userId){const userExists=(await this.userListCollection_).hasItem(userId);this.logger_.logMethodFull?.("hasUser",{userId},userExists);return userExists}async getUserData(userId){this.logger_.logMethodArgs?.("getUserData",userId);const userInfoDocument=await this.openUserInfoDocument_(userId);return userInfoDocument.getData()}async getUserMeta(userId){this.logger_.logMethodArgs?.("getUserMeta",userId);const document=await this.openUserInfoDocument_(userId);return document.getStoreMeta()}save(userId){this.logger_.logMethodArgs?.("save",userId);this.openUserInfoDocument_(userId).then(document=>{document.save()})}saveImmediate(userId){this.logger_.logMethodArgs?.("saveImmediate",userId);this.openUserInfoDocument_(userId).then(document=>{document.saveImmediate()})}async userIds(){return(await this.userListCollection_).ids()}async verifyUserToken(userId,token){const tokenFileExist=existsSync(await this.getUserTokenFilePath_(userId,token));this.logger_.logMethodFull?.("verifyUserToken",{userId,token:token.slice(0,12)+"..."},tokenFileExist);return tokenFileExist}async getUserDirectory(userId){const userInfoDocument=await this.openUserInfoDocument_(userId);const userDirectoryPath=dirname(resolve(this.nitrobase_.config.rootPath,userInfoDocument.path));this.logger_.logMethodFull?.("getUserDirectory",{userId},userDirectoryPath);return userDirectoryPath}newUserInfoDocument_(userId,userInfo){this.logger_.logMethodArgs?.("newUserInfoDocument",{userId,userInfo});const storeId={..._NitrobaseUserManagement.nitrobaseIds.userInfo,ownerId:userId};if(this.nitrobase_.hasStore(storeId)){this.logger_.accident?.("newUserInfoDocument_","store_already_exists",storeId);return}this.nitrobase_.newDocument(storeId,userInfo)}openUserInfoDocument_(userId){this.logger_.logMethodArgs?.("openUserInfoDocument_",userId);return this.nitrobase_.openDocument({..._NitrobaseUserManagement.nitrobaseIds.userInfo,ownerId:userId})}async makeTokenFile_(userId,token,isManager){this.logger_.logMethodArgs?.("makeTokenFile",{userId,token:token.slice(0,12)+"...",isManager});const userDirectory=await this.getUserDirectory(userId);await makeEmptyFile(resolve(userDirectory,`.token/${token}.asn`));if(isManager===true){await makeEmptyFile(resolve(userDirectory,".auth/manager.asn"))}}async getUserTokenFilePath_(userId,token){const userTokenFilePath=resolve(await this.getUserDirectory(userId),`.token/${token}.asn`);this.logger_.logMethodFull?.("getTokenFilePath",{userId,token:token.slice(0,12)+"..."},userTokenFilePath);return userTokenFilePath}};export{NitrobaseUserManagement};
|
|
4
|
-
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/user-management.ts", "../src/logger.ts"],
|
|
4
|
-
"sourcesContent": ["import {dirname, resolve} from 'node:path';\n\nimport {createLogger} from '@alwatr/logger';\nimport {Region, type AlwatrAuth, type StoreFileId, type StoreFileMeta} from '@alwatr/nitrobase-types';\nimport {existsSync, makeEmptyFile} from '@alwatr/node-fs';\n\nimport {logger} from './logger.js';\n\nimport type {AlwatrNitrobase} from '@alwatr/nitrobase-engine';\nimport type {DocumentReference} from '@alwatr/nitrobase-reference';\n\n/**\n * New user data type.\n */\nexport type NewUser<TUser extends JsonObject> = AlwatrAuth & {\n data: TUser;\n isManager?: true;\n};\n\n/**\n * Nitrobase User Management Interface.\n */\ninterface NitrobaseUserManagementInterface<TUser extends JsonObject> {\n /**\n * Create a new user.\n * @param user The new user data.\n */\n newUser(user: NewUser<TUser>): Promise<void>;\n\n /**\n * Check if a user exists.\n * @param userId The user ID.\n * @returns True if the user exists, false otherwise.\n */\n hasUser(userId: string): Promise<boolean>;\n\n /**\n * Get user data.\n * @param userId The user ID.\n * @returns The user data.\n */\n getUserData(userId: string): Promise<TUser>;\n\n /**\n * Get user meta data.\n * @param userId The user ID.\n * @returns The user meta data.\n */\n getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>>;\n\n /**\n * Verify user token.\n * @param userId The user ID.\n * @param token The user token.\n * @returns True if the token is valid, false otherwise.\n */\n verifyUserToken(userId: string, token: string): Promise<boolean>;\n\n /**\n * Save user data to storage.\n * @param userId The user ID.\n */\n save(userId: string): void;\n\n /**\n * Immediately save user data to storage.\n * @param userId The user ID.\n */\n saveImmediate(userId: string): void;\n\n /**\n * Get all user IDs.\n * @returns An async generator of user IDs.\n */\n userIds(): Promise<Generator<string, void, void>>;\n\n /**\n * Get the user directory path.\n * @param userId The user ID.\n * @returns The user directory path.\n */\n getUserDirectory(userId: string): Promise<string>;\n}\n\n/**\n * Nitrobase User Management Class.\n * Manages user data and authentication.\n */\nexport class NitrobaseUserManagement<TUser extends JsonObject> implements NitrobaseUserManagementInterface<TUser> {\n /**\n * Default Nitrobase store IDs used by this class.\n */\n public static readonly nitrobaseIds = {\n userList: {\n name: 'user-list',\n region: Region.Managers,\n schemaVer: 1,\n } as Readonly<StoreFileId>,\n\n userInfo: {\n name: 'user-info',\n region: Region.PerUser,\n } as Readonly<StoreFileId>,\n } as const;\n\n /**\n * Initialize the user management module.\n * @param nitrobase The Nitrobase instance.\n */\n public static async initialize(nitrobase: AlwatrNitrobase): Promise<void> {\n logger.logMethod?.('NitrobaseUserManagement.initialize');\n nitrobase.newCollection(this.nitrobaseIds.userList);\n }\n\n protected logger_ = createLogger('user-management');\n\n protected userListCollection_;\n\n /**\n * Create a new instance of NitrobaseUserManagement.\n * @param nitrobase_ The Nitrobase instance.\n */\n constructor(protected readonly nitrobase_: AlwatrNitrobase) {\n this.logger_.logMethod?.('constructor');\n this.userListCollection_ = this.nitrobase_.openCollection(NitrobaseUserManagement.nitrobaseIds.userList);\n }\n\n // Implementation of NitrobaseUserManagementInterface methods with JSDoc comments\n\n public async newUser(user: NewUser<TUser>): Promise<void> {\n this.logger_.logMethodArgs?.('newUser', user);\n\n (await this.userListCollection_).addItem(user.userId, {});\n\n this.newUserInfoDocument_(user.userId, user.data);\n\n await this.makeTokenFile_(user.userId, user.userToken, user.isManager);\n }\n\n public async hasUser(userId: string): Promise<boolean> {\n const userExists = (await this.userListCollection_).hasItem(userId);\n this.logger_.logMethodFull?.('hasUser', {userId}, userExists);\n return userExists;\n }\n\n public async getUserData(userId: string): Promise<TUser> {\n this.logger_.logMethodArgs?.('getUserData', userId);\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n return userInfoDocument.getData();\n }\n\n public async getUserMeta(userId: string): Promise<Readonly<StoreFileMeta>> {\n this.logger_.logMethodArgs?.('getUserMeta', userId);\n const document = await this.openUserInfoDocument_(userId);\n return document.getStoreMeta();\n }\n\n public save(userId: string): void {\n this.logger_.logMethodArgs?.('save', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.save();\n });\n }\n\n public saveImmediate(userId: string): void {\n this.logger_.logMethodArgs?.('saveImmediate', userId);\n this.openUserInfoDocument_(userId).then((document) => {\n document.saveImmediate();\n });\n }\n\n public async userIds(): Promise<Generator<string, void, void>> {\n return (await this.userListCollection_).ids();\n }\n\n public async verifyUserToken(userId: string, token: string): Promise<boolean> {\n const tokenFileExist = existsSync(await this.getUserTokenFilePath_(userId, token));\n this.logger_.logMethodFull?.('verifyUserToken', {userId, token: token.slice(0, 12) + '...'}, tokenFileExist);\n return tokenFileExist;\n }\n\n public async getUserDirectory(userId: string): Promise<string> {\n const userInfoDocument = await this.openUserInfoDocument_(userId);\n const userDirectoryPath = dirname(resolve(this.nitrobase_.config.rootPath, userInfoDocument.path));\n this.logger_.logMethodFull?.('getUserDirectory', {userId}, userDirectoryPath);\n return userDirectoryPath;\n }\n\n /**\n * Create a new user info document.\n * @param userId The user ID.\n * @param userInfo The user info data.\n */\n protected newUserInfoDocument_(userId: string, userInfo: TUser): void {\n this.logger_.logMethodArgs?.('newUserInfoDocument', {userId, userInfo});\n const storeId: StoreFileId = {\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n };\n if (this.nitrobase_.hasStore(storeId)) {\n this.logger_.accident?.('newUserInfoDocument_', 'store_already_exists', storeId);\n return;\n }\n this.nitrobase_.newDocument<TUser>(storeId, userInfo);\n }\n\n /**\n * Open the user info document.\n * @param userId The user ID.\n * @returns A promise resolving to the user info document.\n */\n protected openUserInfoDocument_(userId: string): Promise<DocumentReference<TUser>> {\n this.logger_.logMethodArgs?.('openUserInfoDocument_', userId);\n return this.nitrobase_.openDocument<TUser>({\n ...NitrobaseUserManagement.nitrobaseIds.userInfo,\n ownerId: userId,\n });\n }\n\n /**\n * Create a token file for the user.\n * @param userId The user ID.\n * @param token The user token.\n * @param isManager Whether the user is a manager.\n */\n protected async makeTokenFile_(userId: string, token: string, isManager?: true): Promise<void> {\n this.logger_.logMethodArgs?.('makeTokenFile', {userId, token: token.slice(0, 12) + '...', isManager});\n const userDirectory = await this.getUserDirectory(userId);\n\n await makeEmptyFile(resolve(userDirectory, `.token/${token}.asn`));\n\n if (isManager === true) {\n await makeEmptyFile(resolve(userDirectory, '.auth/manager.asn'));\n }\n }\n\n /**\n * Get the path to the user token file.\n * @param userId The user ID.\n * @param token The user token.\n * @returns The path to the user token file.\n */\n protected async getUserTokenFilePath_(userId: string, token: string): Promise<string> {\n const userTokenFilePath = resolve(await this.getUserDirectory(userId), `.token/${token}.asn`);\n this.logger_.logMethodFull?.('getTokenFilePath', {userId, token: token.slice(0, 12) + '...'}, userTokenFilePath);\n return userTokenFilePath;\n }\n}\n", "import {createLogger} from '@alwatr/logger';\n\nimport type {} from '@alwatr/nanolib';\nimport type {} from '@alwatr/type-helper';\n\nexport const logger = /* #__PURE__ */ createLogger(__package_name__);\n"],
|
|
5
|
-
"mappings": ";;AAAA,OAAQ,QAAS,YAAc,YAE/B,OAAQ,gBAAAA,kBAAmB,iBAC3B,OAAQ,WAAoE,0BAC5E,OAAQ,WAAY,kBAAoB,kBCJxC,OAAQ,iBAAmB,iBAKpB,IAAM,OAAyB,aAAa,mCAAgB,EDmF5D,IAAM,wBAAN,MAAM,wBAAqG,CAkChH,YAA+B,WAA6B,CAA7B,2BAR/B,KAAU,QAAUC,cAAa,iBAAiB,EAShD,KAAK,QAAQ,YAAY,aAAa,EACtC,KAAK,oBAAsB,KAAK,WAAW,eAAe,yBAAwB,aAAa,QAAQ,CACzG,CAjCA,YAAuB,aAAe,CACpC,SAAU,CACR,KAAM,YACN,OAAQ,OAAO,SACf,UAAW,CACb,EAEA,SAAU,CACR,KAAM,YACN,OAAQ,OAAO,OACjB,CACF,EAMA,aAAoB,WAAW,UAA2C,CACxE,OAAO,YAAY,oCAAoC,EACvD,UAAU,cAAc,KAAK,aAAa,QAAQ,CACpD,CAiBA,MAAa,QAAQ,KAAqC,CACxD,KAAK,QAAQ,gBAAgB,UAAW,IAAI,GAE3C,MAAM,KAAK,qBAAqB,QAAQ,KAAK,OAAQ,CAAC,CAAC,EAExD,KAAK,qBAAqB,KAAK,OAAQ,KAAK,IAAI,EAEhD,MAAM,KAAK,eAAe,KAAK,OAAQ,KAAK,UAAW,KAAK,SAAS,CACvE,CAEA,MAAa,QAAQ,OAAkC,CACrD,MAAM,YAAc,MAAM,KAAK,qBAAqB,QAAQ,MAAM,EAClE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