@warlock.js/auth 4.0.171 → 4.1.1
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/README.md +37 -0
- package/cjs/index.cjs +807 -0
- package/cjs/index.cjs.map +1 -0
- package/esm/commands/auth-cleanup-command.d.mts +13 -0
- package/esm/commands/auth-cleanup-command.d.mts.map +1 -0
- package/esm/commands/auth-cleanup-command.mjs +34 -0
- package/esm/commands/auth-cleanup-command.mjs.map +1 -0
- package/esm/commands/jwt-secret-generator-command.d.mts +5 -0
- package/esm/commands/jwt-secret-generator-command.d.mts.map +1 -0
- package/esm/commands/jwt-secret-generator-command.mjs +15 -0
- package/esm/commands/jwt-secret-generator-command.mjs.map +1 -0
- package/esm/contracts/auth-contract.d.mts +40 -0
- package/esm/contracts/auth-contract.d.mts.map +1 -0
- package/esm/contracts/index.d.mts +2 -0
- package/esm/contracts/types.d.mts +170 -0
- package/esm/contracts/types.d.mts.map +1 -0
- package/esm/contracts/types.mjs +25 -0
- package/esm/contracts/types.mjs.map +1 -0
- package/esm/index.d.mts +15 -0
- package/esm/index.mjs +17 -0
- package/esm/middleware/auth.middleware.d.mts +22 -0
- package/esm/middleware/auth.middleware.d.mts.map +1 -0
- package/esm/middleware/auth.middleware.mjs +72 -0
- package/esm/middleware/auth.middleware.mjs.map +1 -0
- package/esm/middleware/index.mjs +3 -0
- package/esm/models/access-token/access-token.model.d.mts +13 -0
- package/esm/models/access-token/access-token.model.d.mts.map +1 -0
- package/esm/models/access-token/access-token.model.mjs +23 -0
- package/esm/models/access-token/access-token.model.mjs.map +1 -0
- package/esm/models/access-token/index.d.mts +1 -0
- package/esm/models/access-token/index.mjs +3 -0
- package/esm/models/access-token/migration.mjs +24 -0
- package/esm/models/access-token/migration.mjs.map +1 -0
- package/esm/models/auth.model.d.mts +63 -0
- package/esm/models/auth.model.d.mts.map +1 -0
- package/esm/models/auth.model.mjs +77 -0
- package/esm/models/auth.model.mjs.map +1 -0
- package/esm/models/index.d.mts +8 -0
- package/esm/models/index.d.mts.map +1 -0
- package/esm/models/index.mjs +14 -0
- package/esm/models/index.mjs.map +1 -0
- package/esm/models/refresh-token/index.d.mts +1 -0
- package/esm/models/refresh-token/index.mjs +3 -0
- package/esm/models/refresh-token/migration.mjs +27 -0
- package/esm/models/refresh-token/migration.mjs.map +1 -0
- package/esm/models/refresh-token/refresh-token.model.d.mts +36 -0
- package/esm/models/refresh-token/refresh-token.model.d.mts.map +1 -0
- package/esm/models/refresh-token/refresh-token.model.mjs +58 -0
- package/esm/models/refresh-token/refresh-token.model.mjs.map +1 -0
- package/esm/services/auth-events.d.mts +89 -0
- package/esm/services/auth-events.d.mts.map +1 -0
- package/esm/services/auth-events.mjs +68 -0
- package/esm/services/auth-events.mjs.map +1 -0
- package/esm/services/auth.service.d.mts +95 -0
- package/esm/services/auth.service.d.mts.map +1 -0
- package/esm/services/auth.service.mjs +275 -0
- package/esm/services/auth.service.mjs.map +1 -0
- package/esm/services/generate-jwt-secret.d.mts +5 -0
- package/esm/services/generate-jwt-secret.d.mts.map +1 -0
- package/esm/services/generate-jwt-secret.mjs +48 -0
- package/esm/services/generate-jwt-secret.mjs.map +1 -0
- package/esm/services/index.d.mts +4 -0
- package/esm/services/index.mjs +6 -0
- package/esm/services/jwt.d.mts +52 -0
- package/esm/services/jwt.d.mts.map +1 -0
- package/esm/services/jwt.mjs +58 -0
- package/esm/services/jwt.mjs.map +1 -0
- package/esm/utils/auth-error-codes.d.mts +23 -0
- package/esm/utils/auth-error-codes.d.mts.map +1 -0
- package/esm/utils/auth-error-codes.mjs +23 -0
- package/esm/utils/auth-error-codes.mjs.map +1 -0
- package/llms-full.txt +1023 -0
- package/llms.txt +16 -0
- package/package.json +47 -36
- package/skills/auth-basics/SKILL.md +88 -0
- package/skills/customize-user-type/SKILL.md +137 -0
- package/skills/handle-login-and-logout/SKILL.md +160 -0
- package/skills/manage-tokens/SKILL.md +169 -0
- package/skills/overview/SKILL.md +66 -0
- package/skills/protect-routes/SKILL.md +105 -0
- package/skills/register-user/SKILL.md +135 -0
- package/skills/run-auth-commands/SKILL.md +125 -0
- package/esm/commands/auth-cleanup-command.d.ts +0 -10
- package/esm/commands/auth-cleanup-command.d.ts.map +0 -1
- package/esm/commands/auth-cleanup-command.js +0 -29
- package/esm/commands/auth-cleanup-command.js.map +0 -1
- package/esm/commands/jwt-secret-generator-command.d.ts +0 -2
- package/esm/commands/jwt-secret-generator-command.d.ts.map +0 -1
- package/esm/commands/jwt-secret-generator-command.js +0 -7
- package/esm/commands/jwt-secret-generator-command.js.map +0 -1
- package/esm/contracts/auth-contract.d.ts +0 -23
- package/esm/contracts/auth-contract.d.ts.map +0 -1
- package/esm/contracts/index.d.ts +0 -3
- package/esm/contracts/index.d.ts.map +0 -1
- package/esm/contracts/types.d.ts +0 -167
- package/esm/contracts/types.d.ts.map +0 -1
- package/esm/contracts/types.js +0 -20
- package/esm/contracts/types.js.map +0 -1
- package/esm/index.d.ts +0 -8
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -1
- package/esm/index.js.map +0 -1
- package/esm/middleware/auth.middleware.d.ts +0 -2
- package/esm/middleware/auth.middleware.d.ts.map +0 -1
- package/esm/middleware/auth.middleware.js +0 -72
- package/esm/middleware/auth.middleware.js.map +0 -1
- package/esm/middleware/index.d.ts +0 -2
- package/esm/middleware/index.d.ts.map +0 -1
- package/esm/models/access-token/access-token.model.d.ts +0 -9
- package/esm/models/access-token/access-token.model.d.ts.map +0 -1
- package/esm/models/access-token/access-token.model.js +0 -14
- package/esm/models/access-token/access-token.model.js.map +0 -1
- package/esm/models/access-token/index.d.ts +0 -2
- package/esm/models/access-token/index.d.ts.map +0 -1
- package/esm/models/access-token/migration.d.ts +0 -2
- package/esm/models/access-token/migration.d.ts.map +0 -1
- package/esm/models/access-token/migration.js +0 -22
- package/esm/models/access-token/migration.js.map +0 -1
- package/esm/models/auth.model.d.ts +0 -58
- package/esm/models/auth.model.d.ts.map +0 -1
- package/esm/models/auth.model.js +0 -68
- package/esm/models/auth.model.js.map +0 -1
- package/esm/models/index.d.ts +0 -5
- package/esm/models/index.d.ts.map +0 -1
- package/esm/models/index.js +0 -1
- package/esm/models/index.js.map +0 -1
- package/esm/models/refresh-token/index.d.ts +0 -2
- package/esm/models/refresh-token/index.d.ts.map +0 -1
- package/esm/models/refresh-token/migration.d.ts +0 -2
- package/esm/models/refresh-token/migration.d.ts.map +0 -1
- package/esm/models/refresh-token/migration.js +0 -23
- package/esm/models/refresh-token/migration.js.map +0 -1
- package/esm/models/refresh-token/refresh-token.model.d.ts +0 -32
- package/esm/models/refresh-token/refresh-token.model.d.ts.map +0 -1
- package/esm/models/refresh-token/refresh-token.model.js +0 -53
- package/esm/models/refresh-token/refresh-token.model.js.map +0 -1
- package/esm/services/auth-events.d.ts +0 -85
- package/esm/services/auth-events.d.ts.map +0 -1
- package/esm/services/auth-events.js +0 -65
- package/esm/services/auth-events.js.map +0 -1
- package/esm/services/auth.service.d.ts +0 -92
- package/esm/services/auth.service.d.ts.map +0 -1
- package/esm/services/auth.service.js +0 -322
- package/esm/services/auth.service.js.map +0 -1
- package/esm/services/generate-jwt-secret.d.ts +0 -2
- package/esm/services/generate-jwt-secret.d.ts.map +0 -1
- package/esm/services/generate-jwt-secret.js +0 -47
- package/esm/services/generate-jwt-secret.js.map +0 -1
- package/esm/services/index.d.ts +0 -5
- package/esm/services/index.d.ts.map +0 -1
- package/esm/services/jwt.d.ts +0 -23
- package/esm/services/jwt.d.ts.map +0 -1
- package/esm/services/jwt.js +0 -40
- package/esm/services/jwt.js.map +0 -1
- package/esm/utils/auth-error-codes.d.ts +0 -18
- package/esm/utils/auth-error-codes.d.ts.map +0 -1
- package/esm/utils/auth-error-codes.js +0 -18
- package/esm/utils/auth-error-codes.js.map +0 -1
- package/esm/utils/duration.d.ts +0 -45
- package/esm/utils/duration.d.ts.map +0 -1
- package/esm/utils/duration.js +0 -93
- package/esm/utils/duration.js.map +0 -1
- package/esm/utils/index.d.ts +0 -3
- package/esm/utils/index.d.ts.map +0 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Model } from "@warlock.js/cascade";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/auth/src/models/access-token/access-token.model.d.ts
|
|
4
|
+
declare class AccessToken extends Model {
|
|
5
|
+
/**
|
|
6
|
+
* {@inheritDoc}
|
|
7
|
+
*/
|
|
8
|
+
static table: string;
|
|
9
|
+
static schema: any;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { AccessToken };
|
|
13
|
+
//# sourceMappingURL=access-token.model.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-token.model.d.mts","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/access-token/access-token.model.ts"],"mappings":";;;cAWa,WAAA,SAAoB,KAAK;;AAAtC;;SAIgB,KAAA;EAAA,OAEA,MAAA;AAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Model } from "@warlock.js/cascade";
|
|
2
|
+
import { v } from "@warlock.js/seal";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/auth/src/models/access-token/access-token.model.ts
|
|
5
|
+
const accessTokenSchema = v.object({
|
|
6
|
+
token: v.string().required(),
|
|
7
|
+
last_access: v.date().defaultNow().optional(),
|
|
8
|
+
user_id: v.scalar().required(),
|
|
9
|
+
user_type: v.string().required(),
|
|
10
|
+
is_active: v.boolean().default(true)
|
|
11
|
+
});
|
|
12
|
+
var AccessToken = class extends Model {
|
|
13
|
+
static {
|
|
14
|
+
this.table = "access_tokens";
|
|
15
|
+
}
|
|
16
|
+
static {
|
|
17
|
+
this.schema = accessTokenSchema;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
export { AccessToken };
|
|
23
|
+
//# sourceMappingURL=access-token.model.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access-token.model.mjs","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/access-token/access-token.model.ts"],"sourcesContent":["import { Model } from \"@warlock.js/cascade\";\r\nimport { v } from \"@warlock.js/seal\";\r\n\r\nconst accessTokenSchema = v.object({\r\n token: v.string().required(),\r\n last_access: v.date().defaultNow().optional(),\r\n user_id: v.scalar().required(),\r\n user_type: v.string().required(),\r\n is_active: v.boolean().default(true),\r\n});\r\n\r\nexport class AccessToken extends Model {\r\n /**\r\n * {@inheritDoc}\r\n */\r\n public static table = \"access_tokens\";\r\n\r\n public static schema = accessTokenSchema;\r\n}\r\n"],"mappings":";;;;AAGA,MAAM,oBAAoB,EAAE,OAAO;CACjC,OAAO,EAAE,OAAO,EAAE,SAAS;CAC3B,aAAa,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS;CAC5C,SAAS,EAAE,OAAO,EAAE,SAAS;CAC7B,WAAW,EAAE,OAAO,EAAE,SAAS;CAC/B,WAAW,EAAE,QAAQ,EAAE,QAAQ,IAAI;AACrC,CAAC;AAED,IAAa,cAAb,cAAiC,MAAM;;eAIf;;;gBAEC;;AACzB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { AccessToken } from "./access-token.model.mjs";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AccessToken } from "./access-token.model.mjs";
|
|
2
|
+
import { migrate } from "@warlock.js/cascade";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/auth/src/models/access-token/migration.ts
|
|
5
|
+
const AccessTokenMigration = migrate(AccessToken, {
|
|
6
|
+
name: "accessToken",
|
|
7
|
+
up() {
|
|
8
|
+
this.createTableIfNotExists();
|
|
9
|
+
this.primaryUuid();
|
|
10
|
+
this.text("token").unique();
|
|
11
|
+
this.timestamp("last_access").nullable();
|
|
12
|
+
this.uuid("user_id").index();
|
|
13
|
+
this.string("user_type", 50).nullable();
|
|
14
|
+
this.boolean("is_active").nullable();
|
|
15
|
+
this.timestamps();
|
|
16
|
+
},
|
|
17
|
+
down() {
|
|
18
|
+
this.dropTableIfExists();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
export { AccessTokenMigration };
|
|
24
|
+
//# sourceMappingURL=migration.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.mjs","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/access-token/migration.ts"],"sourcesContent":["import { migrate } from \"@warlock.js/cascade\";\r\nimport { AccessToken } from \"./access-token.model\";\r\n\r\nexport const AccessTokenMigration = migrate(AccessToken, {\r\n name: \"accessToken\",\r\n up() {\r\n // Create table\r\n this.createTableIfNotExists();\r\n\r\n // Primary key\r\n this.primaryUuid();\r\n\r\n // Token field\r\n this.text(\"token\").unique();\r\n this.timestamp(\"last_access\").nullable();\r\n\r\n // User reference (flat columns for cross-driver compatibility)\r\n this.uuid(\"user_id\").index();\r\n this.string(\"user_type\", 50).nullable();\r\n\r\n // Status (for token revocation)\r\n this.boolean(\"is_active\").nullable();\r\n\r\n // Timestamps\r\n this.timestamps();\r\n },\r\n down() {\r\n this.dropTableIfExists();\r\n },\r\n});\r\n"],"mappings":";;;;AAGA,MAAa,uBAAuB,QAAQ,aAAa;CACvD,MAAM;CACN,KAAK;EAEH,KAAK,uBAAuB;EAG5B,KAAK,YAAY;EAGjB,KAAK,KAAK,OAAO,EAAE,OAAO;EAC1B,KAAK,UAAU,aAAa,EAAE,SAAS;EAGvC,KAAK,KAAK,SAAS,EAAE,MAAM;EAC3B,KAAK,OAAO,aAAa,EAAE,EAAE,SAAS;EAGtC,KAAK,QAAQ,WAAW,EAAE,SAAS;EAGnC,KAAK,WAAW;CAClB;CACA,OAAO;EACL,KAAK,kBAAkB;CACzB;AACF,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { RefreshToken } from "./refresh-token/refresh-token.model.mjs";
|
|
2
|
+
import { AccessTokenOutput, DeviceInfo, TokenPair } from "../contracts/types.mjs";
|
|
3
|
+
import { Authenticable } from "../contracts/auth-contract.mjs";
|
|
4
|
+
import { ChildModel, Model, ModelSchema } from "@warlock.js/cascade";
|
|
5
|
+
|
|
6
|
+
//#region ../../@warlock.js/auth/src/models/auth.model.d.ts
|
|
7
|
+
declare abstract class Auth<Schema extends ModelSchema = ModelSchema> extends Model<Schema> implements Authenticable {
|
|
8
|
+
/**
|
|
9
|
+
* Get user type
|
|
10
|
+
*/
|
|
11
|
+
abstract get userType(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Get access token payload
|
|
14
|
+
*/
|
|
15
|
+
accessTokenPayload(): {
|
|
16
|
+
id: any;
|
|
17
|
+
userType: string;
|
|
18
|
+
created_at: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Create both access and refresh tokens
|
|
22
|
+
*/
|
|
23
|
+
createTokenPair(deviceInfo?: DeviceInfo): Promise<TokenPair>;
|
|
24
|
+
/**
|
|
25
|
+
* Generate access token
|
|
26
|
+
*/
|
|
27
|
+
generateAccessToken(data?: any): Promise<AccessTokenOutput>;
|
|
28
|
+
/**
|
|
29
|
+
* Generate refresh token
|
|
30
|
+
*/
|
|
31
|
+
generateRefreshToken(deviceInfo?: DeviceInfo): Promise<RefreshToken | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Remove current access token
|
|
34
|
+
*/
|
|
35
|
+
removeAccessToken(token: string): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Remove refresh token
|
|
38
|
+
*/
|
|
39
|
+
removeRefreshToken(token: string): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Remove all access tokens
|
|
42
|
+
*/
|
|
43
|
+
removeAllAccessTokens(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Revoke all tokens (logout from all devices)
|
|
46
|
+
*/
|
|
47
|
+
revokeAllTokens(): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Get active sessions
|
|
50
|
+
*/
|
|
51
|
+
activeSessions(): Promise<RefreshToken[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Attempt to login the user
|
|
54
|
+
*/
|
|
55
|
+
static attempt(this: ChildModel<Auth>, data: any): Promise<Auth | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Confirm password
|
|
58
|
+
*/
|
|
59
|
+
confirmPassword(password: string): Promise<boolean>;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { Auth };
|
|
63
|
+
//# sourceMappingURL=auth.model.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.model.d.mts","names":[],"sources":["../../../../../../@warlock.js/auth/src/models/auth.model.ts"],"mappings":";;;;;;uBAMsB,IAAA,gBAAoB,WAAA,GAAc,WAAA,UAC9C,KAAA,CAAM,MAAA,aACH,aAAA;;AAFb;;eAOsB,QAAA,CAAA;EAPoB;;;EAYjC,kBAAA,CAAA;;;;;EAqBwC;;;EAdlC,eAAA,CAAgB,UAAA,GAAa,UAAA,GAAa,OAAA,CAAQ,SAAA;EA4Bf;;;EArBnC,mBAAA,CAAoB,IAAA,SAAa,OAAA,CAAQ,iBAAA;EA0CvB;;;EAnClB,oBAAA,CAAqB,UAAA,GAAa,UAAA,GAAa,OAAA,CAAQ,YAAA;EA0CJ;;;EAnCnD,iBAAA,CAAkB,KAAA,WAAgB,OAAA;EAtCvB;;;EA6CX,kBAAA,CAAmB,KAAA,WAAgB,OAAA;EA/CM;;;EAsDzC,qBAAA,CAAA,GAAyB,OAAA;EA/ClB;;;EAsDP,eAAA,CAAA,GAAmB,OAAA;;;;EAOnB,cAAA,CAAA,GAAkB,OAAA,CAAQ,YAAA;EAjDgB;;;EAAA,OAwDnC,OAAA,CAAQ,IAAA,EAAM,UAAA,CAAW,IAAA,GAAO,IAAA,QAAY,OAAA,CAAQ,IAAA;EAjD1B;;;EAwDjC,eAAA,CAAgB,QAAA,WAAmB,OAAA;AAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { authService } from "../services/auth.service.mjs";
|
|
2
|
+
import "../services/index.mjs";
|
|
3
|
+
import { Model } from "@warlock.js/cascade";
|
|
4
|
+
|
|
5
|
+
//#region ../../@warlock.js/auth/src/models/auth.model.ts
|
|
6
|
+
var Auth = class extends Model {
|
|
7
|
+
/**
|
|
8
|
+
* Get access token payload
|
|
9
|
+
*/
|
|
10
|
+
accessTokenPayload() {
|
|
11
|
+
return authService.buildAccessTokenPayload(this);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create both access and refresh tokens
|
|
15
|
+
*/
|
|
16
|
+
async createTokenPair(deviceInfo) {
|
|
17
|
+
return authService.createTokenPair(this, deviceInfo);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Generate access token
|
|
21
|
+
*/
|
|
22
|
+
async generateAccessToken(data) {
|
|
23
|
+
return authService.generateAccessToken(this, data);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate refresh token
|
|
27
|
+
*/
|
|
28
|
+
async generateRefreshToken(deviceInfo) {
|
|
29
|
+
return authService.createRefreshToken(this, deviceInfo);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Remove current access token
|
|
33
|
+
*/
|
|
34
|
+
async removeAccessToken(token) {
|
|
35
|
+
return authService.removeAccessToken(this, token);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Remove refresh token
|
|
39
|
+
*/
|
|
40
|
+
async removeRefreshToken(token) {
|
|
41
|
+
return authService.removeRefreshToken(this, token);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Remove all access tokens
|
|
45
|
+
*/
|
|
46
|
+
async removeAllAccessTokens() {
|
|
47
|
+
return authService.removeAllAccessTokens(this);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Revoke all tokens (logout from all devices)
|
|
51
|
+
*/
|
|
52
|
+
async revokeAllTokens() {
|
|
53
|
+
return authService.revokeAllTokens(this);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get active sessions
|
|
57
|
+
*/
|
|
58
|
+
async activeSessions() {
|
|
59
|
+
return authService.getActiveSessions(this);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Attempt to login the user
|
|
63
|
+
*/
|
|
64
|
+
static async attempt(data) {
|
|
65
|
+
return authService.attemptLogin(this, data);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Confirm password
|
|
69
|
+
*/
|
|
70
|
+
async confirmPassword(password) {
|
|
71
|
+
return authService.verifyPassword(password, this.string("password"));
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { Auth };
|
|
77
|
+
//# sourceMappingURL=auth.model.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.model.mjs","names":[],"sources":["../../../../../../@warlock.js/auth/src/models/auth.model.ts"],"sourcesContent":["import { type ChildModel, Model, type ModelSchema } from \"@warlock.js/cascade\";\r\nimport type { Authenticable } from \"../contracts/auth-contract\";\r\nimport type { AccessTokenOutput, DeviceInfo, TokenPair } from \"../contracts/types\";\r\nimport { authService } from \"../services\";\r\nimport type { RefreshToken } from \"./refresh-token/refresh-token.model\";\r\n\r\nexport abstract class Auth<Schema extends ModelSchema = ModelSchema>\r\n extends Model<Schema>\r\n implements Authenticable\r\n{\r\n /**\r\n * Get user type\r\n */\r\n public abstract get userType(): string;\r\n\r\n /**\r\n * Get access token payload\r\n */\r\n public accessTokenPayload() {\r\n return authService.buildAccessTokenPayload(this);\r\n }\r\n\r\n /**\r\n * Create both access and refresh tokens\r\n */\r\n public async createTokenPair(deviceInfo?: DeviceInfo): Promise<TokenPair> {\r\n return authService.createTokenPair(this, deviceInfo);\r\n }\r\n\r\n /**\r\n * Generate access token\r\n */\r\n public async generateAccessToken(data?: any): Promise<AccessTokenOutput> {\r\n return authService.generateAccessToken(this, data);\r\n }\r\n\r\n /**\r\n * Generate refresh token\r\n */\r\n public async generateRefreshToken(deviceInfo?: DeviceInfo): Promise<RefreshToken | undefined> {\r\n return authService.createRefreshToken(this, deviceInfo);\r\n }\r\n\r\n /**\r\n * Remove current access token\r\n */\r\n public async removeAccessToken(token: string): Promise<void> {\r\n return authService.removeAccessToken(this, token);\r\n }\r\n\r\n /**\r\n * Remove refresh token\r\n */\r\n public async removeRefreshToken(token: string): Promise<void> {\r\n return authService.removeRefreshToken(this, token);\r\n }\r\n\r\n /**\r\n * Remove all access tokens\r\n */\r\n public async removeAllAccessTokens(): Promise<void> {\r\n return authService.removeAllAccessTokens(this);\r\n }\r\n\r\n /**\r\n * Revoke all tokens (logout from all devices)\r\n */\r\n public async revokeAllTokens(): Promise<void> {\r\n return authService.revokeAllTokens(this);\r\n }\r\n\r\n /**\r\n * Get active sessions\r\n */\r\n public async activeSessions(): Promise<RefreshToken[]> {\r\n return authService.getActiveSessions(this);\r\n }\r\n\r\n /**\r\n * Attempt to login the user\r\n */\r\n public static async attempt(this: ChildModel<Auth>, data: any): Promise<Auth | null> {\r\n return authService.attemptLogin(this, data);\r\n }\r\n\r\n /**\r\n * Confirm password\r\n */\r\n public async confirmPassword(password: string): Promise<boolean> {\r\n return authService.verifyPassword(password, this.string(\"password\")!);\r\n }\r\n}\r\n"],"mappings":";;;;;AAMA,IAAsB,OAAtB,cACU,MAEV;;;;CASE,AAAO,qBAAqB;EAC1B,OAAO,YAAY,wBAAwB,IAAI;CACjD;;;;CAKA,MAAa,gBAAgB,YAA6C;EACxE,OAAO,YAAY,gBAAgB,MAAM,UAAU;CACrD;;;;CAKA,MAAa,oBAAoB,MAAwC;EACvE,OAAO,YAAY,oBAAoB,MAAM,IAAI;CACnD;;;;CAKA,MAAa,qBAAqB,YAA4D;EAC5F,OAAO,YAAY,mBAAmB,MAAM,UAAU;CACxD;;;;CAKA,MAAa,kBAAkB,OAA8B;EAC3D,OAAO,YAAY,kBAAkB,MAAM,KAAK;CAClD;;;;CAKA,MAAa,mBAAmB,OAA8B;EAC5D,OAAO,YAAY,mBAAmB,MAAM,KAAK;CACnD;;;;CAKA,MAAa,wBAAuC;EAClD,OAAO,YAAY,sBAAsB,IAAI;CAC/C;;;;CAKA,MAAa,kBAAiC;EAC5C,OAAO,YAAY,gBAAgB,IAAI;CACzC;;;;CAKA,MAAa,iBAA0C;EACrD,OAAO,YAAY,kBAAkB,IAAI;CAC3C;;;;CAKA,aAAoB,QAAgC,MAAiC;EACnF,OAAO,YAAY,aAAa,MAAM,IAAI;CAC5C;;;;CAKA,MAAa,gBAAgB,UAAoC;EAC/D,OAAO,YAAY,eAAe,UAAU,KAAK,OAAO,UAAU,CAAE;CACtE;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RefreshToken } from "./refresh-token/refresh-token.model.mjs";
|
|
2
|
+
import { Auth } from "./auth.model.mjs";
|
|
3
|
+
import { AccessToken } from "./access-token/access-token.model.mjs";
|
|
4
|
+
//#region ../../@warlock.js/auth/src/models/index.d.ts
|
|
5
|
+
declare const authMigrations: any[];
|
|
6
|
+
//#endregion
|
|
7
|
+
export { authMigrations };
|
|
8
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../../../../@warlock.js/auth/src/models/index.ts"],"mappings":";;;;cAOa,cAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AccessToken } from "./access-token/access-token.model.mjs";
|
|
2
|
+
import "./access-token/index.mjs";
|
|
3
|
+
import { RefreshToken } from "./refresh-token/refresh-token.model.mjs";
|
|
4
|
+
import "./refresh-token/index.mjs";
|
|
5
|
+
import { AccessTokenMigration } from "./access-token/migration.mjs";
|
|
6
|
+
import { RefreshTokenMigration } from "./refresh-token/migration.mjs";
|
|
7
|
+
import { Auth } from "./auth.model.mjs";
|
|
8
|
+
|
|
9
|
+
//#region ../../@warlock.js/auth/src/models/index.ts
|
|
10
|
+
const authMigrations = [AccessTokenMigration, RefreshTokenMigration];
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { authMigrations };
|
|
14
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../../@warlock.js/auth/src/models/index.ts"],"sourcesContent":["import { AccessTokenMigration } from \"./access-token/migration\";\r\nimport { RefreshTokenMigration } from \"./refresh-token/migration\";\r\n\r\nexport * from \"./access-token\";\r\nexport * from \"./auth.model\";\r\nexport * from \"./refresh-token\";\r\n\r\nexport const authMigrations = [AccessTokenMigration, RefreshTokenMigration];\r\n"],"mappings":";;;;;;;;;AAOA,MAAa,iBAAiB,CAAC,sBAAsB,qBAAqB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { RefreshToken } from "./refresh-token.model.mjs";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { RefreshToken } from "./refresh-token.model.mjs";
|
|
2
|
+
import { migrate } from "@warlock.js/cascade";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/auth/src/models/refresh-token/migration.ts
|
|
5
|
+
const RefreshTokenMigration = migrate(RefreshToken, {
|
|
6
|
+
name: "refreshToken",
|
|
7
|
+
up() {
|
|
8
|
+
this.createTableIfNotExists();
|
|
9
|
+
this.primaryUuid();
|
|
10
|
+
this.text("token").unique();
|
|
11
|
+
this.uuid("user_id").index();
|
|
12
|
+
this.string("user_type", 50).nullable();
|
|
13
|
+
this.text("family_id").index().nullable();
|
|
14
|
+
this.timestamp("expires_at").index().nullable();
|
|
15
|
+
this.timestamp("last_used_at").nullable();
|
|
16
|
+
this.timestamp("revoked_at").nullable();
|
|
17
|
+
this.json("device_info").nullable();
|
|
18
|
+
this.timestamps();
|
|
19
|
+
},
|
|
20
|
+
down() {
|
|
21
|
+
this.dropTableIfExists();
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { RefreshTokenMigration };
|
|
27
|
+
//# sourceMappingURL=migration.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.mjs","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/refresh-token/migration.ts"],"sourcesContent":["import { migrate } from \"@warlock.js/cascade\";\nimport { RefreshToken } from \"./refresh-token.model\";\n\nexport const RefreshTokenMigration = migrate(RefreshToken, {\n name: \"refreshToken\",\n up() {\n // Create table\n this.createTableIfNotExists();\n\n // Primary key\n this.primaryUuid();\n\n // Token fields\n this.text(\"token\").unique();\n this.uuid(\"user_id\").index();\n this.string(\"user_type\", 50).nullable();\n this.text(\"family_id\").index().nullable();\n this.timestamp(\"expires_at\").index().nullable();\n this.timestamp(\"last_used_at\").nullable();\n this.timestamp(\"revoked_at\").nullable();\n this.json(\"device_info\").nullable();\n\n // Timestamps\n this.timestamps();\n },\n down() {\n this.dropTableIfExists();\n },\n});\n"],"mappings":";;;;AAGA,MAAa,wBAAwB,QAAQ,cAAc;CACzD,MAAM;CACN,KAAK;EAEH,KAAK,uBAAuB;EAG5B,KAAK,YAAY;EAGjB,KAAK,KAAK,OAAO,EAAE,OAAO;EAC1B,KAAK,KAAK,SAAS,EAAE,MAAM;EAC3B,KAAK,OAAO,aAAa,EAAE,EAAE,SAAS;EACtC,KAAK,KAAK,WAAW,EAAE,MAAM,EAAE,SAAS;EACxC,KAAK,UAAU,YAAY,EAAE,MAAM,EAAE,SAAS;EAC9C,KAAK,UAAU,cAAc,EAAE,SAAS;EACxC,KAAK,UAAU,YAAY,EAAE,SAAS;EACtC,KAAK,KAAK,aAAa,EAAE,SAAS;EAGlC,KAAK,WAAW;CAClB;CACA,OAAO;EACL,KAAK,kBAAkB;CACzB;AACF,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Model } from "@warlock.js/cascade";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/auth/src/models/refresh-token/refresh-token.model.d.ts
|
|
4
|
+
declare class RefreshToken extends Model {
|
|
5
|
+
/**
|
|
6
|
+
* {@inheritDoc}
|
|
7
|
+
*/
|
|
8
|
+
static table: string;
|
|
9
|
+
/**
|
|
10
|
+
* {@inheritDoc}
|
|
11
|
+
*/
|
|
12
|
+
static schema: any;
|
|
13
|
+
/**
|
|
14
|
+
* Check if token is expired
|
|
15
|
+
*/
|
|
16
|
+
get isExpired(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Check if token is revoked
|
|
19
|
+
*/
|
|
20
|
+
get isRevoked(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Check if token is valid (not expired and not revoked)
|
|
23
|
+
*/
|
|
24
|
+
get isValid(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Revoke this token
|
|
27
|
+
*/
|
|
28
|
+
revoke(): Promise<this>;
|
|
29
|
+
/**
|
|
30
|
+
* Mark token as used (update last_used_at)
|
|
31
|
+
*/
|
|
32
|
+
markAsUsed(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { RefreshToken };
|
|
36
|
+
//# sourceMappingURL=refresh-token.model.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh-token.model.d.mts","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/refresh-token/refresh-token.model.ts"],"mappings":";;;cAca,YAAA,SAAqB,KAAA;;AAAlC;;SAIgB,KAAA;EAiCS;;;EAAA,OA5BT,MAAA;EATuB;;;EAAA,IAc1B,SAAA,CAAA;EAAA;;;EAAA,IASA,SAAA,CAAA;EAcY;;;EAAA,IAPZ,OAAA,CAAA;EAcuB;;;EAPrB,MAAA,CAAA,GAAU,OAAA;;;;EAOV,UAAA,CAAA,GAAc,OAAA;AAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Model } from "@warlock.js/cascade";
|
|
2
|
+
import { v } from "@warlock.js/seal";
|
|
3
|
+
|
|
4
|
+
//#region ../../@warlock.js/auth/src/models/refresh-token/refresh-token.model.ts
|
|
5
|
+
const refreshTokenSchema = v.object({
|
|
6
|
+
token: v.string().required(),
|
|
7
|
+
user_id: v.scalar().required(),
|
|
8
|
+
user_type: v.string().required(),
|
|
9
|
+
family_id: v.string().required(),
|
|
10
|
+
expires_at: v.date().required(),
|
|
11
|
+
last_used_at: v.date().default(() => /* @__PURE__ */ new Date()),
|
|
12
|
+
revoked_at: v.date().optional(),
|
|
13
|
+
device_info: v.record(v.any()).optional()
|
|
14
|
+
});
|
|
15
|
+
var RefreshToken = class extends Model {
|
|
16
|
+
static {
|
|
17
|
+
this.table = "refresh_tokens";
|
|
18
|
+
}
|
|
19
|
+
static {
|
|
20
|
+
this.schema = refreshTokenSchema;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Check if token is expired
|
|
24
|
+
*/
|
|
25
|
+
get isExpired() {
|
|
26
|
+
const expiresAt = this.get("expires_at");
|
|
27
|
+
if (!expiresAt) return false;
|
|
28
|
+
return /* @__PURE__ */ new Date() > new Date(expiresAt);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check if token is revoked
|
|
32
|
+
*/
|
|
33
|
+
get isRevoked() {
|
|
34
|
+
return !!this.get("revoked_at");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if token is valid (not expired and not revoked)
|
|
38
|
+
*/
|
|
39
|
+
get isValid() {
|
|
40
|
+
return !this.isExpired && !this.isRevoked;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Revoke this token
|
|
44
|
+
*/
|
|
45
|
+
async revoke() {
|
|
46
|
+
return this.merge({ revoked_at: /* @__PURE__ */ new Date() }).save();
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Mark token as used (update last_used_at)
|
|
50
|
+
*/
|
|
51
|
+
async markAsUsed() {
|
|
52
|
+
await this.merge({ last_used_at: /* @__PURE__ */ new Date() }).save();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
export { RefreshToken };
|
|
58
|
+
//# sourceMappingURL=refresh-token.model.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refresh-token.model.mjs","names":[],"sources":["../../../../../../../@warlock.js/auth/src/models/refresh-token/refresh-token.model.ts"],"sourcesContent":["import { Model } from \"@warlock.js/cascade\";\nimport { v } from \"@warlock.js/seal\";\n\nconst refreshTokenSchema = v.object({\n token: v.string().required(),\n user_id: v.scalar().required(),\n user_type: v.string().required(),\n family_id: v.string().required(),\n expires_at: v.date().required(),\n last_used_at: v.date().default(() => new Date()),\n revoked_at: v.date().optional(),\n device_info: v.record(v.any()).optional(),\n});\n\nexport class RefreshToken extends Model {\n /**\n * {@inheritDoc}\n */\n public static table = \"refresh_tokens\";\n\n /**\n * {@inheritDoc}\n */\n public static schema = refreshTokenSchema;\n\n /**\n * Check if token is expired\n */\n public get isExpired(): boolean {\n const expiresAt = this.get(\"expires_at\");\n if (!expiresAt) return false;\n return new Date() > new Date(expiresAt);\n }\n\n /**\n * Check if token is revoked\n */\n public get isRevoked(): boolean {\n return !!this.get(\"revoked_at\");\n }\n\n /**\n * Check if token is valid (not expired and not revoked)\n */\n public get isValid(): boolean {\n return !this.isExpired && !this.isRevoked;\n }\n\n /**\n * Revoke this token\n */\n public async revoke(): Promise<this> {\n return this.merge({ revoked_at: new Date() }).save();\n }\n\n /**\n * Mark token as used (update last_used_at)\n */\n public async markAsUsed(): Promise<void> {\n await this.merge({ last_used_at: new Date() }).save();\n }\n}\n"],"mappings":";;;;AAGA,MAAM,qBAAqB,EAAE,OAAO;CAClC,OAAO,EAAE,OAAO,EAAE,SAAS;CAC3B,SAAS,EAAE,OAAO,EAAE,SAAS;CAC7B,WAAW,EAAE,OAAO,EAAE,SAAS;CAC/B,WAAW,EAAE,OAAO,EAAE,SAAS;CAC/B,YAAY,EAAE,KAAK,EAAE,SAAS;CAC9B,cAAc,EAAE,KAAK,EAAE,8BAAc,IAAI,KAAK,CAAC;CAC/C,YAAY,EAAE,KAAK,EAAE,SAAS;CAC9B,aAAa,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;AAC1C,CAAC;AAED,IAAa,eAAb,cAAkC,MAAM;;eAIhB;;;gBAKC;;;;;CAKvB,IAAW,YAAqB;EAC9B,MAAM,YAAY,KAAK,IAAI,YAAY;EACvC,IAAI,CAAC,WAAW,OAAO;EACvB,uBAAO,IAAI,KAAK,IAAI,IAAI,KAAK,SAAS;CACxC;;;;CAKA,IAAW,YAAqB;EAC9B,OAAO,CAAC,CAAC,KAAK,IAAI,YAAY;CAChC;;;;CAKA,IAAW,UAAmB;EAC5B,OAAO,CAAC,KAAK,aAAa,CAAC,KAAK;CAClC;;;;CAKA,MAAa,SAAwB;EACnC,OAAO,KAAK,MAAM,EAAE,4BAAY,IAAI,KAAK,EAAE,CAAC,EAAE,KAAK;CACrD;;;;CAKA,MAAa,aAA4B;EACvC,MAAM,KAAK,MAAM,EAAE,8BAAc,IAAI,KAAK,EAAE,CAAC,EAAE,KAAK;CACtD;AACF"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { RefreshToken } from "../models/refresh-token/refresh-token.model.mjs";
|
|
2
|
+
import { Auth } from "../models/auth.model.mjs";
|
|
3
|
+
import { DeviceInfo, TokenPair } from "../contracts/types.mjs";
|
|
4
|
+
import { EventSubscription } from "@mongez/events";
|
|
5
|
+
|
|
6
|
+
//#region ../../@warlock.js/auth/src/services/auth-events.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Auth event payload types
|
|
9
|
+
*/
|
|
10
|
+
type AuthEventPayloads = {
|
|
11
|
+
"login.success": [user: Auth, tokenPair: TokenPair, deviceInfo?: DeviceInfo];
|
|
12
|
+
"login.failed": [credentials: {
|
|
13
|
+
email?: string;
|
|
14
|
+
username?: string;
|
|
15
|
+
}, reason: string];
|
|
16
|
+
"login.attempt": [credentials: {
|
|
17
|
+
email?: string;
|
|
18
|
+
username?: string;
|
|
19
|
+
}];
|
|
20
|
+
logout: [user: Auth];
|
|
21
|
+
"logout.all": [user: Auth];
|
|
22
|
+
"logout.failsafe": [user: Auth];
|
|
23
|
+
"token.created": [user: Auth, tokenPair: TokenPair];
|
|
24
|
+
"token.refreshed": [user: Auth, newTokenPair: TokenPair, oldRefreshToken: RefreshToken];
|
|
25
|
+
"token.revoked": [user: Auth, token: RefreshToken];
|
|
26
|
+
"token.expired": [token: RefreshToken];
|
|
27
|
+
"token.familyRevoked": [familyId: string, tokens: RefreshToken[]];
|
|
28
|
+
"password.changed": [user: Auth];
|
|
29
|
+
"password.resetRequested": [user: Auth, resetToken: string];
|
|
30
|
+
"password.reset": [user: Auth];
|
|
31
|
+
"session.created": [user: Auth, refreshToken: RefreshToken, deviceInfo?: DeviceInfo];
|
|
32
|
+
"session.destroyed": [user: Auth, refreshToken: RefreshToken];
|
|
33
|
+
"cleanup.completed": [expiredCount: number];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Auth event names
|
|
37
|
+
*/
|
|
38
|
+
type AuthEventName = keyof AuthEventPayloads;
|
|
39
|
+
/**
|
|
40
|
+
* Callback type for a specific event
|
|
41
|
+
*/
|
|
42
|
+
type AuthEventCallback<T extends AuthEventName> = (...args: AuthEventPayloads[T]) => void | Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Type-safe auth events manager
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Subscribe to events with full autocomplete
|
|
49
|
+
* authEvents.on("login.success", (user, tokenPair, deviceInfo) => {
|
|
50
|
+
* console.log(`User ${user.id} logged in`);
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* authEvents.on("token.refreshed", (user, newPair, oldToken) => {
|
|
54
|
+
* console.log(`Token refreshed for user ${user.id}`);
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* // Trigger events
|
|
58
|
+
* authEvents.emit("login.success", user, tokenPair, deviceInfo);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare const authEvents: {
|
|
62
|
+
/**
|
|
63
|
+
* Subscribe to an auth event
|
|
64
|
+
*/
|
|
65
|
+
on<T extends AuthEventName>(event: T, callback: AuthEventCallback<T>): EventSubscription;
|
|
66
|
+
/**
|
|
67
|
+
* Subscribe to an auth event (alias for `on`)
|
|
68
|
+
*/
|
|
69
|
+
subscribe<T extends AuthEventName>(event: T, callback: AuthEventCallback<T>): EventSubscription;
|
|
70
|
+
/**
|
|
71
|
+
* Emit an auth event
|
|
72
|
+
*/
|
|
73
|
+
emit<T extends AuthEventName>(event: T, ...args: AuthEventPayloads[T]): void;
|
|
74
|
+
/**
|
|
75
|
+
* Emit an auth event (alias for `emit`)
|
|
76
|
+
*/
|
|
77
|
+
trigger<T extends AuthEventName>(event: T, ...args: AuthEventPayloads[T]): void;
|
|
78
|
+
/**
|
|
79
|
+
* Unsubscribe from all auth events
|
|
80
|
+
*/
|
|
81
|
+
unsubscribeAll(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Unsubscribe from a specific auth event
|
|
84
|
+
*/
|
|
85
|
+
off(event?: AuthEventName): void;
|
|
86
|
+
};
|
|
87
|
+
//#endregion
|
|
88
|
+
export { AuthEventCallback, AuthEventName, AuthEventPayloads, authEvents };
|
|
89
|
+
//# sourceMappingURL=auth-events.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-events.d.mts","names":[],"sources":["../../../../../../@warlock.js/auth/src/services/auth-events.ts"],"mappings":";;;;;;;;;KAQY,iBAAA;EAEV,eAAA,GAAkB,IAAA,EAAM,IAAA,EAAM,SAAA,EAAW,SAAA,EAAW,UAAA,GAAa,UAAA;EACjE,cAAA,GAAiB,WAAA;IAAe,KAAA;IAAgB,QAAA;EAAA,GAAqB,MAAA;EACrE,eAAA,GAAkB,WAAA;IAAe,KAAA;IAAgB,QAAA;EAAA;EAGjD,MAAA,GAAS,IAAA,EAAM,IAAA;EACf,YAAA,GAAe,IAAA,EAAM,IAAA;EACrB,iBAAA,GAAoB,IAAA,EAAM,IAAA;EAG1B,eAAA,GAAkB,IAAA,EAAM,IAAA,EAAM,SAAA,EAAW,SAAA;EACzC,iBAAA,GAAoB,IAAA,EAAM,IAAA,EAAM,YAAA,EAAc,SAAA,EAAW,eAAA,EAAiB,YAAA;EAC1E,eAAA,GAAkB,IAAA,EAAM,IAAA,EAAM,KAAA,EAAO,YAAA;EACrC,eAAA,GAAkB,KAAA,EAAO,YAAA;EACzB,qBAAA,GAAwB,QAAA,UAAkB,MAAA,EAAQ,YAAA;EAGlD,kBAAA,GAAqB,IAAA,EAAM,IAAA;EAC3B,yBAAA,GAA4B,IAAA,EAAM,IAAA,EAAM,UAAA;EACxC,gBAAA,GAAmB,IAAA,EAAM,IAAA;EAGzB,iBAAA,GAAoB,IAAA,EAAM,IAAA,EAAM,YAAA,EAAc,YAAA,EAAc,UAAA,GAAa,UAAA;EACzE,mBAAA,GAAsB,IAAA,EAAM,IAAA,EAAM,YAAA,EAAc,YAAA;EAGhD,mBAAA,GAAsB,YAAA;AAAA;;;;KAMZ,aAAA,SAAsB,iBAAiB;;;;KAKvC,iBAAA,WAA4B,aAAA,QACnC,IAAA,EAAM,iBAAA,CAAkB,CAAA,aACjB,OAAA;;;;;;;;;;;;;;;;;;;cAyBC,UAAA;EAzDe;;;eA6Db,aAAA,EAAa,KAAA,EAAS,CAAA,EAAC,QAAA,EAAY,iBAAA,CAAkB,CAAA,IAAK,iBAAA;EA1DzC;;;sBAiEV,aAAA,EAAa,KAAA,EAAS,CAAA,EAAC,QAAA,EAAY,iBAAA,CAAkB,CAAA,IAAK,iBAAA;EAhEpD;;;iBAuEX,aAAA,EAAa,KAAA,EAAS,CAAA,KAAC,IAAA,EAAW,iBAAA,CAAkB,CAAA;EAvEO;;;oBA8ExD,aAAA,EAAa,KAAA,EAAS,CAAA,KAAC,IAAA,EAAW,iBAAA,CAAkB,CAAA;EA7ExC;;;;EACL;;;cA0Fb,aAAA;AAAA"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import events from "@mongez/events";
|
|
2
|
+
|
|
3
|
+
//#region ../../@warlock.js/auth/src/services/auth-events.ts
|
|
4
|
+
/**
|
|
5
|
+
* Event namespace prefix for auth events
|
|
6
|
+
*/
|
|
7
|
+
const AUTH_EVENT_PREFIX = "auth.";
|
|
8
|
+
/**
|
|
9
|
+
* Type-safe auth events manager
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Subscribe to events with full autocomplete
|
|
14
|
+
* authEvents.on("login.success", (user, tokenPair, deviceInfo) => {
|
|
15
|
+
* console.log(`User ${user.id} logged in`);
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* authEvents.on("token.refreshed", (user, newPair, oldToken) => {
|
|
19
|
+
* console.log(`Token refreshed for user ${user.id}`);
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Trigger events
|
|
23
|
+
* authEvents.emit("login.success", user, tokenPair, deviceInfo);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const authEvents = {
|
|
27
|
+
/**
|
|
28
|
+
* Subscribe to an auth event
|
|
29
|
+
*/
|
|
30
|
+
on(event, callback) {
|
|
31
|
+
return events.subscribe(AUTH_EVENT_PREFIX + event, callback);
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Subscribe to an auth event (alias for `on`)
|
|
35
|
+
*/
|
|
36
|
+
subscribe(event, callback) {
|
|
37
|
+
return this.on(event, callback);
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Emit an auth event
|
|
41
|
+
*/
|
|
42
|
+
emit(event, ...args) {
|
|
43
|
+
events.trigger(AUTH_EVENT_PREFIX + event, ...args);
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* Emit an auth event (alias for `emit`)
|
|
47
|
+
*/
|
|
48
|
+
trigger(event, ...args) {
|
|
49
|
+
this.emit(event, ...args);
|
|
50
|
+
},
|
|
51
|
+
/**
|
|
52
|
+
* Unsubscribe from all auth events
|
|
53
|
+
*/
|
|
54
|
+
unsubscribeAll() {
|
|
55
|
+
events.unsubscribeNamespace(AUTH_EVENT_PREFIX.slice(0, -1));
|
|
56
|
+
},
|
|
57
|
+
/**
|
|
58
|
+
* Unsubscribe from a specific auth event
|
|
59
|
+
*/
|
|
60
|
+
off(event) {
|
|
61
|
+
if (event) events.unsubscribe(AUTH_EVENT_PREFIX + event);
|
|
62
|
+
else this.unsubscribeAll();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
export { authEvents };
|
|
68
|
+
//# sourceMappingURL=auth-events.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-events.mjs","names":[],"sources":["../../../../../../@warlock.js/auth/src/services/auth-events.ts"],"sourcesContent":["import events, { type EventSubscription } from \"@mongez/events\";\nimport type { DeviceInfo, TokenPair } from \"../contracts/types\";\nimport type { Auth } from \"../models/auth.model\";\nimport type { RefreshToken } from \"../models/refresh-token\";\n\n/**\n * Auth event payload types\n */\nexport type AuthEventPayloads = {\n // Login events\n \"login.success\": [user: Auth, tokenPair: TokenPair, deviceInfo?: DeviceInfo];\n \"login.failed\": [credentials: { email?: string; username?: string }, reason: string];\n \"login.attempt\": [credentials: { email?: string; username?: string }];\n\n // Logout events\n logout: [user: Auth];\n \"logout.all\": [user: Auth];\n \"logout.failsafe\": [user: Auth];\n\n // Token events\n \"token.created\": [user: Auth, tokenPair: TokenPair];\n \"token.refreshed\": [user: Auth, newTokenPair: TokenPair, oldRefreshToken: RefreshToken];\n \"token.revoked\": [user: Auth, token: RefreshToken];\n \"token.expired\": [token: RefreshToken];\n \"token.familyRevoked\": [familyId: string, tokens: RefreshToken[]];\n\n // Password events\n \"password.changed\": [user: Auth];\n \"password.resetRequested\": [user: Auth, resetToken: string];\n \"password.reset\": [user: Auth];\n\n // Session events\n \"session.created\": [user: Auth, refreshToken: RefreshToken, deviceInfo?: DeviceInfo];\n \"session.destroyed\": [user: Auth, refreshToken: RefreshToken];\n\n // Cleanup events\n \"cleanup.completed\": [expiredCount: number];\n};\n\n/**\n * Auth event names\n */\nexport type AuthEventName = keyof AuthEventPayloads;\n\n/**\n * Callback type for a specific event\n */\nexport type AuthEventCallback<T extends AuthEventName> = (\n ...args: AuthEventPayloads[T]\n) => void | Promise<void>;\n\n/**\n * Event namespace prefix for auth events\n */\nconst AUTH_EVENT_PREFIX = \"auth.\";\n\n/**\n * Type-safe auth events manager\n *\n * @example\n * ```typescript\n * // Subscribe to events with full autocomplete\n * authEvents.on(\"login.success\", (user, tokenPair, deviceInfo) => {\n * console.log(`User ${user.id} logged in`);\n * });\n *\n * authEvents.on(\"token.refreshed\", (user, newPair, oldToken) => {\n * console.log(`Token refreshed for user ${user.id}`);\n * });\n *\n * // Trigger events\n * authEvents.emit(\"login.success\", user, tokenPair, deviceInfo);\n * ```\n */\nexport const authEvents = {\n /**\n * Subscribe to an auth event\n */\n on<T extends AuthEventName>(event: T, callback: AuthEventCallback<T>): EventSubscription {\n return events.subscribe(AUTH_EVENT_PREFIX + event, callback as Function);\n },\n\n /**\n * Subscribe to an auth event (alias for `on`)\n */\n subscribe<T extends AuthEventName>(event: T, callback: AuthEventCallback<T>): EventSubscription {\n return this.on(event, callback);\n },\n\n /**\n * Emit an auth event\n */\n emit<T extends AuthEventName>(event: T, ...args: AuthEventPayloads[T]): void {\n events.trigger(AUTH_EVENT_PREFIX + event, ...args);\n },\n\n /**\n * Emit an auth event (alias for `emit`)\n */\n trigger<T extends AuthEventName>(event: T, ...args: AuthEventPayloads[T]): void {\n this.emit(event, ...args);\n },\n\n /**\n * Unsubscribe from all auth events\n */\n unsubscribeAll(): void {\n events.unsubscribeNamespace(AUTH_EVENT_PREFIX.slice(0, -1));\n },\n\n /**\n * Unsubscribe from a specific auth event\n */\n off(event?: AuthEventName): void {\n if (event) {\n events.unsubscribe(AUTH_EVENT_PREFIX + event);\n } else {\n this.unsubscribeAll();\n }\n },\n};\n"],"mappings":";;;;;;AAsDA,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;AAoB1B,MAAa,aAAa;;;;CAIxB,GAA4B,OAAU,UAAmD;EACvF,OAAO,OAAO,UAAU,oBAAoB,OAAO,QAAoB;CACzE;;;;CAKA,UAAmC,OAAU,UAAmD;EAC9F,OAAO,KAAK,GAAG,OAAO,QAAQ;CAChC;;;;CAKA,KAA8B,OAAU,GAAG,MAAkC;EAC3E,OAAO,QAAQ,oBAAoB,OAAO,GAAG,IAAI;CACnD;;;;CAKA,QAAiC,OAAU,GAAG,MAAkC;EAC9E,KAAK,KAAK,OAAO,GAAG,IAAI;CAC1B;;;;CAKA,iBAAuB;EACrB,OAAO,qBAAqB,kBAAkB,MAAM,GAAG,EAAE,CAAC;CAC5D;;;;CAKA,IAAI,OAA6B;EAC/B,IAAI,OACF,OAAO,YAAY,oBAAoB,KAAK;OAE5C,KAAK,eAAe;CAExB;AACF"}
|