anzar 1.2.9 → 1.3.5

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 CHANGED
@@ -39,21 +39,17 @@ in your main entry file (`main.ts` for example), import Anzar and create your au
39
39
  import { Anzar } from "anzar";
40
40
  import anzarConfig from "anzar.yml";
41
41
 
42
- const anzar = Anzar(anzarConfig);
42
+ const anzar = new Anzar(anzarConfig);
43
43
  ```
44
44
 
45
- ⚠️ **Warning:**
46
- Only extend these functionality if you are using **JWT** authentication
47
-
48
- ⚠️ **Warning:** Implement a secure Storage solution
45
+ ⚠️ **Note**
46
+ Choose your token storage adapter
49
47
 
50
48
  ```typescript
51
- import { SessionTokens } from "anzar.types";
52
- const anzar = Anzar(anzarConfig, {
53
- getToken: () => "AccessToken",
54
- getRefreshToken: () => "RefreshToken",
55
- onTokenRefresh(tokens) => //store tokens.access, tokens.refresh,
56
- ...
49
+ import { LocalStorage } from "anzar/adapters";
50
+
51
+ const anzar = new Anzar(anzarConfig, {
52
+ storage: new LocalStorage(),
57
53
  });
58
54
  ```
59
55
 
@@ -65,12 +61,11 @@ Anzar provides authentication support for email and password.
65
61
  ### Sign Up
66
62
  To sign up a user you need to call the method register with the user's information.
67
63
  ```typescript
64
+ import type { AuthResult } from "anzar/types";
65
+
68
66
  try {
69
- const response = await anzar.Auth.register({ username, email, password });
70
- const data: AuthResponse = response.data;
71
- if (data.tokens) {
72
- // Store securely the `access`,`refresh` tokens
73
- }
67
+ const data: AuthResult = await anzar.Auth.register({ username, email, password });
68
+ console.log(data);
74
69
  } catch (e) {
75
70
  const message = e.response?.data?.message ?? "An unknown error occurred";
76
71
  console.log(message);
@@ -83,12 +78,11 @@ try {
83
78
  ### Sign In
84
79
  To sign in a user you need to call the method login.
85
80
  ```typescript
81
+ import type { AuthResult } from "anzar/types";
82
+
86
83
  try {
87
- const response = await anzar.Auth.login({ email, password });
88
- const data: AuthResponse = response.data;
89
- if (data.tokens) {
90
- // Store securely the `access`,`refresh` tokens
91
- }
84
+ const data: AuthResult = await anzar.Auth.login({ email, password });
85
+ console.log(data);
92
86
  } catch (e) {
93
87
  const message = e.response?.data?.message ?? "An unknown error occurred";
94
88
  console.log(message);
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/index.ts
21
+ var adapters_exports = {};
22
+ __export(adapters_exports, {
23
+ LocalStorage: () => LocalStorage,
24
+ MemoryStorage: () => MemoryStorage
25
+ });
26
+ module.exports = __toCommonJS(adapters_exports);
27
+
28
+ // src/adapters/local_storage.ts
29
+ var LocalStorage = class {
30
+ getToken() {
31
+ return localStorage.getItem("AccessToken");
32
+ }
33
+ getRefreshToken() {
34
+ return localStorage.getItem("RefreshToken");
35
+ }
36
+ onTokenRefresh(tokens) {
37
+ localStorage.setItem("AccessToken", tokens.access);
38
+ localStorage.setItem("RefreshToken", tokens.refresh);
39
+ }
40
+ onSessionExpired() {
41
+ localStorage.clear();
42
+ }
43
+ onLogout() {
44
+ localStorage.clear();
45
+ }
46
+ };
47
+
48
+ // src/adapters/memory_storage.ts
49
+ var MemoryStorage = class {
50
+ acessToken = null;
51
+ refreshToken = null;
52
+ getToken() {
53
+ return this.acessToken;
54
+ }
55
+ getRefreshToken() {
56
+ return this.refreshToken;
57
+ }
58
+ onTokenRefresh(tokens) {
59
+ this.acessToken = tokens.access;
60
+ this.refreshToken = tokens.refresh;
61
+ }
62
+ onSessionExpired() {
63
+ this.acessToken = null;
64
+ this.refreshToken = null;
65
+ }
66
+ onLogout() {
67
+ this.acessToken = null;
68
+ this.refreshToken = null;
69
+ }
70
+ };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-BnHsA4fU.cjs';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,21 @@
1
+ import { B as BaseStorage, S as SessionTokens } from '../base_storage-BnHsA4fU.js';
2
+
3
+ declare class LocalStorage implements BaseStorage {
4
+ getToken(): string | null;
5
+ getRefreshToken(): string | null;
6
+ onTokenRefresh(tokens: SessionTokens): void;
7
+ onSessionExpired?(): void;
8
+ onLogout?(): void;
9
+ }
10
+
11
+ declare class MemoryStorage implements BaseStorage {
12
+ private acessToken;
13
+ private refreshToken;
14
+ getToken(): string | null;
15
+ getRefreshToken(): string | null;
16
+ onTokenRefresh(tokens: SessionTokens): void;
17
+ onSessionExpired?(): void;
18
+ onLogout?(): void;
19
+ }
20
+
21
+ export { LocalStorage, MemoryStorage };
@@ -0,0 +1,27 @@
1
+ import {
2
+ MemoryStorage
3
+ } from "../chunk-5SSEPZKA.js";
4
+
5
+ // src/adapters/local_storage.ts
6
+ var LocalStorage = class {
7
+ getToken() {
8
+ return localStorage.getItem("AccessToken");
9
+ }
10
+ getRefreshToken() {
11
+ return localStorage.getItem("RefreshToken");
12
+ }
13
+ onTokenRefresh(tokens) {
14
+ localStorage.setItem("AccessToken", tokens.access);
15
+ localStorage.setItem("RefreshToken", tokens.refresh);
16
+ }
17
+ onSessionExpired() {
18
+ localStorage.clear();
19
+ }
20
+ onLogout() {
21
+ localStorage.clear();
22
+ }
23
+ };
24
+ export {
25
+ LocalStorage,
26
+ MemoryStorage
27
+ };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ * SessionTokens model
14
+ */
15
+ interface SessionTokens {
16
+ 'access': string;
17
+ 'expires_in': number;
18
+ 'refresh': string;
19
+ 'token_type': string;
20
+ }
21
+
22
+ interface BaseStorage {
23
+ getToken(): string | null;
24
+ getRefreshToken(): string | null;
25
+ onTokenRefresh(tokens: SessionTokens): void;
26
+ onSessionExpired?(): void;
27
+ onLogout?(): void;
28
+ }
29
+
30
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ /**
13
+ * SessionTokens model
14
+ */
15
+ interface SessionTokens {
16
+ 'access': string;
17
+ 'expires_in': number;
18
+ 'refresh': string;
19
+ 'token_type': string;
20
+ }
21
+
22
+ interface BaseStorage {
23
+ getToken(): string | null;
24
+ getRefreshToken(): string | null;
25
+ onTokenRefresh(tokens: SessionTokens): void;
26
+ onSessionExpired?(): void;
27
+ onLogout?(): void;
28
+ }
29
+
30
+ export type { BaseStorage as B, SessionTokens as S };
@@ -0,0 +1,27 @@
1
+ // src/adapters/memory_storage.ts
2
+ var MemoryStorage = class {
3
+ acessToken = null;
4
+ refreshToken = null;
5
+ getToken() {
6
+ return this.acessToken;
7
+ }
8
+ getRefreshToken() {
9
+ return this.refreshToken;
10
+ }
11
+ onTokenRefresh(tokens) {
12
+ this.acessToken = tokens.access;
13
+ this.refreshToken = tokens.refresh;
14
+ }
15
+ onSessionExpired() {
16
+ this.acessToken = null;
17
+ this.refreshToken = null;
18
+ }
19
+ onLogout() {
20
+ this.acessToken = null;
21
+ this.refreshToken = null;
22
+ }
23
+ };
24
+
25
+ export {
26
+ MemoryStorage
27
+ };
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ declare const Role: {
13
+ readonly User: "User";
14
+ readonly Admin: "Admin";
15
+ };
16
+ type Role = typeof Role[keyof typeof Role];
17
+
18
+ /**
19
+ * Anzar Software API
20
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
21
+ *
22
+ * The version of the OpenAPI document: 0.6.2
23
+ * Contact: dev@anzar.io
24
+ *
25
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
26
+ * https://openapi-generator.tech
27
+ * Do not edit the class manually.
28
+ */
29
+
30
+ interface User {
31
+ '_id'?: string | null;
32
+ 'createdAt': string;
33
+ 'email': string;
34
+ 'role': Role;
35
+ 'username': string;
36
+ 'verified': boolean;
37
+ }
38
+
39
+ /**
40
+ * Anzar Software API
41
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
42
+ *
43
+ * The version of the OpenAPI document: 0.6.2
44
+ * Contact: dev@anzar.io
45
+ *
46
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
47
+ * https://openapi-generator.tech
48
+ * Do not edit the class manually.
49
+ */
50
+ /**
51
+ * Verification model
52
+ */
53
+ interface Verification {
54
+ 'link': string;
55
+ 'token': string;
56
+ }
57
+
58
+ interface AuthResult {
59
+ user: User;
60
+ verification?: Verification | null;
61
+ }
62
+
63
+ export type { AuthResult as A, User as U, Verification as V };
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Anzar Software API
3
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
4
+ *
5
+ * The version of the OpenAPI document: 0.6.2
6
+ * Contact: dev@anzar.io
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ */
12
+ declare const Role: {
13
+ readonly User: "User";
14
+ readonly Admin: "Admin";
15
+ };
16
+ type Role = typeof Role[keyof typeof Role];
17
+
18
+ /**
19
+ * Anzar Software API
20
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
21
+ *
22
+ * The version of the OpenAPI document: 0.6.2
23
+ * Contact: dev@anzar.io
24
+ *
25
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
26
+ * https://openapi-generator.tech
27
+ * Do not edit the class manually.
28
+ */
29
+
30
+ interface User {
31
+ '_id'?: string | null;
32
+ 'createdAt': string;
33
+ 'email': string;
34
+ 'role': Role;
35
+ 'username': string;
36
+ 'verified': boolean;
37
+ }
38
+
39
+ /**
40
+ * Anzar Software API
41
+ * REST API for the Anzar platform. All protected routes require a Bearer token.
42
+ *
43
+ * The version of the OpenAPI document: 0.6.2
44
+ * Contact: dev@anzar.io
45
+ *
46
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
47
+ * https://openapi-generator.tech
48
+ * Do not edit the class manually.
49
+ */
50
+ /**
51
+ * Verification model
52
+ */
53
+ interface Verification {
54
+ 'link': string;
55
+ 'token': string;
56
+ }
57
+
58
+ interface AuthResult {
59
+ user: User;
60
+ verification?: Verification | null;
61
+ }
62
+
63
+ export type { AuthResult as A, User as U, Verification as V };